OpenAPITools / OpenAPITools/openapi-generator
[BUG] Nested fields can result in name conflict resulting in invalid code
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 (example)?
- 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
We are experience invalid code being generated from the attached open spec file (minimal example file, not our real). The problems seems to be related that the name is created based only on the outer most object, not the nested one. When nested multiple levels deep, each new nesting should maybe be added to the class name?
openapi-generator version
5.1.1
OpenAPI declaration file content or url
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Failing nested openspec",
"description": "Open api generator does not handle this"
},
"paths": {
"/failing": {
"get": {
"summary": "Example failing",
"responses": {
"200": {
"description": "Response body example",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/failing"
}
}
}
}
},
"operationId": "bad2a8e2-da90-4b39-9b52-4b2192bfdd2a"
}
}
},
"components": {
"schemas": {
"failing": {
"title": "failingData",
"properties": {
"result": {
"type": "object",
"properties": {
"clientRoles": {
"type": "array",
"items": {
"type": "object",
"properties": {
"client": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
},
}
}
}
}
},
}
},
"type": "object"
}
}
}
}
Generation Details
Tried using both kotlin-spring, kotling and java generator, all with the same result.
Using the following config.yml file.
apiPackage: "com.failling"
basePackage: "com.failling"
modelPackage: "com.failling"
apiSuffix: Adapter
modelNameSuffix: Dto
serializationLibrary: jackson
gradleBuildFile: false
enumPropertyNaming: original
interfaceOnly: false
Steps to reproduce
Generate code using open-api-generator.
Verify that FailingResultClientDto points to FailingResultClientRolesDto, which again points to FailingResultClientDto.
Related issues/PRs
Suggest a fix
One possible solution would be to keep all fields, not just top level, producing the classname FailinResultClientRolesClientRolesDto
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 with the supplied OpenAPI declaration and reproduce generation for kotlin-spring, Kotlin, and Java using the shown configuration. Compare the generated FailingResultClientDto and FailingResultClientRolesDto references; done means nested models receive distinct names and the generated code is valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100