[BUG][Java] inlined enum are duplicated between parent and child class
Created by: aromanet42
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?