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

[BUG] [Java] [resttemplate] Spring's @Component annotation is imported even if generateClientAsBean is set to false #20657

Open
5 of 6 tasks
jair-tamayo opened this issue Feb 12, 2025 · 3 comments

Comments

@jair-tamayo
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The files generated from resttemplate are importing org.springframework.stereotype.Component even when generateClientAsBean property is set to false (default).

According to the docs, the @Component annotation will be only added if generateClientAsBean is set to true

So, to fix the issue, you just need to replace the current content with bellow suggestion in both places:

{{#generateClientAsBean}}
import org.springframework.stereotype.Component;
{{/generateClientAsBean}}
openapi-generator version

I'm currently using below settings - however I can reproduce the issue in prior versions

Generator: java
Version: 7.11.0
Library: resttemplate
OpenAPI declaration file content or url

I have used your petstore spec to reproduce the issue

Steps to reproduce

Run below command to generate the code

java -jar openapi-generator-cli-7.11.0.jar generate -i petstore.yaml -g java -o output --additional-properties=library=resttemplate

Go to the source files for ApiClient and UserApi, you'll see org.springframework.stereotype.Component being added by default

import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;     <----- it should not be here
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
Suggest a fix

You just need to replace the current content in:

with below code

{{#generateClientAsBean}}
import org.springframework.stereotype.Component;
{{/generateClientAsBean}}
@wing328
Copy link
Member

wing328 commented Feb 13, 2025

can you please file a PR with the suggested fix? we will review accordingly

@jair-tamayo
Copy link
Author

@wing328 do you have any docs on how to setup the repository?

I managed to clone it but I'm not able to create branches/push changes

Image

@Mattias-Sehlstedt
Copy link
Contributor

Mattias-Sehlstedt commented Feb 21, 2025

Hi @jair-tamayo, you need to create a fork of the repository, then create your branch and your feature on your fork. You can then create a PR that will be that your feature should be merge into the upstream.

For example, I have forked this repo here and then implemented my feature on a branch. I then have a PR where I request to merge my feature.

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

No branches or pull requests

3 participants