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][SPRING] Model Inheritance over multiple layers with REF_AS_PARENT_IN_ALLOF=true, allOf and required parameters produces compilation error #15148

Closed
5 tasks done
kzander91 opened this issue Apr 6, 2023 · 1 comment

Comments

@kzander91
Copy link

kzander91 commented Apr 6, 2023

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

I have an inheritance hierarchy four levels deep: Child inherits from Parent which inherits from GrandParent which inherits from GreatGrandParent.
Each type has a required property (see full spec below).

This generates the following model classes, with constructors for the required parameters. The constructor of Child delegates to the super constructor with parameters in incorrect order, causing compiler errors if the types don't match and not causing compiler errors, if the types happen to be compatible:

public class GreatGrandParent {
  private Integer greatGrandParentProp;

  public GreatGrandParent(Integer greatGrandParentProp) {
    this.greatGrandParentProp = greatGrandParentProp;
  }
//...
}
public class GrandParent extends GreatGrandParent {
  private String grandParentProp;

  public GrandParent(String grandParentProp, Integer greatGrandParentProp) {
    super(greatGrandParentProp);
    this.grandParentProp = grandParentProp;
  }
//...
}
public class Parent extends GrandParent {
  private Boolean parentProp;

  public Parent(Boolean parentProp, Integer greatGrandParentProp, String grandParentProp) {
    super(grandParentProp, greatGrandParentProp);
    this.parentProp = parentProp;
  }
//...
}
public class Child extends Parent {
  private String childProp;

  public Child(String childProp, Integer greatGrandParentProp, String grandParentProp, Boolean parentProp) {
    super(parentProp, grandParentProp, greatGrandParentProp); // <-- Compiler error here, param order should be parentProp, greatGrandParentProp, grandParentProp
    this.childProp = childProp;
  }
//...
}
openapi-generator version

6.5.0 and master (ba2c42e)

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Test
  version: 1.0.0
paths:
  /test:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Child'
      responses:
        200:
          description: success

components:
  schemas:
    GreatGrandParent:
      type: object
      properties:
        greatGrandParentProp:
          type: integer
      required:
        - greatGrandParentProp

    GrandParent:
      allOf:
        - $ref: '#/components/schemas/GreatGrandParent'
        - type: object
          properties:
            grandParentProp:
              type: string
      required:
        - grandParentProp

    Parent:
      allOf:
        - $ref: '#/components/schemas/GrandParent'
        - type: object
          properties:
            parentProp:
              type: boolean
      required:
        - parentProp

    Child:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object
          properties:
            childProp:
              type: string
      required:
        - childProp
Generation Details
java -jar openapi-generator-cli.jar generate -g spring -i spec.yaml -o out --openapi-normalizer=REF_AS_PARENT_IN_ALLOF=true
Steps to reproduce

Generate code from above spec with spring generator and REF_AS_PARENT_IN_ALLOF=true.

Related issues/PRs

#14934
#14941
#9756
#14172

Suggest a fix

Ensure that parameters are passed into parent constructor in the correct order.

Workaround

--additional-properties=generatedConstructorWithRequiredArgs=false can be used to skip generating the erroneous constructors.

@kzander91
Copy link
Author

Fixed by #15827.

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

No branches or pull requests

1 participant