Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

ibaranga
Copy link

@ibaranga ibaranga commented Sep 29, 2024

Addressing issue #19712

What

  • Introduce springHttpClientAdapter useHttpServiceProxyFactoryInterfacesConfigurator for spring-http-interface
  • 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.

  • spring-http-interface: introduce springHttpClientAdapter, fix paramDoc.mustache #19710 (comment)

  • 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

  • Read the contribution guidelines.
  • 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.
  • 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.
  • 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)
  • 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

…mDoc.mustache`

---
### 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

---

### Testing Done

- **Manual testing**: Verified that the generated code uses `WebClient`, `RestTemplate`, or `RestClient` based on the value of the `springHttpClientAdapter` property.
- **Tested different configurations**:
   - **`reactive: false` & `web-client`**: Logs a warning, suggesting the use of `rest-client` or `rest-template`.
   - **`springHttpClientAdapter: rest-template`**: Generates code with `RestTemplateAdapter` using the `httpInterfacesRestTemplateConfiguration.mustache`.
   - **`springHttpClientAdapter: web-client` (default)**: Generates code using `WebClientAdapter` from `httpInterfacesWebClientConfiguration.mustache` and ensures backward compatibility.
- **Tested `paramDoc.mustache` change**: Verified that the empty `paramDoc.mustache` prevents the generation of `@Parameter` annotations on request body parameters and resolves the compile errors caused by missing imports.

---

### PR checklist

- [ ] Read the [contribution guidelines](https://github.com/openapitools/openapi-generator/blob/master/CONTRIBUTING.md).
- [ ] 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.
- [ ] Run the following to [build the project](https://github.com/OpenAPITools/openapi-generator#14---build-projects) 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](https://gitforwindows.org/))
  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.
- [ ] File the PR against the [correct branch](https://github.com/OpenAPITools/openapi-generator/wiki/Git-Branches): `master` (upcoming `7.x.0` minor release - breaking changes with fallbacks), `8.0.x` (breaking changes without fallbacks)
- [ ] If your PR is targeting a particular programming language, @mention the [technical committee](https://github.com/openapitools/openapi-generator/#62---openapi-generator-technical-committee) members, so they are more likely to review the pull request.
@@ -28,7 +28,7 @@ public abstract class HttpInterfacesAbstractConfigurator {
{{#apis}}
@Bean(name = "{{configPackage}}.HttpInterfacesAbstractConfigurator.{{classVarName}}")
{{classname}} {{classVarName}}HttpProxy() {
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(WebClientAdapter.forClient(webClient)).build();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is marked as deprecated, replacing it with builderFor, which is also in line with the rest of the httpInterfaces*Configuration.mustache templates

@wing328
Copy link
Member

wing328 commented Oct 1, 2024

@ibaranga
Copy link
Author

ibaranga commented Oct 1, 2024

@wing328 thanks, fixed!

@wing328 wing328 modified the milestones: 7.9.0, 7.10.0 Oct 8, 2024
@wing328
Copy link
Member

wing328 commented Oct 11, 2024

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

@ibaranga
Copy link
Author

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

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

@wing328
Copy link
Member

wing328 commented Nov 18, 2024

filed #20129 to trigger all the tests

let's see how that goes

@wing328 wing328 modified the milestones: 7.10.0, 7.11.0 Nov 18, 2024
@wing328
Copy link
Member

wing328 commented Nov 18, 2024

@ibaranga can you please add sample to test the new option similar to #20071 (comment) ?

@davidkarlsen
Copy link

It would be good to test this with latest GA version of spring, as #20174 is related

@wing328
Copy link
Member

wing328 commented Nov 25, 2024

@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 wing328 closed this Nov 25, 2024
@wing328 wing328 reopened this Nov 25, 2024
@ibaranga
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants