Skip to content

Commit 9db1adb

Browse files
committed
Fix #2433
1 parent 8f0b80d commit 9db1adb

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,3 +914,7 @@ Vladimir Tsanev (tsachev@github)
914914
* Contributed #2415: Builder-based POJO deserializer should pass builder instance, not type,
915915
to `handleUnknownVanilla()` to fix earlier #822
916916
(2.10.0)
917+
918+
David Becker (dsbecker@github)
919+
* Suggested #2433: Improve `NullNode.equals()`
920+
(2.10.0)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Project: jackson-databind
2929
#2425: Add global config override setting for `@JsonFormat.lenient()`
3030
#2428: Use "activateDefaultTyping" over "enableDefaultTyping" in 2.10 with new methods
3131
#2430: Change `ObjectMapper.valueToTree()` to convert `null` to `NullNode`
32+
#2433: Improve `NullNode.equals()`
33+
(suggested by David B)
3234

3335
2.10.0.pr1 (19-Jul-2019)
3436

src/main/java/com/fasterxml/jackson/databind/JsonNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ protected <T extends JsonNode> T _this() {
10771077

10781078
/**
10791079
* Helper method that throws {@link IllegalArgumentException} as a result of
1080-
* violating "required-constraint" for this node (for {@link #require() or related
1080+
* violating "required-constraint" for this node (for {@link #required} or related
10811081
* methods).
10821082
*/
10831083
protected <T> T _reportRequiredViolation(String msgTemplate, Object...args) {

src/main/java/com/fasterxml/jackson/databind/node/NullNode.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* This singleton value class is used to contain explicit JSON null
1212
* value.
1313
*/
14-
public final class NullNode
14+
public class NullNode
1515
extends ValueNode
1616
{
1717
// // Just need a fly-weight singleton
@@ -67,7 +67,11 @@ public final void serialize(JsonGenerator g, SerializerProvider provider)
6767

6868
@Override
6969
public boolean equals(Object o) {
70-
return (o == this);
70+
// 29-Aug-2019, tatu: [databind#2433] Since custom sub-classes are allowed (bad idea probably),
71+
// need to do better comparison
72+
if (o == this) return true;
73+
if (!(o instanceof NullNode)) return false;
74+
return ((NullNode) o).isNull();
7175
}
7276

7377
@Override

src/test/java/com/fasterxml/jackson/databind/node/TestNullNode.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ final static class CovarianceBean {
1717
public void setArray(ArrayNode n) { _array = n; }
1818
}
1919

20+
@SuppressWarnings("serial")
21+
static class MyNull extends NullNode { }
22+
23+
private final ObjectMapper MAPPER = sharedMapper();
24+
2025
public void testBasicsWithNullNode() throws Exception
2126
{
2227
// Let's use something that doesn't add much beyond JsonNode base
@@ -59,7 +64,7 @@ public void testBasicsWithNullNode() throws Exception
5964
public void testNullHandling() throws Exception
6065
{
6166
// First, a stand-alone null
62-
JsonNode n = objectReader().readTree("null");
67+
JsonNode n = MAPPER.readTree("null");
6368
assertNotNull(n);
6469
assertTrue(n.isNull());
6570
assertFalse(n.isNumber());
@@ -81,21 +86,32 @@ public void testNullHandling() throws Exception
8186

8287
public void testNullSerialization() throws Exception
8388
{
84-
ObjectMapper mapper = new ObjectMapper();
8589
StringWriter sw = new StringWriter();
86-
mapper.writeValue(sw, NullNode.instance);
90+
MAPPER.writeValue(sw, NullNode.instance);
8791
assertEquals("null", sw.toString());
8892
}
8993

9094
public void testNullHandlingCovariance() throws Exception
9195
{
9296
String JSON = "{\"object\" : null, \"array\" : null }";
93-
CovarianceBean bean = objectMapper().readValue(JSON, CovarianceBean.class);
97+
CovarianceBean bean = MAPPER.readValue(JSON, CovarianceBean.class);
9498

9599
ObjectNode on = bean._object;
96100
assertNull(on);
97101

98102
ArrayNode an = bean._array;
99103
assertNull(an);
100104
}
105+
106+
@SuppressWarnings("unlikely-arg-type")
107+
public void testNullEquality() throws Exception
108+
{
109+
JsonNode n = MAPPER.nullNode();
110+
assertTrue(n.isNull());
111+
assertEquals(n, new MyNull());
112+
assertEquals(new MyNull(), n);
113+
114+
assertFalse(n.equals(null));
115+
assertFalse(n.equals("foo"));
116+
}
101117
}

0 commit comments

Comments
 (0)