Subclasses missing in generated @JsonSubTypes
Closed this issue · 2 comments
sebplorenz commented
Hi,
I have the following openapi spec
openapi: 3.0.0
info:
version: "4.0"
title: Pet Service
paths:
/animal:
post:
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Animal"
required: true
responses:
200:
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Animal"
components:
schemas:
Animal:
type: object
properties:
name:
type: string
discriminator:
propertyName: dtype
Mammal:
allOf:
- $ref: '#/components/schemas/Animal'
Nonmammal:
allOf:
- $ref: '#/components/schemas/Animal'
Fish:
properties:
saltwater:
type: boolean
allOf:
- $ref: '#/components/schemas/Nonmammal'
Bird:
properties:
altitude:
type: integer
allOf:
- $ref: '#/components/schemas/Nonmammal'
The resulting Animal class is only annotated with the directed descendants Mammal and Nonmammal.
@com.fasterxml.jackson.annotation.JsonSubTypes({
@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Mammal.class, name = "Mammal"),
@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Nonmammal.class, name = "Nonmammal"),
})
public abstract class Animal {
...
}
It is missing Bird and Fish. This will cause an error if Bird or Fish are posted to the server.
The correct JsonSubTypes annotation would be:
@com.fasterxml.jackson.annotation.JsonSubTypes({
@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Mammal.class, name = "Mammal"),
@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Nonmammal.class, name = "Nonmammal"),
@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Fish.class, name = "Fish"),
@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Bird.class, name = "Bird")
})
public abstract class Animal {
...
}
sschnabe commented
I cannot reproduce this, see 5996980.
May the discriminator.mapping is missing in your spec?
sebplorenz commented
That was it! The missing discriminator.mapping was the problem. Thank you, Stephan.