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

Support JsonTypeInfo and JsonSubTypes annotations

Open mthyman opened this issue 13 years ago • 7 comments

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.

mthyman avatar Aug 09 '13 06:08 mthyman

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 avatar Aug 12 '13 21:08 fehguy

@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?

dilipkrish avatar Aug 13 '13 02:08 dilipkrish

That's the next step of this.

fehguy avatar Aug 13 '13 02:08 fehguy

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).

Martin-Luft avatar Mar 09 '16 16:03 Martin-Luft

@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 ?

mukulb90 avatar Aug 21 '18 18:08 mukulb90

Any update since @mukulb90 's comment? This thread seems only but it still doesn't seem to work, even on 2.0.8...

christophercolumbusdog avatar Jul 16 '19 14:07 christophercolumbusdog

Please file a new ticket for further investigation.

webron avatar Jul 16 '19 15:07 webron