我有一个具有以下功能的示例 SpringBoot API:
- 1 个控制器,可通过 GET 请求公开可调用的单个端点并返回自定义类(在我的示例中为 ContainerClass)
- ContainerClass 包含一个属性 List
- ParentClass 是一个抽象类,有 2 个子类:ChildA 和 ChildB
我尝试使用springdoc-openapi-maven-plugin从此 API 生成 OpenApi 合同。
在我的 pom.xml 中,我有以下元素:
- SpringBoot 版本:2.2.6
- org.springdoc:springdoc-openapi-ui:1.4.1
- org.springdoc:springdoc-openapi-maven-plugin:1.0
这是我从中生成模式的类。
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
public class ContainerClass {
@ArraySchema(
arraySchema = @Schema(discriminatorProperty = "classType"),
schema = @Schema(implementation = ParentClass.class)
)
public List<ParentClass> elements;
// + Getter/Setter
}
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "classType",
defaultImpl = ParentClass.class,
visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = ChildA.class, name = "CHILD_A"),
@JsonSubTypes.Type(value = ChildB.class, name = "CHILD_B")})
@Schema(
description = "Parent description",
discriminatorProperty = "classType",
discriminatorMapping = {
@DiscriminatorMapping(value = "CHILD_A", schema = ChildA.class),
@DiscriminatorMapping(value = "CHILD_B", schema = ChildB.class)
}
)
public abstract class ParentClass {
public String classType;
// + Getter/Setter
}
@io.swagger.v3.oas.annotations.media.Schema(description = " Child A", allOf = ParentClass.class)
public class ChildA extends ParentClass{
}
@io.swagger.v3.oas.annotations.media.Schema(description = " Child B", allOf = ParentClass.class)
public class ChildB extends ParentClass{
}
当我运行 springdoc-openapi-maven-plugin 时,我得到以下合同文件。
openapi: 3.0.1
info:
title: OpenAPI definition
version: v0
servers:
- url: http://localhost:8080
description: Generated server url
paths:
/container:
get:
tags:
- hello-controller
operationId: listElements
responses:
"200":
description: OK
content:
'*/*':
schema:
$ref: '#/components/schemas/ContainerClass'
components:
schemas:
ChildA:
type: object
description: ' Child A'
allOf:
- $ref: '#/components/schemas/ParentClass'
ChildB:
type: object
description: ' Child B'
allOf:
- $ref: '#/components/schemas/ParentClass'
ContainerClass:
type: object
properties:
elements:
type: array
description: array schema description
items:
oneOf:
- $ref: '#/components/schemas/ChildA'
- $ref: '#/components/schemas/ChildB'
ParentClass:
type: object
properties:
classType:
type: string
description: Parent description
discriminator:
propertyName: classType
mapping:
CHILD_A: '#/components/schemas/ChildA'
CHILD_B: '#/components/schemas/ChildB'
实际上,在我的上下文中,为了不对现有消费者进行任何重大更改,我需要ContainerClass模式中的items属性来包含ParentClass模式中包含的鉴别器部分,如下所示:
ContainerClass:
type: object
properties:
elements:
type: array
description: array schema description
items:
discriminator:
propertyName: classType
mapping:
CHILD_A: '#/components/schemas/ChildA'
CHILD_B: '#/components/schemas/ChildB'
oneOf:
- $ref: '#/components/schemas/ChildA'
- $ref: '#/components/schemas/ChildB'
当我尝试在注释中设置属性时,我无法做到这一点。当我调试io.swagger.v3.core.jackson.ModelResolver的代码时,我无法找到一种方法来做到这一点。到目前为止,我还没有找到对我有帮助的代码示例。
有没有办法让 ComposedSchema (在我的例子中包含在ContainerClass中的数组)具有由 springdoc-openapi-maven-plugin 执行生成的鉴别器部分?