Skip to content

fix(spring): qualify swagger @Tag annotation when the api file imports a model named Tag - #25682

Open
Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:fix/24554-spring-tag-collision
Open

Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:fix/24554-spring-tag-collision

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #24554.

If the spec defines a model named Tag and an api accepts/returns it, the generated Spring api imports both the model (org.openapitools.model.Tag) and io.swagger.v3.oas.annotations.tags.Tag. Two single-type imports of the same simple name is a hard javac error, so the generated code does not compile:

error: a type with the same simple name is already defined by the single-type-import of org.openapitools.model.Tag

The model import is the one that matters — it is the type used in the signatures — so this PR keeps it and references the annotation by its fully qualified name instead, skipping the annotation import:

import org.openapitools.model.Tag;
...
@io.swagger.v3.oas.annotations.tags.Tag(name = "pets", description = "the pets API")

The check is per api file

The detection lives in SpringCodegen.postProcessOperationsWithModels and looks at the file's own imports, not at whether the spec contains a model named Tag at all: the petstore spec defines a Tag model, but most of its api files never import it, and qualifying the annotation there would be needless churn. Files without the model import are emitted byte-for-byte unchanged — regenerating the petstore springboot sample produces no diff.

apiController.mustache gets the same treatment (it never uses @Tag itself, so only the import needs to go). That import sits behind the undocumented _api_controller_impl_ flag, so the regression test for it enables the flag explicitly.

Tests

Two tests in SpringCodegenTest generate from a minimal spec with a Tag model (model-named-tag.yaml) and assert that:

  • the model import is still present (that is the type actually used in signatures),
  • import io.swagger.v3.oas.annotations.tags.Tag; is gone,
  • the annotation is referenced fully qualified,
  • the model is still referenced by its simple name in @Schema(implementation = Tag.class) and the return type.

mvn -pl modules/openapi-generator test is green (5757 tests).

Verification

Generating the spec from the issue and running mvn compile on the generated project: before the fix, javac fails with 4 errors (the duplicate single-type-import plus three ambiguous Tag references); after the fix, BUILD SUCCESS.

One note on workarounds: nameMappings can rename the model away, but the failure mode here is a compile error in generated code that gives no hint a model name collides with a swagger annotation, so I think it is worth having the generator do the right thing by itself.


Summary by cubic

Fixes a Spring generator compile error when a spec defines a model named Tag and an api uses it. Generated api files previously imported both org.openapitools.model.Tag and io.swagger.v3.oas.annotations.tags.Tag, which javac rejects as duplicate single-type imports. The model import stays (it's the type used in signatures); the annotation is now referenced fully qualified and its import is skipped.

Detection

  • The check is per api file, based on the file's own imports, so files that don't import the model are emitted unchanged and the petstore springboot sample regenerates with no diff.
  • apiController.mustache gets the same treatment; the regression test for that path enables the undocumented _api_controller_impl_ flag.

Written for commit 14807a3. Summary will update on new commits.

View guided diff

…s a model named Tag

What: when a generated Spring api file imports a model named `Tag`, the
class-level swagger annotation is now emitted fully qualified
(`@io.swagger.v3.oas.annotations.tags.Tag`) and the
`import io.swagger.v3.oas.annotations.tags.Tag;` line is skipped, in
api.mustache and apiController.mustache. Api files that don't import such
a model are emitted byte-for-byte unchanged.

Why: importing both the model and the annotation is two single-type
imports of the same simple name, which javac rejects outright ("a type
with the same simple name is already defined by the single-type-import"),
so any spec that has a model named `Tag` and an api accepting/returning
it produced non-compiling code. The model import is the one that matters
(it is the type used in the signatures), so the annotation is the side
that has to give.

How: SpringCodegen.postProcessOperationsWithModels detects the collision
per api file, based on that file's own imports (not on whether the spec
contains a `Tag` model at all), and exposes a `qualifySwaggerTagAnnotation`
flag the templates act on. Regression tests cover the api interface and
the controller impl path; the petstore springboot sample regenerates
byte-for-byte unchanged, so no checked-in sample exercises the collision.

Fixes OpenAPITools#24554

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 5 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java:1152">
P3: The comment misstates the javac rule. The clash is two different types sharing the simple name `Tag`, not a duplicate import of the same type, which javac accepts. Reword the comment so future readers don't draw the wrong conclusion.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment on lines +1152 to +1154
// A model named `Tag` collides with io.swagger.v3.oas.annotations.tags.Tag: when an api file
// imports the model, importing the annotation too is a duplicate single-type-import, which
// javac rejects outright. The templates qualify the annotation reference and skip the import

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3: The comment misstates the javac rule. The clash is two different types sharing the simple name Tag, not a duplicate import of the same type, which javac accepts. Reword the comment so future readers don't draw the wrong conclusion.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java, line 1152:

<comment>The comment misstates the javac rule. The clash is two different types sharing the simple name `Tag`, not a duplicate import of the same type, which javac accepts. Reword the comment so future readers don't draw the wrong conclusion.</comment>

<file context>
@@ -1149,6 +1149,16 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
 
     @Override
     public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
+        // A model named `Tag` collides with io.swagger.v3.oas.annotations.tags.Tag: when an api file
+        // imports the model, importing the annotation too is a duplicate single-type-import, which
+        // javac rejects outright. The templates qualify the annotation reference and skip the import
</file context>
Suggested change
// A model named `Tag` collides with io.swagger.v3.oas.annotations.tags.Tag: when an api file
// imports the model, importing the annotation too is a duplicate single-type-import, which
// javac rejects outright. The templates qualify the annotation reference and skip the import
// A model named `Tag` collides with io.swagger.v3.oas.annotations.tags.Tag. When an api file imports
// the model, importing the annotation too gives two different types the same simple name `Tag`, which
// javac rejects. The templates qualify the annotation reference and skip its import for exactly those
// files; files that don't import the model are emitted unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Spring] Model named Tag collides with @Tag annotation import, generated code does not compile

1 participant