OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] inlined enum are duplicated between parent and child class

Open
#6,630 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.