swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Missing model using multilevel inheritance

Open kcmoliveira opened this issue 3 years ago • 0 comments

Swagger Core Version: 2.2.12

I have a very simple REST API to explain the problem I'm having. The model looks like following: image When I go to swagger-ui page, the RubberMouseToy and BoneToy are missing from the schemas. image However, if I change the GetAnimalsResponse to return a list of Domestic instead, both RubberMouseToy and BoneToy appear in the schemas. But this time, Dog and Cat dissaper from the schemas and the return type in GetAnimalsResponse is Domestic, instead of oneOf. image Does anyone knows what's missing?

I'm using swagger-core version 2.2.1.

Here's the code

public class GetAnimalsResponse {
    private List<Animal> animals;

    public List<Animal> getAnimals() {
        return animals;
    }

    public void setAnimals(List<Animal> animals) {
        this.animals = animals;
    }
}

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

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Domestic.class, name = "Domestic"),
        @JsonSubTypes.Type(value = Dog.class, name = "Dog"),
        @JsonSubTypes.Type(value = Cat.class, name = "Cat")
})
public class Domestic extends Animal {
    private String name;

    private Toy toy;

    public Domestic() {}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Toy getToy() {
        return toy;
    }

    public void setToy(Toy toy) {
        this.toy = toy;
    }
}

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Toy.class, name = "Toy"),
        @JsonSubTypes.Type(value = BoneToy.class, name = "BoneToy"),
        @JsonSubTypes.Type(value = RubberMouseToy.class, name = "RubberMouseToy")
})
public abstract class Toy {
}

kcmoliveira avatar Aug 23 '22 14:08 kcmoliveira