You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched in the issues and found nothing similar.
Describe the bug
It is currently not possible to combine @JsonUnwrapped with generic types using @JsonTypeInfo
Version Information
2.17.2
Reproduction
I have a generic Versioned<T> type containing a int version() and a payload T item(). I'd like to be able to (de)serialize this using @JsonUnwrapped, but cannot seem to get this to work together with the necessary @JsonTypeInfo to deal with the generic types.
Take the following types
public record Inner(Stringfoo, intbar) {}
public record Versioned<T>(
intversion,
@JsonUnwrapped@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class") Titem) {}
And the following example
Versioned<Inner> example = newVersioned<>(1, newInner("foo", 123));
newObjectMapper().writeValueAsString(example);
But this does not work. Even when disabling SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS it still does drops the @class property, making deserialization fail.
Expected behavior
No response
Additional context
I found a bunch of threads from 10+ years ago where this was not supported through Jackson. I hope this has changed by now. It is not clear to me from this simple example why this is not supported.
Alternatively, I tried to create a manual StdSerializer and StdDeserializer but was unable to get the @JsonTypeInfo annotation to work. Any examples given would be great!
The text was updated successfully, but these errors were encountered:
Quick note: I'd recommend trying this out with 2.18.0-rc1 since there have been many improvements to property introspection, specifically helping with record types.
The issue may still be there but it is good to rule out possibility of a fix.
Quick note: I'd recommend trying this out with 2.18.0-rc1 since there have been many improvements to property introspection, specifically helping with record types. The issue may still be there but it is good to rule out possibility of a fix.
Thanks for the tip! Gave it a try, but observing same issue. FWIW, the records are a mere example. The issue is the same with any POJO.
Ok just wanted to rule out any possibility of property-introspection fixes (which include improvements to Creator handling, which while essential to Records also affects Constructor usage by POJOs) in 2.18 might have helped.
But looking at this more closely... this won't work now, or possibly, ever.
Unwrapping only affects regular properties, not type ids -- polymorphic id is considered metadata and could not be moved around.
Further problems are conflict between generic and polymorphic types (won't play nicely).
So I suspect that to make things work, you will need to change the set up -- as things are declared this is unlikely to be workable with Jackson (at least without custom (de)serializers, but writing those is lots of work).
Search before asking
Describe the bug
It is currently not possible to combine
@JsonUnwrapped
with generic types using@JsonTypeInfo
Version Information
2.17.2
Reproduction
I have a generic
Versioned<T>
type containing aint version()
and a payloadT item()
. I'd like to be able to (de)serialize this using@JsonUnwrapped
, but cannot seem to get this to work together with the necessary@JsonTypeInfo
to deal with the generic types.Take the following types
And the following example
I would expect this produce the following JSON:
But this does not work. Even when disabling
SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS
it still does drops the@class
property, making deserialization fail.Expected behavior
No response
Additional context
I found a bunch of threads from 10+ years ago where this was not supported through Jackson. I hope this has changed by now. It is not clear to me from this simple example why this is not supported.
Alternatively, I tried to create a manual
StdSerializer
andStdDeserializer
but was unable to get the@JsonTypeInfo
annotation to work. Any examples given would be great!The text was updated successfully, but these errors were encountered: