-
-
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
Fix for the bug? that READ_ENUMS_USING_TO_STRING doesn't support null… #2307
Conversation
I think it'd be best to file an issue first explaining what exactly is the problem, since that's bit unclear to me. That would make sure I understand problem being fixed (I know there's a unit test, which is good, but it shows what happens more than why), and then that the fix works for use case as well as overall design. |
Thanks for the quick response, cowtowncoder. I created an issue here: |
Ok. My point is this: if you indicate that the value of However... I am not sure I accept the premise that I could easily add an explicit exception to throw in this case, however, to indicate that it is invalid usage, if that would help. |
@@ -76,7 +76,7 @@ public static EnumResolver constructUsingToString(Class<Enum<?>> enumCls, | |||
// from last to first, so that in case of duplicate values, first wins | |||
for (int i = enumValues.length; --i >= 0; ) { | |||
Enum<?> e = enumValues[i]; | |||
map.put(e.toString(), e); | |||
map.put(e.toString() + "", e); |
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.
or maybe it should continue if e is null and not add anything to the map
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 think I like "skip nulls" approach: will use that.
public void testEnumWithToStringNullValue() { | ||
ObjectMapper mapper = new ObjectMapper().enable(READ_ENUMS_USING_TO_STRING); | ||
ObjectNode on = mapper.createObjectNode(); | ||
on.put("enumWithToStringNullValue", "NON_NULL"); |
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.
w/out the supplied "fix", this throws an exception even through I'm not mapping the NULL enum.
I think my expectation would probably be that null should map to the enum value EnumCustomerFileSource.NULL. However, given the weirdness of a null returned from toString (especially from an enum), I wouldn't be set on any expected behavior here. In fact, my code submission doesn't map null to the enum NULL - it only maps the string "null" to NULL, which isn't really correct, but at least lets me use jackson with this facebook api. It's a shallow solution to a problem that probably doesn't warrant a deeper solution. My main goal here was to allow READ_ENUMS_USING_TO_STRING to work in cases like this for the non-null values. As it is, simply the fact that one of the enums returns null means that an exception is thrown every time you attempt to map any enum value (including non-nulls) using READ_ENUMS_USING_TO_STRING. This facebook code is in github, so I'll comment over there as well. |
Thank you for contributing this -- I finally found time to get back to this, and I think I agree with "skip the nulls" approach, as such entries seem wrong to me, and I think my preferences would be in order of
and (1) seems best just because one does not always have control over types, so bit of defensiveness seems appropriate. I will actually make changes to 2.10, mostly because 2.9 branch is almost closed and I want to minimize changes there. I will use test included, and do simple patch -- thank you again for suggesting it. |
Thanks, @cowtowncoder ! |
… values.