Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JsonTypeInfo and JsonSubTypes annotations #280

Closed
mthyman opened this issue Aug 9, 2013 · 7 comments
Closed

Support JsonTypeInfo and JsonSubTypes annotations #280

mthyman opened this issue Aug 9, 2013 · 7 comments

Comments

@mthyman
Copy link

mthyman commented Aug 9, 2013

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.

@fehguy
Copy link
Contributor

fehguy commented Aug 12, 2013

Added support for subType declaration:

1e6d5dc

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

@dilipkrish
Copy link
Contributor

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

@fehguy
Copy link
Contributor

fehguy commented Aug 13, 2013

That's the next step of this.

@Martin-Luft
Copy link

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

@mukulb90
Copy link

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

@christophercolumbusdog
Copy link

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

@webron
Copy link
Contributor

webron commented Jul 16, 2019

Please file a new ticket for further investigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants