Can Swagger JaxRS do ApiModel Inheritance with Discriminator?

I tried with current Swagger JaxRs 1.0 master and devel_2.0 branch for Swagger 2.0.

@ApiModel(value = "Animal", 
  subTypes = {Dog.class, Lion.class}, 
  discriminator = "type")
public class Animal {

    @ApiModelProperty(value = "the discriminator field.")
    private String type;

      

And here is one of the subclasses,

@ApiModel(value = "Lion", parent = Animal.class)
public class Lion {

@ApiModelProperty(value = "the discriminator field.")
private String type;

      

I didn't find many examples of what to expect, but here is the output in my current Swagger 2.0 swagger.json project.

   "definitions":{
      "Animal":{
         "properties":{
            "type":{
               "type":"string",
               "description":"the discriminator field."
            }
         },
         "discriminator":"type"
      },

      

No signs of a Dog or Lion object under the definitions. Nothing in the request object. I'm not sure how it will look if it works, but let me know if you know how it should work.

All the code is here if you want to see the full context.

https://github.com/javatestcase/RestEasy/tree/RestEasyVersion2

+3


source to share


1 answer


Your examples helped me a lot, so I thought I should help you in return because I got it now!

You need to tell about serialization / deserialization, how to link the implementation:



@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 = Lion.class, name = "Lion"),
    @JsonSubTypes.Type(value = Dog.class, name = "Dog")
})
@ApiModel(value = "Animal", subTypes = {Dog.class, Lion.class}, discriminator = "type")
public class Animal {

      

+3


source







All Articles