Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #6630
Closed
Open
Issue created Jun 11, 2020 by Administrator@rootContributor

[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?

Assignee
Assign to
Time tracking