OpenAPITools / OpenAPITools/openapi-generator

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

Open
#20,657 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache and ApiClient.mustache, then run the documented Java generator command with the petstore.yaml spec and resttemplate library. Inspect the generated UserApi and ApiClient imports. Done means org.springframework.stereotype.Component is included only when generateClientAsBean is true.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.