Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Java] "allOf" does not support inheritance #9756

Closed
rgoers opened this issue Jun 13, 2021 · 13 comments
Closed

[BUG] [Java] "allOf" does not support inheritance #9756

rgoers opened this issue Jun 13, 2021 · 13 comments

Comments

@rgoers
Copy link

rgoers commented Jun 13, 2021

Description

I am trying to generate Java classes from an openapi schema that contains

OrganizationPage:
  type: object
  allOf:
    - $ref: '#/components/schemas/BasePage'
  properties:
    data:
      type: array
      items:
        $ref: '#/components/schemas/Organization'

this ends up generating a class named OrganizationPage with

public class OrganizationPage  implements Serializable {

but I require

public class OrganizationPage  extends BasePage implements Serializable {

the documentation seems to indicate that it is required that the definition of BasePage be modified to add a discriminator field. I am unable to do that as that file is in a different project that is shared by different projects, some of which use older versions of openapi-generator and do not populate a discriminator field. Furthermore, a discriminator field is never required as the base class is never referenced in the API, only the subclass is, so serialization/deserialization doesn't require a discriminator. However, there are utility methods that operate on the base class so all instances must extend the class.

openapi-generator version

openapi-generator-mave-plugin 5.1.0

OpenAPI declaration file content or url
    OrganizationPage:
      type: object
      allOf:
        - $ref: '#/components/schemas/BasePage'
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/Organization'

base-page.yml contains

description: Page resource object for paged REST resource response. Extend this object to return a strongly-typed payload.
type: object
properties:
  totalCount:
    description: The total number of resources at the requested location taking query parameters into account
    readOnly: true
    type: integer
    format: int64
  first:
    description: Link to the first page in the resource collection
    readOnly: true
    type: string
    format: uri
  prev:
    description: Link to the first page in the resource collection
    readOnly: true
    type: string
    format: uri
  next:
    description: Link to the first page in the resource collection
    readOnly: true
    type: string
    format: uri
  last:
    description: Link to the first page in the resource collection
    readOnly: true
    type: string
    format: uri
required:
  - totalCount
Generation Details

mvn clean install produces:

[deprecated] inheritance without use of 'discriminator.propertyName' has been deprecated in the 5.x release. Composed schema name: null. Title: null

The above line is emitted a few times during the build but it isn't clear if it is associated with this issue or not since it doesn't identify the file, line, or object it is referencing.

Related issues/PRs

Both #3100 and #3172 have similarities to this issue.

Suggest a fix

The only fix I can think of is to add an attribute to the definition of the object to direct it to use inheritance instead of composition.

@rgoers
Copy link
Author

rgoers commented Jul 15, 2021

I have tried to modify my schema to generate the extends but nothing I am doing is working my latest incarnation looks like:

common/base-page.yml

`description: Page resource object for paged REST resource response. Extend this object to return a strongly-typed payload.
type: object
discriminator:
  propertyName: pageType
properties:
  totalCount:
    description: The total number of resources at the requested location taking query parameters into account
    readOnly: true
    type: integer
    format: int64
  first:
    description: Link to the first page in the resource collection
    readOnly: true
    type: string
    format: uri
  prev:
    description: Link to the first page in the resource collection
    readOnly: true
    type: string
    format: uri
  next:
    description: Link to the first page in the resource collection
    readOnly: true
    type: string
    format: uri
  last:
    description: Link to the first page in the resource collection
    readOnly: true
    type: string
    format: uri
  pageType:
    type: string
required:
  - totalCount
  - pageType

and the class that extends it has:

components:
  schemas:
    BasePage:
      $ref: './common/base-page.yml'
    Organization:
      $ref: './organization.yml'
    OrganizationPage:
      type: object
      allOf:
        - $ref: '#/components/schemas/BasePage'
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/Organization'

This generates OrganizationPage.java without an "extends BasePage". For some reason it also isn't including pageType as an attribute in the generated class.

Can someone please explain what I am doing incorrectly?

@argenstijn
Copy link

Can this please be fixed? Because no there is no inheritance. Also check what happens if you have multiple discriminator or multiple allOf

@argenstijn
Copy link

Any idea when to fix this?

@tofi86
Copy link

tofi86 commented Jan 31, 2022

Refs #5726.

I'm also interested in a fix for this issue.

@SENIAN
Copy link

SENIAN commented Feb 11, 2022

Any idea for a fix? This is not real inheritance. I cannot use polymorphism using this allOf generation

@zookotic
Copy link

Our team needs this fix too :(

@lostiniceland
Copy link
Contributor

Bug is also present in 6.0.0-beta

@epabst
Copy link

epabst commented Nov 19, 2022

This is a huge issue. This is very important.

@epabst
Copy link

epabst commented Nov 19, 2022

After I got the Pet model working that @marcdejonge provided in #9615 (comment), I figured out a workaround for my own issue. I had separate yaml files for my schema model classes, and by moving them into the same file as the endpoints under this, it worked for me!!!

components:
  schemas:

Note, I also applied this fix to avoid the extra AllOf classes: #3100 (comment)

@wing328
Copy link
Member

wing328 commented Dec 20, 2022

Hi all, I've filed #14172 to allow using $ref as parent in allOf with a new option called "openapi-normalizer".

Please give it a try as follows:

git clone --depth 1 -b allof-parent-type https://github.com/OpenAPITools/openapi-generator/
cd openapi-generator
mvn clean package -DskipTests -Dmaven.javadoc.skip=true
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i /Users/williamcheng/Code/openapi-generator7/modules/openapi-generator/src/test/resources/3_0/allOf_extension_parent.yaml -o /tmp/java-okhttp/ --additional-properties hideGenerationTimestamp="true" --openapi-normalizer REF_AS_PARENT_IN_ALLOF=true

@BillyBolton
Copy link

@wing328 Is this code merged with the most up-to-date version?

@wing328
Copy link
Member

wing328 commented Jun 30, 2023

the fix has been merged into master. please give latest stable version v6.6.0 a try

@wing328 wing328 closed this as completed Jun 30, 2023
@forwardmeasure
Copy link

All, this seems to have regressed in Version 7.2.0. The JAXRS generator that used to generate code correctly up until version 7.0.1 no longer does so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants