Typed object with anchor throws Already had POJO for id
See original GitHub issueTested 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:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top 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 >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
Ended up adding work-around (see https://github.com/FasterXML/jackson-annotations/issues/180) that will avoid the issue, pass the test.
@almson Yes, please open separate issue with changed reproduction, refer this issue as background.