Closed kaqqao closed 2 months ago
Unfortunately this is due to inherent ambiguity of 1-parameter constructor: it can either match 1-property JSON Object (name/value) OR be delegating (the whole JSON value matching value type of the one parameter. Same is not true for 2- or more parameter constructor.
In case of 1-parameter, implicit detection of Creators will try to determine type heuristically, basically:
@JsonValue
found -> DelegatinggetTitle()
) -> PropertiesConstructorDetector
setting if defined (not set by defaultIn this case I think heuristics end up selecting Delegating.
2.18 does simplify handling slightly but basic logic remains the same.
This is why it is recommended that 1-parameter cases are annotated with
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
(or adding @JsonProperty
)
Ooh, I see. Thanks for explaining, much appreciated 🙏
@kaqqao No problem, this is not an intuitive thing unfortunately.
Description
A class with a single-parameter implicit constructor will fail deserialization when the parameter-names module is used to discover the names. But if another (unused) parameter is added, deserialization will work fine.
Version
2.17.1
Reproduction
This will throw:
But if another field is added (even if unused) to the
Book
's constructor:deserialization suddenly passes. Adding an explicit
@JsonProperty("title")
avoids the issue.Expected behavior
Deserialization should either pass or fail in both situations. A dummy unused parameter should not have such an effect.