How to describe polymorphic endpoint in Swagger?

Viewed 2606

We use Spring+Jackson(Java). And in our API we can send a different object to the same endpoint. For example

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME, 
    include = As.PROPERTY, 
    property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Dog.class, name = "dog"),
    @JsonSubTypes.Type(value = Cat.class, name = "cat")
})
public static class Animal {
}

@JsonTypeName("dog")
public static class Dog extends Animal {
    public double barkVolume;
}

@JsonTypeName("cat")
public static class Cat extends Animal {
    boolean likesCream;
    public int lives;
}

@Controller
public class MyController {

    //REST service
    @RequestMapping( value = "test")
    public  @ResponseBody String save(@RequestBody  Animal animal){
        System.out.println(animal.getClass());
        return success;
    }
}

Sending to /test

{
    "type": "dog",
    "barkVolume": 23.3
}

will show Dog.class

Sending to /test

{
    "type": "cat",
    "likesCream": true,
    "lives": 42
}

will show Cat.class

How to describe polymorphic endpoint in Swagger?

1 Answers
Related