-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add enum features into JsonFormat.Feature #3731
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.fasterxml.jackson.databind.deser.enums; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import java.io.IOException; | ||
import java.util.EnumSet; | ||
|
||
|
@@ -36,6 +37,25 @@ protected static class StrictCaseBean { | |
public TestEnum value; | ||
} | ||
|
||
protected static class DefaultEnumBean { | ||
@JsonFormat(with={ JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE }) | ||
public MyEnum2352_3 value; | ||
} | ||
|
||
protected static class DefaultEnumSetBean { | ||
@JsonFormat(with={ JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE }) | ||
public EnumSet<MyEnum2352_3> value; | ||
} | ||
|
||
protected static class NullValueEnumBean { | ||
@JsonFormat(with={ JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL }) | ||
public MyEnum2352_3 value; | ||
} | ||
|
||
protected static class NullEnumSetBean { | ||
@JsonFormat(with={ JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL }) | ||
public EnumSet<MyEnum2352_3> value; | ||
} | ||
|
||
// for [databind#2352]: Support aliases on enum values | ||
enum MyEnum2352_1 { | ||
|
@@ -213,4 +233,56 @@ public void testEnumWithAliasAndDefaultForUnknownValueEnabled() throws Exception | |
MyEnum2352_3 multipleAliases2 = reader.readValue(q("multipleAliases2")); | ||
assertEquals(MyEnum2352_3.C, multipleAliases2); | ||
} | ||
|
||
public void testEnumWithDefaultForUnknownValueEnabled() throws Exception { | ||
final String JSON = a2q("{'value':'ok'}"); | ||
|
||
DefaultEnumBean pojo = READER_DEFAULT.forType(DefaultEnumBean.class) | ||
.readValue(JSON); | ||
assertEquals(MyEnum2352_3.B, pojo.value); | ||
// including disabling acceptance | ||
try { | ||
READER_DEFAULT.forType(StrictCaseBean.class) | ||
.readValue(JSON); | ||
fail("Should not pass"); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, "not one of the values accepted for Enum class"); | ||
verifyException(e, "[JACKSON, OK, RULES]"); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to testing single I don't remember if things might just work due to delegation, but it might be necessary to change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the test cases for EnumSet. Please have look if behaviour is expected.Thanks. |
||
public void testEnumWithNullForUnknownValueEnabled() throws Exception { | ||
final String JSON = a2q("{'value':'ok'}"); | ||
|
||
NullValueEnumBean pojo = READER_DEFAULT.forType(NullValueEnumBean.class) | ||
.readValue(JSON); | ||
assertNull(pojo.value); | ||
// including disabling acceptance | ||
try { | ||
READER_DEFAULT.forType(StrictCaseBean.class) | ||
.readValue(JSON); | ||
fail("Should not pass"); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, "not one of the values accepted for Enum class"); | ||
verifyException(e, "[JACKSON, OK, RULES]"); | ||
} | ||
} | ||
|
||
public void testEnumWithDefaultForUnknownValueEnumSet() throws Exception { | ||
final String JSON = a2q("{'value':['ok']}"); | ||
|
||
DefaultEnumSetBean pojo = READER_DEFAULT.forType(DefaultEnumSetBean.class) | ||
.readValue(JSON); | ||
assertEquals(1, pojo.value.size()); | ||
assertTrue(pojo.value.contains(MyEnum2352_3.B)); | ||
} | ||
|
||
public void testEnumWithNullForUnknownValueEnumSet() throws Exception { | ||
final String JSON = a2q("{'value':['ok','B']}"); | ||
|
||
NullEnumSetBean pojo = READER_DEFAULT.forType(NullEnumSetBean.class) | ||
.readValue(JSON); | ||
assertEquals(1, pojo.value.size()); | ||
assertTrue(pojo.value.contains(MyEnum2352_3.B)); | ||
} | ||
} |
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.
Will need to leave old constructor for backwards-compatibility; I can add it after merging.