Fix improper (De)Serialization given both @JsonTypeInfo and @JsonValue#5850
Fix improper (De)Serialization given both @JsonTypeInfo and @JsonValue#5850blacelle wants to merge 3 commits intoFasterXML:3.xfrom
Conversation
|
Failing tests: relies on relies on
I can not find in
Or maybe, what I'm looking for is a way to prevent |
| */ | ||
| boolean forceTypeInformation; | ||
| if (_accessor == null) { | ||
| // Why do we forceTypeInformation if the type is natural? Shouldn't it be the contrary? |
There was a problem hiding this comment.
Agree that sounds odd. It must be due to fundamental problem with @JsonValue handling usually changing type of value being serialized (POJO) into intermediate type; so assumption here being former is not "natural", but if latter is, we want to determine type serialization requirement on former.
Like @JsonValue annotated method returning String, for some POJO.
|
There is basic contradiction between Problem is the "translation" And so So one possibility would be to start failing cases like this. If so, we'd likely need But not that if the two are supported, handling of |
|
First, let me reword my use-case; why I go for both
So:
This is much related with #5705, which address this need with custom
I suggest something in this vein, yes. |
|
Ok, put another way, the problem is that unlike for regular usage, where both It may not be impossible to support but is hard to reason about within code. of, say: will need This case also illustrated challenges on "natural" types -- so, normally when serializing "String as String", no type info should be included, f.ex on: no type id would be include for but this is now a problem for Changing |
|
I guess I get your point. TO put it in others words: with In my case (and in my original understanding), the question was irrelevant as With my current understanding, I just see an ambiguity.
I guess you consider
I'll have a look. Regarding the ambiguity, I wonder if one way forward is we could disable |
|
On Mon, Mar 30, 2026 at 12:02 AM Benoit Chatain Lacelle < ***@***.***> wrote:
*blacelle* left a comment (FasterXML/jackson-databind#5850)
<#5850?email_source=notifications&email_token=AAANOGOMMRJB4JEM3IJFUOD4TILYNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIMJVGI3TGNRWGAY2M4TFMFZW63VHMNXW23LFNZ2KKZLWMVXHJNLQOJPWG33NNVSW45C7N5YGK3S7MNWGSY3L#issuecomment-4152736601>
I guess I get your point. TO put it in others words: with @jsonvalue, it
may be ambiguous to which type does @JsonTypeInfo should apply: the
*embedding* type or the *embedded* type.
It's not ambiguous -- it must be "real" type, not one related to @jsonvalue
annotated accessor -- but it is inconsistent/unrelated to @jsonvalue
annotated value type being actually serialized at low level.
Or put another way: assumption of same value/type being given to
ValueSerializer and TypeSerializes is wrong: we need type info for value of
type "MyValue" while serialization actual java.lang.String value (and so
forth).
In my case (and in my original understanding), the question was irrelevant
as @JsonValuewas always on a natural type (so I expect to type in the
output), and I used @JsonTypeInfo.defaultImpl to force deserialization
with the *embedding* type.
With my current understanding, I just see an ambiguity.
But whereas "natural" String needs no type id (Deserializer knows to map
it without type id), surrogate case for @MyValue
<https://github.com/MyValue> absolutely does (because we do not want to
deserialize as String but as MyValue and hence must have type id to map).
I guess you consider @JsonTypeInfo should output the *embedding* type,
even if the embedded value is a *primitive*.
If I understand the embedding/embedded terms correctly, yes. Type info must
always be about the type that is conceptually being processed (MyValue),
even when physically serializing something else (like String),
type @jsonvalue annotated accessor returns.
Changing StringSerializer to delegate to TypeSerializer breaks 3 unit tests
which is not a lot (but suggests problems of course) so maybe it should be
done -- but only after careful consideration...
I'll have a look.
-+ Tatu +-
… ------------------------------
Regarding the ambiguity, I wonder if one way forward is we could disable
@JsonTypeInfo for some types. In my case, I would disable it for the
class with a @jsonvalue. e.g. what if I go for @JsonTypeInfo.As = NONE
for the type with @jsonvalue? (I suppose it does not work in current code
base, but it may help around this matter).
—
Reply to this email directly, view it on GitHub
<#5850?email_source=notifications&email_token=AAANOGOMMRJB4JEM3IJFUOD4TILYNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIMJVGI3TGNRWGAY2M4TFMFZW63VHMNXW23LFNZ2KKZLWMVXHJNLQOJPWG33NNVSW45C7N5YGK3S7MNWGSY3L#issuecomment-4152736601>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAANOGKIERPAPPXARLHDDJD4TILYNAVCNFSM6AAAAACXDFJYKWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DCNJSG4ZTMNRQGE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
This follow-up #5035
I could open a dedicated issue, but I felt fine just opening a PR for now given I have code (UT and draft for fix).
This case refers to the interaction of
@JsonTypeInfoand@JsonValue.@JsonValue, I argue@JsonTypeInfoshould not generally not trigger the type.@JsonValuefield in an array.Unit-tests demonstrates 2 cases which relates to the similar functional cases, but they seem to refer to different Jackson classes:
@JsonValueimplements some interface with@JsonTypeInfoenum(without@JsonValuebut with similar semantic) implements some interface with@JsonTypeInfoI reach good (in my perspective) behavior for the
@JsonValuecase by forcingJsonValueSerializer.forceTypeInformationwhen serializer has a_accessor(which I interpret as a signal there is a@JsonValue) .The
enumis not benefitting from@JsonValuecase as it seems there is no similarforceTypeInformationaround enum.Some stack relating to the enum case (I tried to find a spot to disable typeSerialization along this stack):