- 1 контроллер, который предоставляет одну конечную точку, вызываемую с помощью запроса GET, и который возвращает собственный класс. (ContainerClass в моем примере)
- ContainerClass содержит список свойств
- ParentClass — это абстрактный класс, имеющий 2 подкласса: ChildA и ChildB
В моем 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 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{
}
Код: Выделить всё
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:
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'
И до сих пор я не нашел пример кода, который мне поможет.
Есть ли способ, чтобы CompusedSchema (в моем случае массив, содержащийся в ContainerClass) имел дисциминатор часть, созданная при выполнении springdoc-openapi-maven-plugin?
Подробнее здесь: https://stackoverflow.com/questions/626 ... a-contract
Мобильная версия