I am in the process of taking a look into polymorphism for OpenAPI. More specifically I am in trying out the example with the Pet
, Cat
and Dog
found here using this portion of yaml:
components:
schemas:
Pet:
type: object
discriminator:
propertyName: petType
properties:
name:
type: string
petType:
type: string
required:
- name
- petType
Cat: ## "Cat" will be used as the discriminator value
description: A representation of a cat
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
huntingSkill:
type: string
description: The measured skill for hunting
enum:
- clueless
- lazy
- adventurous
- aggressive
required:
- huntingSkill
Dog: ## "Dog" will be used as the discriminator value
description: A representation of a dog
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
packSize:
type: integer
format: int32
description: the size of the pack the dog is from
default: 0
minimum: 0
required:
- packSize
Using this I am able to create the DTOs using the OpenAPI spring generator correctly and with a proper structure. Now all seem fine up to this step but when I serve the SwaggerUI using the Spring's dependencies the generated subclasses are not served as part of the schema. For instance, when I fetch the JSON definition of this I get the following:
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [{
"url": "http://localhost:8080",
"description": "Generated server url"
}],
"tags": [{
"name": "Test",
"description": "the Test API"
}],
"paths": {
"/test": {
"get": {
"tags": ["Test"],
"operationId": "testGet",
"responses": {
"200": {
"description": "A list of search results",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"required": ["name", "petType"],
"type": "object",
"properties": {
"name": {
"type": "string"
},
"petType": {
"type": "string"
}
},
"discriminator": {
"propertyName": "petType"
}
}
}
}
}
As we can see there is no Cat
or Dog
definition. This in turn makes consumers of this JSON definition to incorrectly generate their code.
My question is thus, what needs to be done in order to get this working properly. For reference I am using OpenAPI 3.0.1, SpringBoot 3.0 and the following swagger and springdoc dependencies:
implementation 'org.openapitools:jackson-databind-nullable:0.2.4'
implementation 'io.swagger.core.v3:swagger-annotations:2.2.7'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.0'