Anonymous
Swagger генерируется с помощью allOf и oneOf, что приводит к циклическому документированию API.
Сообщение
Anonymous » 14 окт 2024, 15:00
Я использую версию Spring Boot 3.2.7. У меня есть конечная точка отдыха, которая принимает RequestDTO в качестве тела запроса.
определяется как запечатанный интерфейс, как показано ниже для иерархии.
Код: Выделить всё
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = ARequestDTO.class, name = "A")})
@Schema(oneOf = {ARequestDTO.class})
public sealed interface RequestDTO permits ARequestDTO {
String type();
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "subType")
@JsonSubTypes({
@JsonSubTypes.Type(value = DRequestDTO.class, name = "D"),
@JsonSubTypes.Type(value = CRequestDTO.class, name = "C")
})
@Schema(oneOf = {CRequestDTO.class, DRequestDTO.class})
sealed interface ARequestDTO extends RequestDTO permits CRequestDTO, DRequestDTO {
String type();
String subType();
}
record CRequestDTO(String type, String subType) implements ARequestDTO {}
record DRequestDTO(String type, String userId, String subType) implements ARequestDTO {}
При этом swagger генерирует циклический API-документ, как показано ниже:
Код: Выделить всё
{
"paths": {
"/private/api/v1/test/as": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/RequestDTO"
}
]
}
}
},
"required": true
}
}
}
},
"components": {
"schemas": {
"ARequestDTO": {
"required": [
"subType"
],
"type": "object",
"discriminator": {
"propertyName": "subType"
},
"allOf": [
{
"$ref": "#/components/schemas/RequestDTO"
},
{
"type": "object",
"properties": {
"subType": {
"type": "string"
}
}
}
],
"oneOf": [
{
"$ref": "#/components/schemas/CRequestDTO"
},
{
"$ref": "#/components/schemas/DRequestDTO"
}
]
},
"CRequestDTO": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/ARequestDTO"
},
{
"type": "object",
"properties": {
"type": {
"type": "string"
},
"subType": {
"type": "string"
}
}
}
]
},
"DRequestDTO": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/ARequestDTO"
},
{
"type": "object",
"properties": {
"type": {
"type": "string"
},
"userId": {
"type": "string"
},
"subType": {
"type": "string"
}
}
}
]
},
"RequestDTO": {
"required": [
"type"
],
"type": "object",
"properties": {
"type": {
"type": "string"
}
},
"discriminator": {
"propertyName": "type"
},
"oneOf": [
{
"$ref": "#/components/schemas/ARequestDTO"
}
]
}
}
}
}
Не знаю, как добавляется allOf. Помогите, пожалуйста, разобраться.
Подробнее здесь:
https://stackoverflow.com/questions/790 ... ic-api-doc
1728907252
Anonymous
Я использую версию Spring Boot 3.2.7. У меня есть конечная точка отдыха, которая принимает RequestDTO в качестве тела запроса. [code]RequestDTO[/code] определяется как запечатанный интерфейс, как показано ниже для иерархии. [code]@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonSubTypes({@JsonSubTypes.Type(value = ARequestDTO.class, name = "A")}) @Schema(oneOf = {ARequestDTO.class}) public sealed interface RequestDTO permits ARequestDTO { String type(); } @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "subType") @JsonSubTypes({ @JsonSubTypes.Type(value = DRequestDTO.class, name = "D"), @JsonSubTypes.Type(value = CRequestDTO.class, name = "C") }) @Schema(oneOf = {CRequestDTO.class, DRequestDTO.class}) sealed interface ARequestDTO extends RequestDTO permits CRequestDTO, DRequestDTO { String type(); String subType(); } record CRequestDTO(String type, String subType) implements ARequestDTO {} record DRequestDTO(String type, String userId, String subType) implements ARequestDTO {} [/code] При этом swagger генерирует циклический API-документ, как показано ниже: [code]{ "paths": { "/private/api/v1/test/as": { "post": { "requestBody": { "content": { "application/json": { "schema": { "oneOf": [ { "$ref": "#/components/schemas/RequestDTO" } ] } } }, "required": true } } } }, "components": { "schemas": { "ARequestDTO": { "required": [ "subType" ], "type": "object", "discriminator": { "propertyName": "subType" }, "allOf": [ { "$ref": "#/components/schemas/RequestDTO" }, { "type": "object", "properties": { "subType": { "type": "string" } } } ], "oneOf": [ { "$ref": "#/components/schemas/CRequestDTO" }, { "$ref": "#/components/schemas/DRequestDTO" } ] }, "CRequestDTO": { "type": "object", "allOf": [ { "$ref": "#/components/schemas/ARequestDTO" }, { "type": "object", "properties": { "type": { "type": "string" }, "subType": { "type": "string" } } } ] }, "DRequestDTO": { "type": "object", "allOf": [ { "$ref": "#/components/schemas/ARequestDTO" }, { "type": "object", "properties": { "type": { "type": "string" }, "userId": { "type": "string" }, "subType": { "type": "string" } } } ] }, "RequestDTO": { "required": [ "type" ], "type": "object", "properties": { "type": { "type": "string" } }, "discriminator": { "propertyName": "type" }, "oneOf": [ { "$ref": "#/components/schemas/ARequestDTO" } ] } } } } [/code] Не знаю, как добавляется allOf. Помогите, пожалуйста, разобраться. Подробнее здесь: [url]https://stackoverflow.com/questions/79086080/swagger-getting-generated-with-alloff-and-oneoff-leading-to-cyclic-api-doc[/url]