spring-http-interface: introduce `springHttpClientAdapter`, fix `paramDoc.mustache`
Addressing issue https://github.com/OpenAPITools/openapi-generator/issues/19712
What
- Introduce
springHttpClientAdapterforspring-http-interface- This property is used for selecting HTTP client implementation in Spring HTTP interfaces, with separate templates for each client configuration
- Added an
spring-http-interface-specific emptyparamDoc.mustache
Why
- Enable selecting different HTTP client implementations when generating Spring HTTP client interfaces
- Provides additional flexibility for users who want to generate non-reactive Spring Boot applications
How
-
springHttpClientAdapter: Allows users to choose between different HTTP client implementations used inHttpInterfacesAbstractConfigurator:-
web-client(set by default, to ensure backward compatibility) -
rest-client -
rest-template
-
- Separate templates for each
HttpInterfacesAbstractConfiguratorimplementation:-
httpInterfacesRestClientConfiguration.mustache -
httpInterfacesRestTemplateConfiguration.mustache -
httpInterfacesWebClientConfiguration.mustache
-
- Log warning for configuration mismatch
- When
reactive: falseis used in combination with the reactiveweb-client, it warns users of potential configuration mismatches, and suggests switching torest-templateorrest-clientfor non-reactive configurations.
- When
- Remove unnecessary paramDoc
- Added an
spring-http-interface-specific emptyparamDoc.mustacheinJavaSpring/libraries/spring-http-interface/paramDoc.mustache - This prevents inheriting the
@Parameterannotations from the default Spring template located atJavaSpring/paramDoc.mustache. - Otherwise, the generated code includes
@Parameterannotations on request body parameters, which were causing compile errors due to missing imports
- Added an
PR checklist
- [x] Read the contribution guidelines.
- [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- [x] Run the following to build the project and update samples:
(For Windows users, please run the script in Git BASH) Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./mvnw clean package ./bin/generate-samples.sh ./bin/configs/*.yaml ./bin/utils/export_docs_generators.sh./bin/generate-samples.sh bin/configs/java*. IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - [x] File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - [x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @banlevente @Zomzog @martin-mfg
please review the errors when you've time: https://github.com/OpenAPITools/openapi-generator/actions/runs/11094295717/job/30896757555?pr=19710
@wing328 thanks, fixed!
please review the errors reported by the CI when you've time.
please review the errors reported by the CI when you've time.
Thanks again, @wing328, I've just updated the PR!
filed https://github.com/OpenAPITools/openapi-generator/pull/20129 to trigger all the tests
let's see how that goes
@ibaranga can you please add sample to test the new option similar to https://github.com/OpenAPITools/openapi-generator/pull/20071#issuecomment-2466666737 ?
It would be good to test this with latest GA version of spring, as https://github.com/OpenAPITools/openapi-generator/issues/20174 is related
@davidkarlsen do you mind doing a test locally for that?
git checkout -b ibaranga-master master
git pull https://github.com/ibaranga/openapi-generator.git master
@wing328, @davidkarlsen, the existing spring-http-interface generator is using internally the HttpServiceProxyFactory#builder(HttpClientAdapter) factory method.
This HttpClientAdapter interface / HttpServiceProxyFactory#builder(HttpClientAdapter) factory method:
- were deprecated in Spring Boot 3.2 in favor of
HttpServiceProxyFactory#builderFor(HttpExchangeAdapter). - were removed in Spring Boot 3.4
- can only be used with WebClient, since there is no spring-provided HttpClientAdapter for RestClient / RestTemplate, unlike the
HttpExchangeAdapterreplacement
Considering this deprecation and the complexity of maintaining configurations and samples for 3 different client
implementations, I’ve introduced the possibility to generate a HttpInterfacesAbstractConfigurator which depends on a provided HttpServiceProxyFactory instance, rather than specific Spring client implementations.
This can be enabled by setting a new property called useHttpServiceProxyFactoryInterfacesConfigurator to "true";
To ensure backward compatibility with Spring Boot 3.1 and flexibility for Spring Boot 3.2+:
-
When
useHttpServiceProxyFactoryInterfacesConfiguratoris missing or false- keep using the Spring Boot 3.1 API (
HttpServiceProxyFactory#builder(WebClientAdapter(webClient))) in generatedHttpInterfacesAbstractConfiguratorclasses, based on a providedWebClientinstance - this will remain functional until Spring Boot 3.4.
- keep using the Spring Boot 3.1 API (
-
When
useHttpServiceProxyFactoryInterfacesConfiguratoris true,- generate
HttpInterfacesAbstractConfiguratorclasses that depend on aHttpServiceProxyFactoryinstance, allowing greater flexibility in selecting the underlying HTTP client
- generate
The changes on custom HttpInterfacesAbstractConfigurator extensions will be minimal, for example
@Configuration
public class MyConfiguration extends HttpInterfacesAbstractConfigurator {
// ...
}
// WebClient example
@Bean
public MyConfiguration myConfiguration(WebClient webClient) {
--- return new MyConfiguration(webClient)
+++ return new MyConfiguration(HttpServiceProxyFactory.builderFor(WebClientAdapter.create(webClient)));
}
// RestTemplate example
@Bean
public MyConfiguration myConfiguration(RestTemplate restTemplate) {
--- return new MyConfiguration(restTemplate)
+++ return new MyConfiguration(HttpServiceProxyFactory.builderFor(RestTemplateAdapter.create(restTemplate)));
}
Hi @ibaranga, thanks for the PR! Could you please resolve the merge conflicts and have a look at the review comments below? Also, please provide an example configuration and input specification for which the
@Parameterproblem occurs.
Hi @martin-mfg, I've addressed the PR comments.
Regarding the @Parameter problem: I can reproduce it only when generating with the Gradle plugin (but not via the CLI).
I’ve prepared an example in this sample repo: https://github.com/ibaranga/openapi-generator-spring-http-interface-example
I analyzed your gradle/
@Parameterissue and found the problem: Instead of specifyingconfigOptions = [library : 'spring-http-interface']you should specifylibrary = 'spring-http-interface'. This is the intended way, and this generates what you expect. It would be nice if you could add a warning in the gradle plugin for situations where users make this (understandable) mistake. In case you don't have time for that, please at least revert the addition of paramDoc.mustache.
Thanks for clarifying - for now I just reverted the addition of paramDoc.mustache.
Do you have any plans to support Kotlin?🤔
Do you have any plans to support Kotlin?🤔
I just opened an issue here and plan to contribute a PR with barebones implementation of kotlin-spring declarative http interface in coming days.