On Wed, Jun 19, 2019 at 10:07 AM Paulomi Mukherjee
<paulomi....@gmail.com> wrote:
>
> Parent.java has
>
> @JsonTypeInfo(
>
>         use = JsonTypeInfo.Id.NAME,
>
>         defaultImpl = com.common.model.Parent.class,
>
>         include = JsonTypeInfo.As.EXISTING_PROPERTY,
>
>         property = "@class"
>
> )
>
> @JsonSubTypes({
>
>         @JsonSubTypes.Type(value = com.common.model.Child.class, name = 
> "com.common.model.Child"),
>
>         @JsonSubTypes.Type(value = com.common.model.Grandchild.class, name = 
> "com.common.model.Grandchild")
>
> })
>
>
>
> Child.java has
>
>
>
> @JsonTypeInfo(
>
>         use = JsonTypeInfo.Id.NAME,
>
>         defaultImpl = com.common.model.Parent.class,
>
>         include = JsonTypeInfo.As.EXISTING_PROPERTY,
>
>         property = "@class"
>
> )
>
> @JsonSubTypes(@JsonSubTypes.Type(value = com.common.model.Grandchild.class, 
> name = "com.common.model.Grandchild"))
>
>
>
>
>
> Note: in Child.java I mentioned defaultImpl as the Parent class as Child 
> class is abstract. My aim is to create a parent object if @class is missing 
> in the payload.
>
>
>
> But when I try to create the child object, I get the below error:
>
> JSON parse error: Missing type id when trying to resolve subtype of [simple 
> type, class com.common.model.Child]: missing type id property '@class'; 
> nested exception is 
> com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id 
> when trying to resolve subtype of [simple type, class 
> com.common.model.Child]: missing type id property '@class'\n at [Source: 
> (PushbackInputStream); line: 3, column: 1]
>
>
>
> I tried giving defaultImpl = Object.class in JsonTypeInfo of Child class. 
> Still I get the same missing type id property '@class' error.
>
>
>
> So, my question is, is it mandatory for defaultImpl to be on the same class 
> and any of the children class?

No --- actually it is recommended that child classes do NOT add
`@JsonTypeInfo` at all, because doing that may lead to impression that
it would be used -- and, worse, this can lead to incompatible behavior
between serialization (during which sub-class info IS used as
available from runtype value) and deserialization (during which only
base class information is usually available).

>
> Can I not give any random or the parent class in defaultImpl ?
>
> Spring boot version is 2.1.5.

No, you must use type that is compatible with expected result  base type.
That is, something that is assingment-compatible: if field with
polymorphic value is declared to be a `BaseType`, default
implementation must be `BaseType` or another type that
extends/implements it.

Things get more confusing if different base type is specified during
serialization and deserialization: ideally same type would be used,
but since root value type handling is fundamentally asymmetric (when
serializing there is an instance available, with subtype class; on
deserialization base type is usually given).

-+ Tatu +-

>
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to jackson-user+unsubscr...@googlegroups.com.
> To post to this group, send email to jackson-user@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/jackson-user/95b7a575-b70f-4420-8766-86f0db975c01%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/CAL4a10hkUhp8vRKV5Pq54-Npgw8M2pO6UmNgKeG44396%3DGkMjw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to