Repository navigation
Conversation
…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
Contributor
There was a problem hiding this comment.
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 |
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #24554.
If the spec defines a model named
Tagand an api accepts/returns it, the generated Spring api imports both the model (org.openapitools.model.Tag) andio.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: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:
The check is per api file
The detection lives in
SpringCodegen.postProcessOperationsWithModelsand looks at the file's own imports, not at whether the spec contains a model namedTagat all: the petstore spec defines aTagmodel, 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.mustachegets the same treatment (it never uses@Tagitself, 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
SpringCodegenTestgenerate from a minimal spec with aTagmodel (model-named-tag.yaml) and assert that:import io.swagger.v3.oas.annotations.tags.Tag;is gone,@Schema(implementation = Tag.class)and the return type.mvn -pl modules/openapi-generator testis green (5757 tests).Verification
Generating the spec from the issue and running
mvn compileon the generated project: before the fix, javac fails with 4 errors (the duplicate single-type-import plus three ambiguousTagreferences); after the fix, BUILD SUCCESS.One note on workarounds:
nameMappingscan 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
Tagand an api uses it. Generated api files previously imported bothorg.openapitools.model.Tagandio.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
apiController.mustachegets 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.