Typed object throws "Missing type id" when annotated with @JsonIdentityInfo
See original GitHub issueTested with Jackson 2.12.0-rc2-SNAPSHOT
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.util.List;
import org.junit.Test;
public class JacksonYamlTest {
static class Container {
@JsonProperty
List<Base> list;
}
@JsonTypeInfo(use = Id.NAME)
@JsonSubTypes({@Type(name="Derived", value=Derived.class)})
@JsonIdentityInfo(generator = ObjectIdGenerators.StringIdGenerator.class)
static class Base {
}
static class Derived extends Base {
@JsonProperty
String a;
}
@Test
public void typedTestYaml() throws Exception {
String yaml = "list:\n" +
" - !Derived\n" +
" a: foo";
ObjectMapper mapper = new ObjectMapper (new YAMLFactory());
Container container = mapper.readValue (yaml, Container.class);
System.out.println (((Derived)container.list.get(0)).a);
}
@Test
public void typedTestJson() throws Exception {
String json = "{\"list\":[{\"@type\":\"Derived\",\"a\":\"foo\"}]}";
ObjectMapper mapper = new ObjectMapper();
Container container = mapper.readValue (json, Container.class);
System.out.println (((Derived)container.list.get(0)).a);
}
}
typedTestYaml throws:
com.fasterxml.jackson.databind.exc.InvalidTypeIdException:
Missing type id when trying to resolve subtype of [simple type, class JacksonYamlTest$Base]: missing type id property '@type' (for POJO property 'list')
at [Source: (StringReader); line: 3, column: 15] (through reference chain: JacksonYamlTest$Container["list"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
at com.fasterxml.jackson.databind.DeserializationContext.missingTypeIdException(DeserializationContext.java:1945)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingTypeId(DeserializationContext.java:1458)
at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._handleMissingTypeId(TypeDeserializerBase.java:307)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedUsingDefaultImpl(AsPropertyTypeDeserializer.java:174)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:113)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1277)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeWithObjectId(CollectionDeserializer.java:443)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeFromArray(CollectionDeserializer.java:334)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:324)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4591)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3546)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3514)
at JacksonYamlTest.typedTestYaml(JacksonYamlTest.java:54)com.fasterxml.jackson.databind.exc.InvalidTypeIdException:
Missing type id when trying to resolve subtype of [simple type, class JacksonYamlTest$Base]: missing type id property '@type' (for POJO property 'list')
at [Source: (StringReader); line: 3, column: 15] (through reference chain: JacksonYamlTest$Container["list"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
at com.fasterxml.jackson.databind.DeserializationContext.missingTypeIdException(DeserializationContext.java:1945)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingTypeId(DeserializationContext.java:1458)
at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._handleMissingTypeId(TypeDeserializerBase.java:307)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedUsingDefaultImpl(AsPropertyTypeDeserializer.java:174)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:113)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1277)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeWithObjectId(CollectionDeserializer.java:443)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeFromArray(CollectionDeserializer.java:334)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:324)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4591)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3546)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3514)
at JacksonYamlTest.typedTestYaml(JacksonYamlTest.java:54)
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Jackson combining @JsonIdentityInfo and @JsonTypeInfo ...
It seems that Jackson expects an object at the animal2 property which has a @class property to find the correct class type to...
Read more >Using @JsonTypeInfo annotation to handle polymorphic types
Let's serialize and then deserialize a View object: public class ExampleMain { public static void main(String[] args) throws IOException ...
Read more >Jackson Json - @JsonTypeInfo, using Logical Type Name ...
If name is missing, class of the type will be checked for @JsonTypeName annotation, and if that is also missing or empty, a...
Read more >Jackson Annotations for JSON - Spring Framework Guru
@JsonIdentityInfo. The. @JsonIdentityInfo. @JsonIdentityInfo tells Jackson to perform serialization or deserialization using the identity of the object. This ...
Read more >Jackson Annotations - @JsonIdentityInfo - Tutorialspoint
@JsonIdentityInfo is used to indicate that object identity will be used during serialization/de-serialization. Example - @JsonIdentityInfo. import java.io.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
your code does not use yaml though? this was specifically a yaml issue
On further inspection, no buffering involved in this particular case. But type-id is off between cases: without
@JsonIdentityInfo
, type id asked atSTART_OBJECT
and is found; with it asked on followingFIELD_NAME
and no longer available.