OpenAPITools / OpenAPITools/openapi-generator

[BUG][SPRING] delegateController: The annotation @Autowired is disallowed for this location

Open Beginner friendly
#4,329 0 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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When using delegatePattern for spring-mvc configuration :

<configOptions>
	<delegatePattern>true</delegatePattern>
	<library>spring-mvc</library>
</configOptions>

In the controller, we have :

    private final ElementsApiDelegate delegate;

    public ElementsApiController(@org.springframework.beans.factory.annotation.Autowired(required = false) ElementsApiDelegate delegate) {
        this.delegate = Optional.ofNullable(delegate).orElse(new ElementsApiDelegate() {});
    }

And there is a Java compilation failure:
The annotation @Autowired is disallowed for this location.

openapi-generator version

4.1.3 and it's not a regression

OpenAPI declaration file content or url

https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/samples/yaml/pet.yml

Command line used for generation
<configuration>
	...
	<configOptions>
		<delegatePattern>true</delegatePattern>
		<library>spring-mvc</library>
	</configOptions>
	<library>spring-mvc</library>
</configuration>
Steps to reproduce

1/ run with any swagger openAPI or not
2/ configure delegatePattern + spring-mvc
3/ generate code for JAVA Spring
4/ have a look to the controller, there is a Java compilation failure
The annotation @Autowired is disallowed for this location

Related issues/PRs

#92 @cbornet

Suggest a fix

https://github.com/OpenAPITools/openapi-generator/blob/66be33a37b030fced0169b8df392c0d7817b13a1/modules/openapi-generator/src/main/resources/JavaSpring/apiController.mustache#L54

    @org.springframework.beans.factory.annotation.Autowired(required = false) private final {{classname}}Delegate delegate;

    public {{classname}}Controller({{classname}}Delegate delegate) {
    {{#jdk8}}
        this.delegate = Optional.ofNullable(delegate).orElse(new {{classname}}Delegate() {});
    }

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

Inspect modules/openapi-generator/src/main/resources/JavaSpring/apiController.mustache around the referenced line and compare the generated controller for delegatePattern with library spring-mvc. Generate the pet.yml sample using the shown configuration, then compile the generated Java code. Done means the controller no longer produces the reported @Autowired location error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.