2

Swagger/openAPI is not generating ChildClass with definition having extends ParentClass signature.

I am using "org.openapi.generator" version "6.2.1" in my Spring Gradle project.

Following is the configuration in my build.gradle

    generatorName = "spring"
    inputSpec = ${rootDir}/open-api/openapi.json
    outputDir = file("${buildDir}/open-api/")
    modelPackage = "com.example.dto"
    configOptions = [
            dateLibrary: "java.util.Date", // define our own date classes instead of using the standard ones
            hideGenerationTimestamp: "true"
    ]

`

openapi.json snippet

"components": {
 "schemas": {
"ParentClass": {
        "type": "object",
        "properties": {
        "parentProperty": {
        "type": "string"            
             }
    }
},
"ChildClass": {
    "allOf": [
        {       
           "$ref": "#/components/schemas/ParentClass"
        },
            {
            "type": "object",
            "properties": {
            "childProperty": {
                "type": "string"
             }
             }
        }
    ]
}
}
}

expected Result should have ChildClass with following definition

public class ParentClass {

@JsonProperty("parentProperty")
private String parentProperty;

}

public class ChildClass extends ParentClass {

@JsonProperty("childProperty")
private String childProperty;

}

However generated result is flat ChildClass with merged properties of ParentClass as follows:

public class ChildClass {

@JsonProperty("childProperty")
private String childProperty;

@JsonProperty("parentProperty")
private String parentProperty;

}

This child class have all properties of parent class (composition) but the relationship between both the classes is lost and breaking code. How can I achieve the expected Result.

Gamzy joy
  • 21
  • 3

1 Answers1

0

You will need to define discriminator for achieving inheritance. because that mapping is used by jackson deserialiser to deserialise object accordingly.

Example:

`        components:
            schemas:
              Pet:
                type: object
                required:
                  - pet_type
                properties:
                  pet_type:
                    type: string
                discriminator:
                  propertyName: pet_type
              Dog:     # "Dog" is a value for the pet_type property (the discriminator value)
                allOf: # Combines the main `Pet` schema with `Dog`-specific properties 
                  - $ref: '#/components/schemas/Pet'
                  - type: object
                    # all other properties specific to a `Dog`
                    properties:
                      bark:
                        type: boolean
                      breed:
                        type: string
                        enum: [Dingo, Husky, Retriever, Shepherd]
              Cat:     # "Cat" is a value for the pet_type property (the discriminator value)
                allOf: # Combines the main `Pet` schema with `Cat`-specific properties 
                  - $ref: '#/components/schemas/Pet'
                  - type: object
                    # all other properties specific to a `Cat`
                    properties:
                      hunts:
                        type: boolean
                      age:
                        type: integer`

Without discriminator, It will spread properties:

          components:
            schemas:
              BasicErrorModel:
                type: object
                required:
                  - message
                  - code
                properties:
                  message:
                    type: string
                  code:
                    type: integer
                    minimum: 100
                    maximum: 600
              ExtendedErrorModel:
                allOf:     # Combines the BasicErrorModel and the inline model
                  - $ref: '#/components/schemas/BasicErrorModel'
                  - type: object
                    required:
                      - rootCause
                    properties:
                      rootCause:
                        type: string
Awais Latif
  • 101
  • 1
  • 7