Jackson @JsonSubType Structure check

Viewed 31

I have Classes like below:

public class Car{
   
   String name;

   Short type;

   @JsonTypeInfo(property = "type", include = JsonTypeInfo.As.EXTERNAL_PROPERTY, use = JsonTypeInfo.Id.NAME)
   @JsonSubTypes({
            @JsonSubTypes.Type(value=A.class , name = "1"),
            @JsonSubTypes.Type(value=B.class , name = "2"),
    })
   Object o;
   
}

public class A{
   String foo;
}

public class B{
   String foo;
   String bar;
}

When i send wrong JSON structure like:

{
"name": "John" ,
"type": 1 ,
"o": {
   "foo": "fooValue",
   "bar": "barValue"

}

I expect to get Jackson MismatchedInputException but It do not raise exception. Jackson set o as A class and set foo property value to fooValue.

I used @JsonIgnoreProperties(ignoreUnknown = false) too but it did not work. Any body have suggession?

0 Answers
Related