-
-
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
Support READ_UNKNOWN_ENUM_VALUES_AS_NULL
with @JsonCreator
#1642
Support READ_UNKNOWN_ENUM_VALUES_AS_NULL
with @JsonCreator
#1642
Conversation
The deserialization feature READ_UNKNOWN_ENUM_VALUES_AS_NULL allows enum values that are not recognised to take a null value when parsed. Before this commit, this deserialization feature did not work whenever an enum had a method marked @JsonCreator, and the enum creator method itself had to choose null in the case of unknown input. With this change, if an enum creator method throws an IllegalArgumentException then this is considered to be an unknown value and (if READ_UNKNOWN_ENUM_VALUES_AS_NULL is active) then null will be used.
Makes sense, thank you for contributing this! Quick question? Have I already asked for a CLA? I thought I might have, and if so we are good. https://github.com/FasterXML/jackson/blob/master/contributor-agreement.pdf and the quickest usual way is to print, fill & sign, scan, email to Looking forward to merging this in (I can backport in 2.8 as well). |
Done, cheers! 👍 |
READ_UNKNOWN_ENUM_VALUES_AS_NULL
with @JsonCreator
Hmmh. Ok. I did merge this, but I am bit uncomfortable with swallowing the exception part; especially for wide applicability. Part of this is that throwing (or, rather, constructing) exceptions is very costly; but the other part (wide applicability) that this could mask real problems. But maybe a reasonable compromise would be only "convert" |
@cowtowncoder maybe I'm not understanding you entirely here, but my intent with this change is to only 'convert' Here's the snippet (my new lines have a Throwable t = ClassUtil.throwRootCauseIfIOE(e);
+ if (ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL) &&
+ t instanceof IllegalArgumentException) {
+ return null;
+ }
+
return ctxt.handleInstantiationProblem(_valueClass, value, t); Does that not do what I think it does? In the case that |
@joelittlejohn as is, it would catch all kinds of But I think that if just |
@cowtowncoder As I understand the code, if the root cause is a NullPointerException then jackson-databind/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java Line 1029 in 3e75d54
|
Acutally, never mind; I was being blind. Check is being already done for |
quick question: has this fix been backported to 2.8? |
@yichuan1118 As per my earlier comment on a commit:
|
The deserialization feature READ_UNKNOWN_ENUM_VALUES_AS_NULL allows enum values that are not recognised to take a null value when parsed. Before this commit, this deserialization feature did not work whenever an enum had a method marked
@JsonCreator
, and the enum creator method itself hadto choose null in the case of unknown input.
With this change, if an enum creator method throws an IllegalArgumentException then this is considered to be an unknown value and (if READ_UNKNOWN_ENUM_VALUES_AS_NULL is active) then null will be
used.