-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
String-based Map
key deserializer is not deterministic when there is no single arg constructor
#3143
Comments
That does sound sub-optimal and should either throw an exception for ambiguous cases, or at least use stable tie-breaker -- possible latter for 2.12 (to prevent perceived regression for some usage), and exception for 2.13. |
Would that mean |
@hisener I'll have look into details but I think that rules should be similar to value deserializers:
For enums there is also |
Added the failing test from above and can reproduce the issue. Method that selects Creator method is |
Map
key deserializer is not deterministic when there is no single arg constructor
Describe the bug
StdKeyDeserializers#findStringBasedKeyDeserializer
uses a static factory method when there is no single arg constructor. It gets the factory method usingBasicBeanDescription#findFactoryMethod
that returns the first single-arg static method. However, the behavior is not deterministic asjava.lang.Class#getDeclaredMethods
Javadoc states:The factory methods are collected using
AnnotatedCreatorCollector#_findPotentialFactories
->ClassUtil#getClassMethods
->java.lang.Class#getDeclaredMethods
.So in the case of two static factory methods, one with
@JsonCreator
annotation and one named#valueOf
or#fromString
, Jackson may use either of them depending on the array order.The second method needs to be named
#valueOf
or#fromString
because of the followingBasicBeanDescription#isFactoryMethod
logic:jackson-databind/src/main/java/com/fasterxml/jackson/databind/introspect/BasicBeanDescription.java
Lines 631 to 645 in e85f7f0
Version information
2.12.2
To Reproduce
It's also available in this branch: 2.12...PicnicSupermarket:hsener/find-factory-method.
Expected behavior
@JsonCreator
method has precedence over other static factory methods in String-based deserialization.Additional context
We noticed this behavior in a custom key deserializer, which uses
BasicBeanDescription#findFactoryMethod
. The behavior was nondeterministic for an enum where we haveEnum#valueOf
and a static factory method with@JsonCreator
annotation.The text was updated successfully, but these errors were encountered: