OpenAPITools / OpenAPITools/openapi-generator

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

Open
#24,554 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in SpringCodegen.postProcessOperationsWithModels and inspect JavaSpring/api.mustache and JavaSpring/apiController.mustache. Reproduce the issue with the provided spec and Spring generation command, then use the reported regression test to verify that an API importing model Tag compiles while unaffected API files retain normal annotation imports.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.