-
-
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
@JsonValue
with integer for enum does not deserialize correctly
#1850
Comments
Ok. Yes, it seems that implict rule of using index overrides delegating construction for integers in this case. I am not 100% if this should or should not work, however, since there is actually one more step that in general is needed: addition of Creator method annotated with This is not needed for I think I'll need to investigate if and how this could be made to work; it seems doable but internal handling does get bit complicated as this is a special case. |
Yes, I can see how it's not a common case. If it's deemed not worthy of inclusion, it might help to clarify the javadocs on |
@tgolden-andplus Yes that would definitely make sense at very least. |
I have also recently experienced this and it was certainly a mystery trying to figure it out. In my case it didn't fail, it just picked the wrong enum (the corresponding ordinal in the enum list) rather than the enum I expected. Very, very confusing. |
One more thing: it is not that |
I also stumbled across this today. Would be cool if that could be handled transparently for enums :). |
Howdy. I just hit this problem. I actually had set I see that the problem lies in My suggestion is the following: keep track of if
This is the testcase.
|
@joaoe Could you please create a new issue and your test case there? While issues look similar they may not be the same and it is easier to track them as separate entries. |
Hi. My issue is exactly the same as the one described originally. |
Hi, I had same issue today, I managed to resolve that using this 2 annotations implemented within my
Hope that helps someone else too, I did spent quite some time to find the variant that works... |
Ok so yes, while I'll add a failing test and hopefully someone at some point has time to improve this; change could go in 2.11 (since I am pretty sure internal API at least has to change to accommodate need to pass typed lookup info). |
How to do it in Kotlin? There is no static method in Kotlin but companion object method, which not works by using annotation @JsonCreator |
LukeChow, |
this worked perfect in my case. thanks a lot @millevlada |
Note: had a dup (#2754). |
We still see the issue in deserialization of the enum of type Integer not with primitive type. This is happening after Jackson library upgrade to 2.11.2 version of the library from 2.9.7, it also exists in the version 2.12.4 Our code was a generated one and generated fine with both required annotations properly. The generated enum from the swagger yaml file is below:
Pojo Class using the enum type:
Input Json to parse: { count: 1 } Exception occurred when tried to parse:
Throwing com.fasterxml.jackson.databind.exc.ValueInstantiationException.from(ValueInstantiationException.Java:47) Can you or someone already has a solution to this issue? Any help will be greatly appreciated. |
@srinivasreddyp I am not sure I understand -- this issue is open so I don't see how it would have been solved. Example has some problems too: content included is invalid JSON (missing double-quotes around Biggger problem, and something that SHOULD throw an exception (but does not seem to is that this is illegal:
as you cannot have 2 I don't know if your case is possible to work around right now. One solution that would work would be to have |
Apologies for the typos, I was not supposed to paste in the actual code so I tried to type in manually which lead to typos in code above. Here is the live example which will produce the exception with the Jackson library versions 2.11.2 & 2.12.3 The same is running fine with the Jackson 2.9.7 version. The issue is only with the Deserialization, where as the Serialization is good. FileName: Info.java `package com.jackson.test; import com.fasterxml.jackson.annotation.JsonCreator; public class Info { public static void main(String[] args) throws Exception { class Data { enum MyEnum { private Integer code; MyEnum(Integer code) { @JsonCreator @JsonValue @OverRide Below is the exception stack trace after running the above code: Exception in thread "main" com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of com.jackson.test.MyEnum, problem: argument type mismatch The same code is working fine if the json received to parse is holding the value of type String instead of Integer. FileName: Info.java `package com.jackson.test; import com.fasterxml.jackson.annotation.JsonCreator; public class Info { public static void main(String[] args) throws Exception { class Data { enum MyEnum { private Integer code; MyEnum(Integer code) { @JsonCreator @JsonValue @OverRide Output after running the above code: Count: 1 Process finished with exit code 0 Please run the above code with the versions I specified, hope this will help understand my issue and reproduce the problem. |
I don't think we are short of examples per se. I just don't really have time to work on this issue, unfortunately. A big challenge at code level is that handling of Enum types is quite different from that of POJOs. Part of this is due to natural differences: Enums are quite different things from Bean-style POJOs (enum values are canonical). |
np .. thank you ! |
I have investigated this issue further, the issue for our case lies in the swagger code generator rather than Jackson The link above has a template code which is used to generate the Pojos out of swagger yaml. Probably the fix is required in their library to consider the Object type as a parameter over the method annotated with @JsonCreator eg: Below code is working for any Java type.
Will try to push a patch to swagger code generator plugin. |
@srinivasreddyp Ok I am glad you found out possible way forward. Handling of Enum creators is bit tricky but generator should be able to produce a working combination. |
Jackson always uses the ordinal number instead of the integer this fix was posted on the comments of the issue regarding this problem: FasterXML/jackson-databind#1850 (comment)
I've managed to fix it by setting @JsonCreator mode to DELEGATING and having @JsonValue on my id field SOME_VALUE(1), @JsonValue
|
Is the fix released ? If yes, in which version ? |
Thanks to @limengning's PR, will shortly fix this! |
@JsonValue
with integer for enum does not deserialize correctly
The Javadoc for
@JsonValue
states that it is the only required annotation to both serialize and deserialize enums as something other than their name or ordinal:The Javadoc also states that it can be applied to any scalar type:
However, annotating an enum like below will fail to deserialize -- Jackson appears to interpret the integer as the ordinal of the enum.
When attempting to deserialize an enum like the above example, on Jackson 2.9.2, I receive the following stack trace: (slightly anonymized)
If I add a
@JsonCreator
to a static method that matches the value to the internal field, the enum can be deserialized correctly. However that is more code I would rather not maintain if possible.The text was updated successfully, but these errors were encountered: