Serialization to subtype when type info parameter is missing

16 views
Skip to first unread message

Saurabh Pawar

unread,
Jul 23, 2019, 8:19:22 PM7/23/19
to jackson-user

I have polymorphic types and serialization when the type parameter is passed in JSON works fine.


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", defaultImpl = Dog.class)
@JsonSubTypes({
    @JsonSubTypes.Type(value=Dog.class, name="dog"),
    @JsonSubTypes.Type(value=Cat.class, name="cat")
})    
public class Animal { ... }

public class Dog extends Animal { ... }
public class Cat extends Animal { ... }

{"name":"Tom", type="cat"} // this works as expected

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response animal(Animal animal) {...}

But, if I directly want to get a subtype object without passing type it does not work.

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response dog(Dog dog) {...}

{"name":"Tom"} // this wont work

 

Missing type id when trying to resolve subtype of [simple type, class ....Dog]: missing type id property 'type'

Any way to achieve this?

Tatu Saloranta

unread,
Jul 25, 2019, 10:10:10 PM7/25/19
to jackson-user
If and when there is `defaultImpl` specified, missing type id should
result in instance of default type being constructed, so this should
work.
So if you can create a standalone test to reproduce the problem,
please file a Github issue.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages