zalando / problem

A Java library that implements application/problem+json
https://zalando.github.io/problem
MIT License
880 stars 83 forks source link

Possible to identify inheritor in request body without specifying type? #482

Closed Mukund2900 closed 1 year ago

Mukund2900 commented 1 year ago

I have a case where my request body has an object which is backed by inheritance. So when I use @RequestBody and do not explicitly specify the type of subclass it does not pick/determine that automatically. Is there a way to do it without this ->

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Fence.class, name = "fence"),
        @JsonSubTypes.Type(value = Rectangle.class, name = "rectangle"),
        @JsonSubTypes.Type(value = Point.class, name = "point"),
        @JsonSubTypes.Type(value = Circle.class, name = "circle")
})
public class Element implements Serializable {
}
whiskeysierra commented 1 year ago

This has nothing to do with this library in particular or Problem+JSON in general. You'd be better off asking this on stackoverflow.

Having said that: Instead of enumerating all subtypes at this place, you can put [`@JsonTypeName`](https://javadoc.io/static/com.fasterxml.jackson.core/jackson-annotations/2.15.0/com/fasterxml/jackson/annotation/JsonTypeName.html) on each class individually. But then you'd need to register those classes explicitly using [`registerSubTypes`](https://javadoc.io/static/com.fasterxml.jackson.core/jackson-databind/2.15.0/com/fasterxml/jackson/databind/cfg/MapperBuilder.html#registerSubtypes-java.lang.Class...-). I don't believe you can get rid of `JsonTypeInfo` here, since it is the root object.