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 052be02 commit 5dfd86f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 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
15 changes: 10 additions & 5 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ Project: jackson-databind
=== Releases ===
------------------------------------------------------------------------

2.10.0-final (not yet released)

#2309: READ_ENUMS_USING_TO_STRING doesn't support null values
(reported, fix suggested by Ben A)
#2442: `ArrayNode.addAll()` adds raw `null` values which cause NPE on `deepCopy()`
and `toString()`
(reported, fix contributed by Hesham M)
#2446: Java 11: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
(reported by David C)
2.10.0.pr2 (31-Aug-2019)
#2237: Add "required" methods in `JsonNode`: `required(String | int)`,
Expand Down Expand Up @@ -34,11 +44,6 @@ Project: jackson-databind
#2430: Change `ObjectMapper.valueToTree()` to convert `null` to `NullNode`
#2433: Improve `NullNode.equals()`
(suggested by David B)
#2442: `ArrayNode.addAll()` adds raw `null` values which cause NPE on `deepCopy()`
and `toString()`
(reported, fix contributed by Hesham M)
#2446: Java 11: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
(reported by David C)

2.10.0.pr1 (19-Jul-2019)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public static <T> CompactStringObjectMap construct(Map<String,T> all)
for (Map.Entry<String,T> 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 @@ -7,12 +7,14 @@
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

@SuppressWarnings("serial")
Expand Down Expand Up @@ -183,6 +185,25 @@ public static ObjectMapper setupObjectMapper(ObjectMapper mapper) {
}
}

// 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 @@ -290,11 +311,11 @@ public void testNumbersToEnums() throws Exception

public void testEnumsWithIndex() throws Exception
{
ObjectMapper m = new ObjectMapper();
m.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
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 @@ -391,10 +412,10 @@ public void testGenericEnumDeserialization() throws Exception

// [databind#381]
public void testUnwrappedEnum() throws Exception {
final ObjectMapper mapper = newJsonMapper();
mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);

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 +443,12 @@ public void testIndexAsString() throws Exception
assertSame(TestEnum.values()[1], en);

// [databind#1690]: unless prevented
final ObjectMapper mapper = jsonMapperBuilder()
.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
.build();
try {
en = mapper.readValue(quote("1"), TestEnum.class);
en = JsonMapper.builder()
.configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, false)
.build()
.readerFor(TestEnum.class)
.readValue(quote("1"));
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot deserialize value of type");
Expand Down Expand Up @@ -528,4 +550,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);
}
}

0 comments on commit 5dfd86f

Please sign in to comment.