-
-
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
Creator lookup fails with InvalidDefinitionException
for conflict between single-double/single-Double arg constructor
#3062
Comments
Hmmh. Interesting. Without looking into details, I am guessing that the conflict found -- and previously erroneously missed (as per Jackson's intent, not yours -- is between
which would be ambiguous. Previously one of them would have been arbitrarily selected (depending on order in which JDK happens to expose them, undefined although traditionally tends to be declaration order). Discovery of a small set of single-argument delegating creators is a sort of legacy feature from Jackson 1.0 era (before introduction of So... the fix itself was intentional, but due to long history of it not being detected I can see why this is problematic; especially given that you do not actually need either. Unfortunately trade-off here (as usual) is between surfacing a likely problem early, at point where it is not known if there is a problem, vs. indicating it only if known (in this case that would be deferral of selection of constructor until one is actually needed, when seeing JSON floating-point number value). To prevent discovery of such constructors it is possible to change auto-detection visibility level with something like:
(or equivalent method in ObjectMapper). This would not change handling of 0-args constructor which will always be detected, and so annotation does not (currently) matter. Similarly you can add Now... to solve the problem otherwise requires some thinking; especially if resolution should go in a patch release (2.12.2 or later), due to backwards compatibility. I can all but guarantee that most changes would result someone else's use case breaking somewhere. :-( First, existence and possible annotation of 0-args constructor is unfortunately not something we can use -- this constructor is only used with Object values, and But I think another option that might work better would be to figure out how to resolve this particular case of wrapper/primitive value (assuming this is what triggers the problem which I can verify). Question then is... which one should have precedence? My gut feeling is that it should be primitive value ( |
Quick note: test seems to pass on 2.11(.4), fail on 2.12.2-SNAPSHOT. Also, forgot to start my initial comment with an important thing: thank you @perdurabo476 for reporting this and other issues! I hope we can address them; at this point I am trying to finalize 2.12.2 since there are a few important fixes in already. |
InvalidDefinitionException
for conflict between single-double/single-Double arg constructor
Implemented so that in case of 2 potentially conflicting constructors (usually implicit), where pairing is primitive/wrapper, primitive one is selected. This resolves reported problem as well as a few other possible kinds (wrt |
Describe the bug
We are using Jackson to (de)serialize POJO's representing our public API in a message oriented middleware (Apache Artemis).
We are using JsonProperty annotations to explicitly annotate getters/setters for properties to use during (de)serialization and each POJO defines at least one public no-arg constructor to use for object creation upon deserialization.
Some POJO's also include additional constructors for convenience reasons, which are not intended to be used by Jackson during deserialization.
After upgrading to 2.12.1 deserialization fails from some of those classes with the following Exception:
The Exception is thrown by method _reportDuplicateCreator in the CreatorCollector class (line 335) which was added in 2.12
Version information
problem occurs in 2.12.1
after upgrade from 2.10.4
To Reproduce
The following code snippet shows a (simplfied) version of one of the POJOs where this problem occurs:
The following simple test case (using a vanilla ObjectMapper instance) works fine in 2.10.4 but throws the Exception mentioned above in 2.12.1:
The explicit JsonCreator annotation in the example above was added for testing purposes (with 2.10.4 we did not explicitly annotate constructors at all!) but it makes no difference if the default constructor is annotated or not.
Expected behavior
In case multiple constructors are present Jackson should default to using the parameterless default constructor if no other creator is explicitly annotated.
In case one constructor is explicitly annotated using JsonCreator Jackson should use the annotated constructor instead.
Additional context
I think the example above represents a a rather common/basic use case for (de)serialization using Jackson and should work out of the box without additional configuration.
Per default we use JSON as target format for serialized objects, however we also support Protobuf as message format, therefore the JsonProperty annotation in the example also specifies an explicit index.
The text was updated successfully, but these errors were encountered: