Skip to content

Handling UnresolvedForwardReference by pre-processing illegal values #2955

Description

@austalakov

I have a Spring Boot API that uses Longs as IDs for objects. It also uses JsonIdentityInfo in combination with JsonIdentityReference. We also have a client application that erroneously sends 0 as default value of a Long, as opposed to null. This was not a problem until we upgraded from Spring Boot 2.0.0 to 2.3.3 (and thus Jackson 2.11.2). Now, the following JSON results in a UnresolvedForwardReference

{
  "id": 0,
  "fooObjects": [
    {
      "id": 0,
      "fooParameters": [
        {
          "id": 0,
          "fooObject": 0
        }
      ],
      "fooCollection": 0
    }
  ]
}

Where the error is:

Unresolved forward references for: 
 at [Source: (PushbackInputStream); line: 1, column: 6161]Object id [0] (for `com.foo.entities.FooObjectEntity`) at [Source: (PushbackInputStream); line: 1, column: 462]

Obviously, the problem should be fixed on the client side, but that is quite difficult in our situation, so I am looking if there is something we can do on the API side with Jackson.

I have tried the following:

  1. A CustomContextualLongDeserializer that returns null instead of 0, if in the right context. This unfortunately causes a NullPointerException because of the return null statement here and that being called from here. It seems like a custom deserializer is too late in the chain to do a replacement with null.
  2. A LongToLongConverter (same deal, returns null instead of 0) used in a JsonDeserialize annotation on the object fields marked as @JsonIdentityReference(alwaysAsId = true). This seems to work in some contexts and resolves the UnresolvedForwardReference problem for the above JSON, but when deserializing the same object in other contexts, it causes the following error instead:
Problem deserializing property 'fooCollection' (expected type: [simple type, class com.foo.entities.FooCollectionEntity]; actual type: `java.lang.Long`)

Any suggestions for a general approach here?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions