Swagger/OpenApi 3.x中使用allOf扩展BaseClass时,组合与继承的区别

3

Swagger/openAPI无法生成具有扩展ParentClass签名的定义的ChildClass。

我在我的Spring Gradle项目中使用“org.openapi.generator”版本“6.2.1”。

以下是我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 片段

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

期望结果 应该具有以下定义的 ChildClass

public class ParentClass {

@JsonProperty("parentProperty")
private String parentProperty;

}

public class ChildClass extends ParentClass {

@JsonProperty("childProperty")
private String childProperty;

}

然而生成的结果是一个扁平的ChildClass,其中包含了ParentClass的合并属性,如下所示:

public class ChildClass {

@JsonProperty("childProperty")
private String childProperty;

@JsonProperty("parentProperty")
private String parentProperty;

}

这个子类拥有父类的所有属性(组合),但是两个类之间的关系丢失了,这破坏了代码。我该如何达到预期结果?
1个回答

0

您需要定义鉴别器以实现继承,因为该映射被Jackson反序列化器用于相应地反序列化对象。

例如:

`        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`

没有区分器,它将会扩散属性:

          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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接