Skip to content

ObjectId resolution with Builder fails for interface types (UnresolvedForwardReference) #5872

@cowtowncoder

Description

@cowtowncoder

(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"
      ]}""";

Metadata

Metadata

Assignees

No one assigned

    Labels

    builder-relatedIssue related to handling of Builder-style deserializationhas-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issueobjectid

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions