0

I have created a very simple demo project with one controller endpoint:

  • Spring Boot 3.5.6
  • Springdoc 2.8.13

This was working much better in Spring Boot 2.7.x

Full project here: https://github.com/humawork/swagger-spring-boot-demo

@RestController
@RequestMapping("/vehicle")
class VehicleController {

    @PostMapping("")
    fun create(
        @RequestBody body: VehicleCreateInput,
    ) {

    }
}

With some simple input body classes:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type"
)
@JsonSubTypes(
    JsonSubTypes.Type(value = CarCreateInput::class, name = "car"),
    JsonSubTypes.Type(value = BikeCreateInput::class, name = "bike"),
)
sealed class VehicleCreateInput(
    val id: UUID,
    val name: String,
)

class BikeCreateInput(
    id: UUID,
    name: String,
) : VehicleCreateInput(id, name)

class CarCreateInput(
    id: UUID,
    name: String,
) : VehicleCreateInput(id, name)

The swagger result looks like this:

enter image description here

Why does it say only (object, object)?

OpenAPI spec that is generated looks like this:

{
  "openapi": "3.1.0",
  "info": {
    "title": "OpenAPI definition",
    "version": "v0"
  },
  "servers": [
    {
      "url": "http://localhost:8080",
      "description": "Generated server url"
    }
  ],
  "paths": {
    "/vehicle": {
      "post": {
        "tags": [
          "vehicle-controller"
        ],
        "operationId": "create",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "oneOf": [
                  {
                    "$ref": "#/components/schemas/BikeCreateInput"
                  },
                  {
                    "$ref": "#/components/schemas/CarCreateInput"
                  }
                ]
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "BikeCreateInput": {
        "allOf": [
          {
            "$ref": "#/components/schemas/VehicleCreateInput"
          }
        ],
        "required": [
          "id",
          "name"
        ]
      },
      "CarCreateInput": {
        "allOf": [
          {
            "$ref": "#/components/schemas/VehicleCreateInput"
          }
        ],
        "required": [
          "id",
          "name"
        ]
      },
      "VehicleCreateInput": {
        "type": "object",
        "discriminator": {
          "propertyName": "type"
        },
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "type": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "name",
          "type"
        ]
      }
    }
  }
}

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.