Skip to content
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

Merged
merged 1 commit into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
return _factory.callOnWith(_valueClass, value);
} catch (Exception e) {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ static enum EnumWithDefaultAnnoAndConstructor {
}
}

static enum StrictEnumCreator {
A, B;

@JsonCreator public static StrictEnumCreator fromId(String value) {
for (StrictEnumCreator e: values()) {
if (e.name().toLowerCase().equals(value)) return e;
}
throw new IllegalArgumentException(value);
}
}

// //

public enum AnEnum {
Expand Down Expand Up @@ -321,6 +332,16 @@ public void testAllowUnknownEnumValuesReadAsNull() throws Exception
assertNull(reader.forType(TestEnum.class).readValue(" 4343 "));
}

// Ability to ignore unknown Enum values:

public void testAllowUnknownEnumValuesReadAsNullWithCreatorMethod() throws Exception
{
// can not use shared mapper when changing configs...
ObjectReader reader = MAPPER.reader(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
assertNull(reader.forType(StrictEnumCreator.class).readValue("\"NO-SUCH-VALUE\""));
assertNull(reader.forType(StrictEnumCreator.class).readValue(" 4343 "));
}

public void testAllowUnknownEnumValuesForEnumSets() throws Exception
{
ObjectReader reader = MAPPER.reader(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
Expand Down