fix(spring): qualify swagger @Tag annotation when the api file imports a model named Tag
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.mustachegets the same treatment; the regression test for that path enables the undocumented_api_controller_impl_flag.