fix(spring): avoid duplicate Tag import when a model is named Tag - #24555
fix(spring): avoid duplicate Tag import when a model is named Tag#24555donald wants to merge 1 commit into
Conversation
|
I actually have also hit this problem with the Edit: Maybe a flag to use the full name for swagger annotations? |
|
@gs-covariance good point — I would rather not make it a flag, though, for two reasons:
The direction I would suggest instead is to generalise the detection, not add a switch: keep a map of That is a bigger change than this bug fix, so I would prefer to keep this PR narrow (it unbreaks |
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.
256d4e5 to
4516b9e
Compare
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 prevents duplicate single-type imports and restores compilation.modelPackage.Tagimports and setqualifySwaggerTagAnnotationinSpringCodegen.postProcessOperationsWithModels.JavaSpring/api.mustache, render@io.swagger.v3.oas.annotations.tags.Tagand skip its import when flagged; inJavaSpring/apiController.mustache, skip the annotation import when flagged. Keeporg.openapitools.model.Tagimported.modelNamedTagDoesNotClashWithSwaggerTagAnnotation; all Spring tests pass and regenerating samples shows no diff.Written for commit 4516b9e. Summary will update on new commits.