openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] Generated code references non-existent classes on "duplicate" property definitions

Open bvschaik opened this issue 3 months ago • 1 comments

Bug Report Checklist

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

Given the following openapi spec: https://github.com/bvschaik/openapi-nonexistent-class-reproducer/blob/main/spec.json

  • One definition of catName as string in CatFields, with title "Given name"
  • One Cat with a name property that references CatFields/catName
  • One definition of dogName as string in DogFields, also with title "Given name"
  • One Dog with a nickname property that references DogFields/dogName

The issue: starting from the OpenAPI generator 7.23.0, this generates a Java Dog class with a property nickname of type DogName. The DogName class is not generated:

public class Dog  {
  @JsonbProperty("nickname")
  protected DogName nickname = null;
  ...
}

In 7.22.0 and lower, the Dog class just has a nickname property of String:

public class Dog  {
  @JsonbProperty("nickname")
  protected String nickname;
  ...
}

Observations:

  • If I change the title of either of the field definitions, the issue is not there.
  • If I change the type of one of the fields to for example integer, the issue is not there.

Conclusion:

If there are two field definitions with the same type and the same title, the OpenAPI generator will generate code using non-existent classes for one of them.

openapi-generator version

Tested and breaks in: 7.23.0, 7.25.0, latest master

This is a regression: it works fine in 7.22.0

OpenAPI declaration file content or url

https://github.com/bvschaik/openapi-nonexistent-class-reproducer/blob/main/spec.json

Generation Details

Use any typed language (tested with Java, Kotlin):

java -jar openapi-generator.jar generate -g java -i https://raw.githubusercontent.com/bvschaik/openapi-nonexistent-class-reproducer/refs/heads/main/spec.json

Steps to reproduce
  1. Generate spec using command above
  2. Inspect the Cat and Dog classes - Dog has a property of type DogName, which doesn't exist
  3. Compile the code - fails
Related issues/PRs

Might be related to: https://github.com/OpenAPITools/openapi-generator/issues/24004

Suggest a fix

bvschaik avatar Aug 31 '26 12:08 bvschaik

Opened PR #25231 to address this issue by adding an \isModelNeeded\ guard and skipping enums in \InlineModelResolver.deduplicateComponents(), ensuring distinct primitive definitions sharing a title are preserved in \components/schemas\ so they unalias properly during generation.

SubhamAshok avatar Oct 12 '26 18:10 SubhamAshok