When using polymorphism and only the parent class is referenced in the API operations its subclasses aren't added to the model.
Jackson discovers the inheritance model with the annotations JsonTypeInfo and JsonSubTypes.
I think it would be useful if Swagger could pick up the classes listed in JsonSubTypes and add them to the model. I also think it would be nice if Swagger could pick up the discriminator property from JsonTypeInfo and add the parameter to the model as adding the property to the subclasses makes Jackson write the property twice.
Added support for subType declaration:
https://github.com/wordnik/swagger-core/commit/1e6d5dc7bedd799bdcf852a43a920b2f9f526355
This doesn't include @JsonTypeInfo as-is. Looking at supporting that but this is the work-around:
@ApiModel(value="my base model",
discriminator="type",
subTypes={WildAnimal.class, DomesticAnimal.class})
@fehguy Isnt it kind of redefining the runtime serialization relationships, just for documentation. Meaning if we're using jackson to serialize to json, the Animal class will have similar annotations
@JsonTypeInfo( use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type" )
@JsonSubTypes( { @Type( value = WildAnimal.class, name = "wild" ),
@Type( value = DomesticAnimal.class, name = "domestic" ) } )
abstract class Animal {
//...
public String name;
}
Now we end up with a problem to keep them in sync. Since you're still in the midst of implementing this, any way to avoid this?
That's the next step of this.
It seems not to be working if the property 'type' (for the example property="type") is not a bean property (only used by Jackson for transportation).
@fehguy Has this been fixed ? Does it work if type is not a bean property?
On 1.5.21 branch, It isn't working for me.
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = A.class, name = "a"),
@JsonSubTypes.Type(value = B.class, name = "b"),
})
public interface C {
String getId();
}
Generated swagger.json:-
"C" : {
"type" : "object",
"discriminator" : "type",
"properties" : {
"id" : {
"type" : "string"
}
}
}
property "type" is missing in properties as well as required(since it is discriminator) which is leading to wrong code generation. Any thoughts on this ?
Any update since @mukulb90 's comment? This thread seems only but it still doesn't seem to work, even on 2.0.8...
Please file a new ticket for further investigation.
Most helpful comment
@fehguy Has this been fixed ? Does it work if type is not a bean property?
On 1.5.21 branch, It isn't working for me.
Generated swagger.json:-
property "type" is missing in properties as well as required(since it is discriminator) which is leading to wrong code generation. Any thoughts on this ?