Skip to content

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

Description

@donald

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If the spec defines a model named Tag and an api returns/accepts it, the generated Spring api
imports 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 does not compile.

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

This is not an ambiguity that a wildcard import could resolve — both imports are explicit.

openapi-generator version

7.25.0-SNAPSHOT, built from master at commit 4aed9c1c635.

OpenAPI declaration file content or url
openapi: 3.0.3
info: { title: t, version: 1.0.0 }
tags: [{ name: pets }]
paths:
  /tag:
    get:
      tags: [pets]
      operationId: getTag
      responses:
        "200":
          description: ok
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Tag" }
components:
  schemas:
    Tag:
      properties:
        id: { type: string }
Generation Details
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
  -i spec.yaml -g spring -o out \
  --additional-properties=interfaceOnly=true,useSpringBoot3=true
Steps to reproduce
  1. Save the spec above as spec.yaml
  2. Run the command above
  3. Look at out/src/main/java/org/openapitools/api/TagApi.java

Actual output

import org.openapitools.model.Tag;                    // line 8
...
import io.swagger.v3.oas.annotations.tags.Tag;        // line 18
...
@Tag(name = "pets", description = "the pets API")
public interface TagApi {
    default ResponseEntity<Tag> getTag(...)

Both Tag types are imported explicitly — javac rejects the file.

Expected output

The model import is the one that matters (it is the type used in the signature), so the annotation should
be referenced by its fully qualified name and not imported:

import org.openapitools.model.Tag;
...
@io.swagger.v3.oas.annotations.tags.Tag(name = "pets", description = "the pets API")
public interface TagApi {
    default ResponseEntity<Tag> getTag(...)
Suggest a fix

In SpringCodegen.postProcessOperationsWithModels, detect that the file being generated imports the model
Tag, and in that case have JavaSpring/api.mustache and JavaSpring/apiController.mustache emit the
fully qualified annotation and skip import io.swagger.v3.oas.annotations.tags.Tag;.

The check has to be per api file (based on that file's imports), not "does the spec contain a model named
Tag" — the sample petstore spec defines a Tag model, but most of its api files never import it, and
qualifying the annotation there would be needless churn.

I have a patch with a regression test ready and will open a PR shortly.

Related issues/PRs

Searched open and closed issues/PRs for Tag model name clashes in the Spring generator; I could not find
an existing report.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions