Swagger генерируется с помощью allOf и oneOf, что приводит к циклическому документированию API.JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Swagger генерируется с помощью allOf и oneOf, что приводит к циклическому документированию API.

Сообщение Anonymous »

Я использую версию Spring Boot 3.2.7. У меня есть конечная точка отдыха, которая принимает RequestDTO в качестве тела запроса.

Код: Выделить всё

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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»