OpenAPITools / OpenAPITools/openapi-generator
[BUG] Generated code references non-existent classes on "duplicate" property definitions
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
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
- Generate spec using command above
- Inspect the Cat and Dog classes - Dog has a property of type DogName, which doesn't exist
- Compile the code - fails
Related issues/PRs
Might be related to: https://github.com/OpenAPITools/openapi-generator/issues/24004
Suggest a fix
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the documented generator command with the linked spec.json for Java, then inspect the generated Cat and Dog classes and compare output with version 7.22.0. Trace how duplicate same-type, same-title property definitions are resolved. Done means the generated Dog code references an existing type or String and compiles successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100