Skip to content

Commit

Permalink
Fix #2309 (NPE for Enum.toString()) as suggested by Ben A
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 10, 2019
1 parent a1ab13b commit 5d75ef3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,10 @@ Pavel Chervakov (pacher@github)
* Reported #2230: `WRITE_BIGDECIMAL_AS_PLAIN` is ignored if `@JsonFormat` is used
(2.10.0)

Ben Anderson (andersonbd1@github)
* Reported, suggested fix for #2309: READ_ENUMS_USING_TO_STRING doesn't support null values
(2.10.0)
Manuel Hegner (manuel-hegner@github)
* Suggested #2311: Unnecessary MultiView creation for property writers
(2.10.0)
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project: jackson-databind

#2237: Add "required" methods in `JsonNode`: `required(String | int)`,
`requiredAt(JsonPointer)`
#2309: READ_ENUMS_USING_TO_STRING doesn't support null values
(reported, fix suggested by Ben A)
#2331: `JsonMappingException` through nested getter with generic wildcard return type
(reported by sunchezz89@github)
#2336: `MapDeserializer` can not merge `Map`s with polymorphic values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public static CompactStringObjectMap construct(Map<String,?> all)
for (Map.Entry<String,?> entry : all.entrySet()) {
String key = entry.getKey();

// 09-Sep-2019, tatu: [databind#2309] skip `null`s if any included
if (key == null) {
continue;
}

int slot = key.hashCode() & mask;
int ix = slot+slot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ public void setupModule(SetupContext context) {
}
}

// for [databind#2309]
static enum Enum2309 {
NON_NULL("NON_NULL"),
NULL(null),
OTHER("OTHER")
;

private String value;

private Enum2309(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -285,12 +304,11 @@ public void testNumbersToEnums() throws Exception

public void testEnumsWithIndex() throws Exception
{
ObjectMapper m = jsonMapperBuilder()
.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX)
.build();
String json = m.writeValueAsString(TestEnum.RULES);
String json = MAPPER.writer()
.with(SerializationFeature.WRITE_ENUMS_USING_INDEX)
.writeValueAsString(TestEnum.RULES);
assertEquals(String.valueOf(TestEnum.RULES.ordinal()), json);
TestEnum result = m.readValue(json, TestEnum.class);
TestEnum result = MAPPER.readValue(json, TestEnum.class);
assertSame(TestEnum.RULES, result);
}

Expand Down Expand Up @@ -390,10 +408,10 @@ public void testGenericEnumDeserialization() throws Exception

// [databind#381]
public void testUnwrappedEnum() throws Exception {
final ObjectMapper mapper = jsonMapperBuilder()
.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.build();
assertEquals(TestEnum.JACKSON, mapper.readValue("[" + quote("JACKSON") + "]", TestEnum.class));
assertEquals(TestEnum.JACKSON,
MAPPER.readerFor(TestEnum.class)
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.readValue("[" + quote("JACKSON") + "]"));
}

public void testUnwrappedEnumException() throws Exception {
Expand Down Expand Up @@ -422,11 +440,10 @@ public void testIndexAsString() throws Exception
assertSame(TestEnum.values()[1], en);

// [databind#1690]: unless prevented
final ObjectMapper mapper = jsonMapperBuilder()
.disable(DeserializationFeature.ALLOW_COERCION_OF_SCALARS)
.build();
try {
en = mapper.readValue(quote("1"), TestEnum.class);
en = MAPPER.readerFor(TestEnum.class)
.without(DeserializationFeature.ALLOW_COERCION_OF_SCALARS)
.readValue(quote("1"));
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot deserialize value of type");
Expand Down Expand Up @@ -535,4 +552,13 @@ public void testExceptionFromCustomEnumKeyDeserializer() throws Exception {
assertTrue(e.getMessage().contains("Undefined AnEnum"));
}
}

// [databind#2309]
public void testEnumToStringNull2309() throws Exception
{
Enum2309 value = MAPPER.readerFor(Enum2309.class)
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.readValue(quote("NON_NULL"));
assertEquals(Enum2309.NON_NULL, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ static enum OK {
OK(String key) { this.key = key; }
}

@SuppressWarnings("rawtypes")
static class LowerCasingEnumSerializer extends StdSerializer<Enum>
{
public LowerCasingEnumSerializer() { super(Enum.class); }
Expand Down

0 comments on commit 5d75ef3

Please sign in to comment.