openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

spring-http-interface: introduce `springHttpClientAdapter`, fix `paramDoc.mustache`

Open ibaranga opened this issue 1 year ago • 4 comments

Addressing issue https://github.com/OpenAPITools/openapi-generator/issues/19712

What

  • Introduce springHttpClientAdapter for spring-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 empty paramDoc.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 in HttpInterfacesAbstractConfigurator:
    • web-client (set by default, to ensure backward compatibility)
    • rest-client
    • rest-template
  • Separate templates for each HttpInterfacesAbstractConfigurator implementation:
    • httpInterfacesRestClientConfiguration.mustache
    • httpInterfacesRestTemplateConfiguration.mustache
    • httpInterfacesWebClientConfiguration.mustache
  • Log warning for configuration mismatch
    • When reactive: false is used in combination with the reactive web-client, it warns users of potential configuration mismatches, and suggests switching to rest-template or rest-client for non-reactive configurations.
  • Remove unnecessary paramDoc
    • Added an spring-http-interface-specific empty paramDoc.mustache in JavaSpring/libraries/spring-http-interface/paramDoc.mustache
    • This prevents inheriting the @Parameter annotations from the default Spring template located at JavaSpring/paramDoc.mustache.
    • Otherwise, the generated code includes @Parameter annotations on request body parameters, which were causing compile errors due to missing imports

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:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (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 ./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 (upcoming 7.x.0 minor 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

ibaranga avatar Sep 29 '24 14:09 ibaranga

please review the errors when you've time: https://github.com/OpenAPITools/openapi-generator/actions/runs/11094295717/job/30896757555?pr=19710

wing328 avatar Oct 01 '24 12:10 wing328

@wing328 thanks, fixed!

ibaranga avatar Oct 01 '24 15:10 ibaranga

please review the errors reported by the CI when you've time.

wing328 avatar Oct 11 '24 05:10 wing328

please review the errors reported by the CI when you've time.

Thanks again, @wing328, I've just updated the PR!

ibaranga avatar Oct 16 '24 19:10 ibaranga

filed https://github.com/OpenAPITools/openapi-generator/pull/20129 to trigger all the tests

let's see how that goes

wing328 avatar Nov 18 '24 10:11 wing328

@ibaranga can you please add sample to test the new option similar to https://github.com/OpenAPITools/openapi-generator/pull/20071#issuecomment-2466666737 ?

wing328 avatar Nov 18 '24 10:11 wing328

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 avatar Nov 25 '24 11:11 davidkarlsen

@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 avatar Nov 25 '24 11:11 wing328

@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 HttpExchangeAdapter replacement

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 useHttpServiceProxyFactoryInterfacesConfigurator is missing or false

    • keep using the Spring Boot 3.1 API (HttpServiceProxyFactory#builder(WebClientAdapter(webClient))) in generated HttpInterfacesAbstractConfigurator classes, based on a provided WebClient instance
    • this will remain functional until Spring Boot 3.4.
  • When useHttpServiceProxyFactoryInterfacesConfigurator is true,

    • generate HttpInterfacesAbstractConfigurator classes that depend on a HttpServiceProxyFactory instance, allowing greater flexibility in selecting the underlying HTTP client

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)));
}

ibaranga avatar Nov 30 '24 15:11 ibaranga

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 @Parameter problem 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

ibaranga avatar Mar 05 '25 05:03 ibaranga

I analyzed your gradle/@Parameter issue and found the problem: Instead of specifying configOptions = [library : 'spring-http-interface'] you should specify library = '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.

ibaranga avatar Mar 06 '25 17:03 ibaranga

Do you have any plans to support Kotlin?🤔

dejavuhuh avatar Oct 15 '25 11:10 dejavuhuh

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.

Picazsoo avatar Nov 05 '25 08:11 Picazsoo