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
  • #5097
Closed
Open
Issue created Jan 23, 2020 by Administrator@rootContributor

[BUG] [Java] inheritance is generated without discriminator field

Created by: vitaliysapounov

Description

We see that inheritance (instead of composition) is generated where, as we think, it should not.

Namely, we have the following model file:

openapi: "3.0.2"

info:
  version: "1.0.0"
  title: Test

  # We only care about schemas, but this prevents codegen error: "attribute paths is missing"
paths:
  /dummy:
    get:
      responses:
        '200':
          description: OK

components:
  schemas:

    PreventInheritance:
      description: We declare dummy dependency on this empty class to prevent inheritance

    Base:
      properties:
        base1:
          type: integer
        base2:
          type: string

    Inheritance:
      allOf:
        - $ref: "#/components/schemas/Base"
        - properties:
            # doses
            Inheritance1:
              type: integer
            Inheritance2:
              type: string

    NoInheritance:
      allOf:
        - $ref: "#/components/schemas/Base"
        - $ref: "#/components/schemas/PreventInheritance"
        - properties:
            NoInheritance1:
              type: integer
            NoInheritance2:
              type: string

The following two classes are of interest:

  1. Basically, Inheritance class is declared as having all the properties of Base and some additional. Note that there is no discriminator defined. Thus, since there is no discriminator, our understanding that composition should be used, not inheritance. In the generated Java class we see that inheritance is generated, however:
public class Inheritance extends Base implements Serializable {

OBSERVED: inheritance is generated EXPECTED: composition should be used, as there is no discriminator field (?)

  1. To prevent generating of inheritance, we used a hack: in NoInheritance class we add not only Base but also empty PreventInheritance. It seems that in this case, the generator chooses to use composition (why?):
public class NoInheritance implements Serializable {

OBSERVED: inheritance is not generated EXPECTED: ? (we are confused what should be used, composition or inheritance, the spec is unclear in that regard)

openapi-generator version

Gradle plugin org.openapitools:openapi-generator-gradle-plugin:4.2.2 (latest as of this writing)

OpenAPI declaration file content or url

Please see above

Command line used for generation

./gradlew build

Steps to reproduce
  1. Unpack the attached Gradle project
  2. Run ./gradlew build
Related issues/PRs
Suggest a fix

We are not sure how/why inheritance vs composition should be selected, as per the spec. But, the current behavior described above seems to be very confusing (the generator seems to choose inheritance in one case and composition in another case, for no clear reason). test_openapi_inheritance.zip

Assignee
Assign to
Time tracking