OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java] inlined enum are duplicated between parent and child class
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
Here is a very small file reproducing the issue:
{
"components": {
"schemas": {
"Child1": {
"allOf": [
{
"$ref": "#/components/schemas/Parent"
},
{
"properties": {
"name": {
"type": "string"
},
"type": {
"enum": [
"DISC1",
"DISC2"
],
"type": "string"
}
},
"type": "object"
}
],
"required": [
"name",
"type"
],
"type": "object"
},
"Child2": {
"allOf": [
{
"$ref": "#/components/schemas/Parent"
},
{
"properties": {
"name": {
"type": "string"
},
"type": {
"enum": [
"DISC1",
"DISC2"
],
"type": "string"
}
},
"type": "object"
}
],
"required": [
"name",
"type"
],
"type": "object"
},
"Parent": {
"discriminator": {
"mapping": {
"DISC1": "#/components/schemas/Child1",
"DISC2": "#/components/schemas/Child2"
},
"propertyName": "type"
},
"properties": {
"type": {
"enum": [
"DISC1",
"DISC2"
],
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
}
}
},
"info": {
"title": "Issue report",
"version": "1.0.0"
},
"openapi": "3.0.1",
"paths": {}
}
I generated the models for this file, using openapi-generator-cli.jar version 4.3.1, for the Java language.
java -jar openapi-generator-cli.jar generate -i openapi3.json -g java
Expected Output
the enum used in the three classes should be declared once, in the Parent class
Actual Output
the enum used in the three classes is declared three times, once in each class, making the build fail because getType in Child1 or Child2 do not return the same class as declared in Parent
Fix suggestion
I went in debugging mode. The issue seems to come from the org.openapitools.codegen.languages.AbstractJavaCodegen#reconcileInlineEnums. In order to check if the enum in the child class is "the same" as in the parent, this method uses the equals method between the two CodegenProperty. The two enums are not strictly equal, because one has isDiscriminator = true, the other has isDiscriminator = false
So the check fails, the method thinks that the two enums are not same, and keeps the enums in each class.
In io.swagger generator, they only compare some fields: https://github.com/swagger-api/swagger-codegen-generators/blob/92b71d309d7da602f41590ab5ed668f2f89d5d7b/src/main/java/io/swagger/codegen/v3/generators/java/AbstractJavaCodegen.java#L1282
Could we do the same in this plugin?
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 org.openapitools.codegen.languages.AbstractJavaCodegen#reconcileInlineEnums and reproduce the issue using openapi3.json with the Java generator command shown. Compare its enum-property equality check with the linked Swagger Generator implementation. Done means the generated Parent, Child1, and Child2 models share one enum declaration and compile 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
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100