-
-
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
JsonCreator on static method in Enum and Enum used as key in map fails randomly #2725
Comments
I would prefer the first proposal: going through all found static @JsonCreator methods. There are use cases when you need more creators. |
First things first: I agree that whether this works or not should be deterministic. One suggestion: since 2.11.0 was just released, may make sense to see if that has improved handling. I think I very recently noticed a missing validation in |
I upgraded to 2.11.0 but the behaviour is still the same, sometimes it works, sometimes it fails |
Thank you for checking. I was about to ask if this only concerns Enum-as-Map-key case, but reading through desc, I think this is true, and "enum as value" case does not have same issue. |
@tomas-skalicky I think one challenge here is that whereas for more general |
I can reproduce this locally, added a failing test. Seems easy enough to change not to fail on first ineligible This is bit messy area, as division between "values" and "Map keys" is not well modeled by Jackson; but it stems from JSON only having Strings as key values, and corresponding need to limit value set, as well as requirements for actual (de)serializers (as Map keys are exposed as |
given this piece of code and jackson-databind 2.10.3
and running it on multiple computers the output is different.
sometimes it works and the map is deserialized correctly and sometimes it does not work and fails with
Exception in thread "main" java.lang.IllegalArgumentException: Parameter #0 type for factory method ([method foo.Test$TestEnum#getByIntegerId(1 params)]) not suitable, must be java.lang.String
now when I change the order of the methods annotated with JsonCreator it works, but then it started to fail on other computers with the same exception as above.
I tried to debug this and found out that sometimes the order of the annotated methods is different that is iterated over in
BasicDeserializerFactory#_createEnumKeyDeserializer
when taking a look at thebeanDesc.getFactoryMethods()
methods. These list of methods as far as I could see is created inAnnotatedCreatorCollector#_findPotentialFactories
viaClassUtil.getClassMethods
which then callsClassUtil.getDeclaredMethods(cls)
and this is usingClass#getDeclaredMethods
which as written in the JavaDoc that the methodselements in the returned array are not sorted and are not in any particular order.
So this is now probably the root of the issues which I can see. And as the loop stops with the exception after the first candidate has been checked inBasicDeserializerFactory
the remaining methods are not taken into consideration.From my perspective, all annotated methods need to be checked before the exception can be thrown or at least the annotation parser should fail when multiple creators are detected.
The text was updated successfully, but these errors were encountered: