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

How to describe polymorphic endpoint #2340

Closed
raderio opened this issue Jul 28, 2017 · 9 comments
Closed

How to describe polymorphic endpoint #2340

raderio opened this issue Jul 28, 2017 · 9 comments

Comments

@raderio
Copy link

raderio commented Jul 28, 2017

We use Spring+Jackson(Java). And in our API we can send different object to same endpoint.
For example

  @JsonTypeInfo(
      use = JsonTypeInfo.Id.NAME, 
      include = As.PROPERTY, 
      property = "type")
    @JsonSubTypes({
        @JsonSubTypes.Type(value = Dog.class, name = "dog"),
        @JsonSubTypes.Type(value = Cat.class, name = "cat")
    })
    public static class Animal {
    }
 
    @JsonTypeName("dog")
    public static class Dog extends Animal {
        public double barkVolume;
    }
 
    @JsonTypeName("cat")
    public static class Cat extends Animal {
        boolean likesCream;
        public int lives;
    }

@Controller
public class MyController {

    //REST service
    @RequestMapping( value = "test")
    public  @ResponseBody String save(@RequestBody  Animal animal){
        System.out.println(animal.getClass());
        return success;
    }
}

Sending to /test

{
    "type": "dog",
    "barkVolume": 23.3
}

will show Dog.class

Sending to /test

{
    "type": "cat",
    "likesCream": true,
    "lives": 42
}

will show Cat.class

How to describe polymorphic endpoint in Swagger?

@raderio raderio changed the title How describe polymorphic endpoint How to describe polymorphic endpoint Jul 31, 2017
@Martin-Luft
Copy link

One problem is, that Swagger does not evaluate the @JsonTypeInfo annotation like mentioned here: #280 (comment) The type property is not a bean property and therefore Swagger does not recognize it :(

frantuma added a commit that referenced this issue Aug 18, 2019
refs #2340 - add JsonTypeInfo.property to schema if not existing
@mudit-sethi
Copy link

@frantuma Thanks for the fix. We really wanted this to be fixed. How can I try and test this fix? We are using swagger jaxrs2 plugin 2.0.8 at the moment.

@frantuma
Copy link
Member

This is available in latest 2.0.9-SNAPSHOT, just reference this version (in case enabling snapshot repos in your pom)

@mudit-sethi
Copy link

@frantuma I got a chance to try your fix with the following:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") @JsonSubTypes({ @JsonSubTypes.Type(name = "DOG", value = DogDto.class), @JsonSubTypes.Type(name = "CAT", value = CatDto.class)})

the swagger doc shows me the 'type' field but not include the sub types in it as part of the generated doc

@frantuma
Copy link
Member

your scenario seems to be working on e.g. in this test

can you share more details about the annotated resources and classes, the swagger configuration, and the resolved spec?

@mudit-sethi
Copy link

I have @Schema(type="object", discriminatorProperty = "type", required = true, oneOf = {Dog.class, Cat.class}) on the Animal class. I think it might be an issue with swagger-ui example not showing it correctly, when I look at the schema in UI then seems to be showing fine

@frantuma
Copy link
Member

If i get it right, it might be an issue with swagger-ui? if this is the case, please open an issue there (providing the spec causing the issue)

@SabhtarshaMojo
Copy link

Hit a NPE when my model had some required fields but did not have the discriminatorProperty as part of the class definition.
Could fix it by changing the code here:

if (modelToUpdate.getRequired() == null || !model.getRequired().contains(typeInfoProp)) {

Changed the condition to following and it fixed it,
if (modelToUpdate.getRequired() == null || !modelToUpdate.getRequired().contains(typeInfoProp)) {

frantuma added a commit that referenced this issue Sep 18, 2019
…ator

ref #2340 - fixes discriminator property resolving
@frantuma
Copy link
Member

thanks @SabhtarshaMojo , it was indeed a bug, fixed in #3293

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

No branches or pull requests

5 participants