fix(spring): avoid duplicate Tag import when a model is named Tag - #24555
Open
donald wants to merge 1 commit into
Open
fix(spring): avoid duplicate Tag import when a model is named Tag#24555donald wants to merge 1 commit into
donald wants to merge 1 commit into
Conversation
If the spec defines a model named `Tag` and an api uses it, the generated
api imported both the model 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 did not compile:
import org.openapitools.model.Tag;
import io.swagger.v3.oas.annotations.tags.Tag;
@tag(name = "pets", ...)
public interface TagApi {
default ResponseEntity<Tag> getTag(...)
error: a type with the same simple name is already defined by the
single-type-import of Tag
The model import is the one that matters -- it is the type used in the
signatures -- so reference the annotation by its fully qualified name and
skip importing it.
The detection is per api file, driven by that file's own imports rather
than by "the spec contains a model named Tag": the petstore sample defines
a Tag model but most of its api files never import it, and qualifying the
annotation there would be needless churn (it rewrote 171 sample files and
broke two existing SpringCodegenTest cases before being scoped down).
Regenerating all 787 sample configs produces no diff.
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
What
If the spec defines a model named
Tagand an api uses it, the generated Spring api imported both themodel and
io.swagger.v3.oas.annotations.tags.Tag. Two single-type-imports of the same simple name is ahard javac error, so the generated code did not compile.
This is not an ambiguity a wildcard import could resolve — both imports are explicit.
How
SpringCodegen.postProcessOperationsWithModelssetsqualifySwaggerTagAnnotationwhen the file beinggenerated imports the model
Tag;JavaSpring/api.mustacheandJavaSpring/apiController.mustachethenemit the fully qualified annotation and skip the annotation import.
The model import is kept — it is the type actually used in the signatures.
Scoping matters here. My first attempt keyed off "the spec contains a model named
Tag" and stored theflag in
additionalProperties. That is both too broad and sticky across files: the petstore sample definesa
Tagmodel, so every spring sample got the qualified annotation even though most api files neverimport the model. It rewrote 171 sample files and broke
testNoRequestMappingAnnotationandtestNoRequestMappingAnnotation_spring_cloud_default. The committed version checksobjs.getImports()formodelPackage() + ".Tag"and stores the flag per operations map, so only the filesthat actually hit the clash change.
Testing
SpringCodegenTest.modelNamedTagDoesNotClashWithSwaggerTagAnnotation— asserts the annotation import isgone, the annotation is fully qualified, and the model import is still present. Verified that this test
fails without the fix.
SpringCodegenTest(294 tests) passes, including the two cases the over-broad first attempt broke../bin/generate-samples.sh ./bin/configs/*.yaml) and./bin/utils/export_docs_generators.sh— no diff.PR checklist
(Both scripts were run; neither produced any change, so there is nothing to commit beyond the fix and its test.)
masterJava/Spring technical committee:
@bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg
🤖 Generated with Claude Code
Summary by cubic
Fixes a compile error in generated Spring APIs when a schema is named
Tagby fully qualifying the Swagger@Tagannotation only in files that import the modelTag. This avoids duplicate single-type imports and restores compilation.modelPackage.Tagimports and setqualifySwaggerTagAnnotationinSpringCodegen.postProcessOperationsWithModels.JavaSpring/api.mustacheandJavaSpring/apiController.mustache, render@io.swagger.v3.oas.annotations.tags.Tagand skip its import when flagged; keeporg.openapitools.model.Tagimported.modelNamedTagDoesNotClashWithSwaggerTagAnnotation; all Spring tests pass and regenerating samples shows no diff.Written for commit 256d4e5. Summary will update on new commits.