-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Allow default enums with @JsonCreator
#4979
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
Allow default enums with @JsonCreator
#4979
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks clean overall. Though we need the old constructor back, other than that, minor adjustments
src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java
Show resolved
Hide resolved
src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDeserializationTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDeserializationTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDeserializationTest.java
Outdated
Show resolved
Hide resolved
Looks like there's one unit test regression with NPE. |
This follows the pattern for READ_UNKNOWN_ENUM_VALUES_AS_NULL from here: https://github.com/FasterXML/jackson-databind/pull/1642/files but for READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE It's more tricky because it wasn't immediately apparent how to get the configured default value on the Creator impl side. I decided to try using the EnumResolver, which works, but am not sure what the repercussions of that are fully though. It works the same way, so only IllegalArgumentExceptions will trigger default behavior, so if you have some other custom creator exception logic that will be unaffected.
db9fb7f
to
143b66f
Compare
|
Aside from test fail(s), one thing before I can merge this PR (after review) is CLA from here: https://github.com/FasterXML/jackson/blob/master/contributor-agreement.pdf (unless already sent). Needs to be sent just once, good for all future contributions. Looking forward to getting this merged, thank you! |
We have compile error now |
Yeah, made a typo when fixing something. I’ll fix it and sign the CLA next week. |
src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java
Show resolved
Hide resolved
_defaultValue = Objects.nonNull(enumResolver)? enumResolver.getDefaultValue() : null; | ||
} | ||
|
||
public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f, JavaType paramType, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be marked @deprecated
(will add)
Ok, I think we'd be ready, once CLA is received! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sorry for the delay, thanks for making those updates. I've sent over the CLA. |
CLA received: can proceed today with final review and merging. Thank you, @dropofwill ! |
@@ -202,6 +224,10 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx | |||
if (ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) { | |||
return null; | |||
} | |||
|
|||
if (ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll move this before as null
... plus, should only return non-null _defaultValue
, I think.
@JsonCreator
This follows the pattern for READ_UNKNOWN_ENUM_VALUES_AS_NULL from here:
https://github.com/FasterXML/jackson-databind/pull/1642/files
but for READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE
It's more tricky because it wasn't immediately apparent how to get the configured default value on the Creator impl side. I decided to try using the EnumResolver, which works, but am not sure what the repercussions of that are fully though.
It works the same way, so only IllegalArgumentExceptions will trigger default behavior, so if you have some other custom creator exception logic that will be unaffected.
There may be a better way to do this, just wanted to open this POC to see what you think. This would be useful in a couple scenarios for apps I work on, but I can work around it if this is not desired/too risky.