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:
- 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.
- 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?
I have a Spring Boot API that uses
Longs as IDs for objects. It also usesJsonIdentityInfoin combination withJsonIdentityReference. We also have a client application that erroneously sends0as default value of aLong, as opposed tonull. This was not a problem until we upgraded from Spring Boot2.0.0to2.3.3(and thus Jackson2.11.2). Now, the following JSON results in aUnresolvedForwardReference{ "id": 0, "fooObjects": [ { "id": 0, "fooParameters": [ { "id": 0, "fooObject": 0 } ], "fooCollection": 0 } ] }Where the error is:
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:
CustomContextualLongDeserializerthat returnsnullinstead of0, if in the right context. This unfortunately causes aNullPointerExceptionbecause of thereturn nullstatement here and that being called from here. It seems like a custom deserializer is too late in the chain to do a replacement withnull.LongToLongConverter(same deal, returnsnullinstead of0) used in aJsonDeserializeannotation on the object fields marked as@JsonIdentityReference(alwaysAsId = true). This seems to work in some contexts and resolves theUnresolvedForwardReferenceproblem for the above JSON, but when deserializing the same object in other contexts, it causes the following error instead:Any suggestions for a general approach here?