ObjectId resolution with Builder fails for interface types (UnresolvedForwardReference) (original) (raw)
(note: follow-up for YAML issue FasterXML/jackson-dataformats-text#292)
When using @JsonIdentityInfo (with StringIdGenerator) together with @JsonTypeInfo / @JsonSubTypes and builder-based deserialization (@JsonDeserialize(builder=...)), back-references to already-deserialized objects fail with:
UnresolvedForwardReference: Unresolved forward references: [{Object id: "id1"}]
This appears to be because the Object Id gets registered against the builder instance rather than the final built object, so when a subsequent reference tries to resolve the id, the lookup fails.
The failure is specifically tied to builder-based deserialization combined with polymorphic type info and object identity — regardless of whether the base type is a class or interface.
To reproduce:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME) @JsonSubTypes({@JsonSubTypes.Type(name = "Derived", value = Derived.class)}) @JsonIdentityInfo(generator = ObjectIdGenerators.StringIdGenerator.class, resolver = SimpleObjectIdResolver.class) interface Base { }
@JsonDeserialize(builder = Derived.DerivedBuilder.class) class Derived implements Base { public String a; Derived(String a) { this.a = a; }
@JsonPOJOBuilder(withPrefix = "", buildMethodName = "build")
static class DerivedBuilder {
private String a;
public DerivedBuilder a(String a) { this.a = a; return this; }
public Derived build() { return new Derived(this.a); }
}}
// Second list element references the first by id — fails String json = """ {"list":[ {"@type":"Derived","@id":"id1","a":"foo"}, "id1" ]}""";