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 with anchor throws Already had POJO for id

See original GitHub issue

Tested with jackson-dataformat-yaml 2.11.3

package foo

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 typedObjectIdTest() throws Exception {
        
        String yaml = "list:\n" +
                      "    - !Derived &id1\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);
    }
}

throws

com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.String) [[ObjectId: key=id1, type=com.fasterxml.jackson.annotation.ObjectIdGenerators$StringIdGenerator, scope=NONE]] (through reference chain: JacksonYamlTest$Container["list"]->java.util.ArrayList[0])
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:397)
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:368)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeWithObjectId(CollectionDeserializer.java:406)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:277)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:249)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)
	at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:156)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4526)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3468)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3436)
	at JacksonYamlTest.typedObjectIdTest(JacksonYamlTest.java:41)

the problem is that SimpleObjectIdResolver.bindItem is called twice. Possibly, BeanDeserializerBase:1188 is not necessary at all.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Nov 12, 2020

Ended up adding work-around (see https://github.com/FasterXML/jackson-annotations/issues/180) that will avoid the issue, pass the test.

0reactions
cowtowncodercommented, Nov 12, 2020

@almson Yes, please open separate issue with changed reproduction, refer this issue as background.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonMappingException: Already had POJO for id
From Annotation Type JsonIdentityInfo : Scope is used to define applicability of an Object Id: all ids must be unique within their scope; ......
Read more >
JsonMappingException: Already had POJO for id
The Problem We have two entities with one-to-many relationships which references each other, but it failed with the exception:
Read more >
Metaprogramming
In Groovy we work with three kinds of objects: POJO, POGO and Groovy Interceptors. Groovy allows metaprogramming for all types of objects but...
Read more >
Consolidated JDK 8 Release Notes
An object becomes phantom reachable after it has been finalized. ... The InetAddress class methods are updated to throw an java.net.
Read more >
Already had POJO for id error
it says to add the JsonIdentityInfo annotation, which I did, but the error keep showing. this is a POST to modify an existent...
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