Skip to content

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

Open
@jair-tamayo

Description

@jair-tamayo

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions