Skip to content

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

Merged

Conversation

dropofwill
Copy link
Contributor

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.

@dropofwill dropofwill marked this pull request as draft February 18, 2025 18:18
Copy link
Member

@JooHyukKim JooHyukKim left a 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

@cowtowncoder
Copy link
Member

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.
@dropofwill dropofwill force-pushed the unknown-with-creator-default-value branch from db9fb7f to 143b66f Compare February 25, 2025 15:10
@dropofwill dropofwill marked this pull request as ready for review February 25, 2025 15:10
@dropofwill
Copy link
Contributor Author

mvn verify is passing again for me now. Fixed NPE with null check, which will result in not finding a default enum value.

@dropofwill dropofwill changed the title DRAFT Allow default enums with @JsonCreator Allow default enums with @JsonCreator Feb 25, 2025
@cowtowncoder cowtowncoder added the cla-needed PR looks good (although may also require code review), but CLA needed from submitter label Feb 26, 2025
@cowtowncoder
Copy link
Member

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.
Easiest way is usually to print, fill & sign, scan/photo, email to cla at fasterxml dot com.

Looking forward to getting this merged, thank you!

@JooHyukKim
Copy link
Member

We have compile error now

@dropofwill
Copy link
Contributor Author

Yeah, made a typo when fixing something. I’ll fix it and sign the CLA next week.

_defaultValue = Objects.nonNull(enumResolver)? enumResolver.getDefaultValue() : null;
}

public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f, JavaType paramType,
Copy link
Member

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)

@cowtowncoder
Copy link
Member

Ok, I think we'd be ready, once CLA is received!

Copy link
Member

@cowtowncoder cowtowncoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dropofwill
Copy link
Contributor Author

Sorry for the delay, thanks for making those updates. I've sent over the CLA.

@cowtowncoder cowtowncoder added cla-received PR already covered by CLA (optional label) and removed cla-needed PR looks good (although may also require code review), but CLA needed from submitter labels Mar 13, 2025
@cowtowncoder
Copy link
Member

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)) {
Copy link
Member

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.

@cowtowncoder cowtowncoder changed the title Allow default enums with @JsonCreator Allow default enums with @JsonCreator Mar 13, 2025
@cowtowncoder cowtowncoder merged commit bd7adfb into FasterXML:2.19 Mar 14, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-received PR already covered by CLA (optional label)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants