question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typed object throws "Missing type id" when annotated with @JsonIdentityInfo

See original GitHub issue

Tested 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:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
yawkatcommented, Jun 29, 2022

your code does not use yaml though? this was specifically a yaml issue

1reaction
cowtowncodercommented, Nov 24, 2020

On further inspection, no buffering involved in this particular case. But type-id is off between cases: without @JsonIdentityInfo, type id asked at START_OBJECT and is found; with it asked on following FIELD_NAME and no longer available.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found