Skip to content

How to describe polymorphic endpoint #2340

Closed
@raderio

Description

@raderio

We use Spring+Jackson(Java). And in our API we can send different object to 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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions