Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Swagger JaxRS do ApiModel Inheritance with discriminator?

I've tried with the Swagger JaxRs current master 1.0, and the 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 sub classes,

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

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

I haven't found any many examples of what to expect, but here is the output in my current Swagger 2.0 projects swagger.json file.

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

No sign of the Dog or Lion object under definitions. Nothing in the request object. I'm not sure what this would look like if it worked, 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

like image 315
javatestcase Avatar asked Dec 23 '14 07:12

javatestcase


1 Answers

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

You need to tell the serialisation/deserialisation how to bind 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 {
like image 120
RoyB Avatar answered Nov 06 '22 05:11

RoyB