Skip to content

Good documentation on applying swagger inheritance in JAX-RS endpoints. #851

@erdebee

Description

@erdebee

Initially I expected swagger to pass class binding information to the Jackson deserializer, since swagger allows for annotating all required inheritance information to be supplied. It took me quite a while to figure out that I needed to add @JsonTypeInfo annotations...

It would have saved me days of my time if there would at least have been an example for polymorphism in the PetStore examples, or anything of sorts. Judging from the amount of unanswered questions on stackoverflow, and lack of examples/implementations I could find online, im not the only one.

Is it an idea to add some basic inheritance to the petstore example?

Suggestion for the petstore pet model:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME, // Were binding by providing a name
    include = JsonTypeInfo.As.PROPERTY, // The name is provided in a property
    property = "type", // Property name is type
    visible = true // Retain the value of type after deserialisation
)
@JsonSubTypes({//Below, we define the names and the binding classes.
    @JsonSubTypes.Type(value = CatPet.class, name = "Cat"),
    @JsonSubTypes.Type(value = DogPet.class, name = "Dog")
})
@ApiModel(value = "Pet", subTypes = {DogPet.class, CatPet.class}, discriminator = "type")
public abstract class Pet {

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions