diff --git a/src/main/java/com/fasterxml/jackson/databind/node/BigIntegerNode.java b/src/main/java/com/fasterxml/jackson/databind/node/BigIntegerNode.java index 00e4c9eba7..aba22df314 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/BigIntegerNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/BigIntegerNode.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Objects; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.SerializerProvider; @@ -113,11 +114,12 @@ public boolean equals(Object o) if (!(o instanceof BigIntegerNode)) { return false; } - return ((BigIntegerNode) o)._value.equals(_value); + BigIntegerNode otherNode = (BigIntegerNode) o; + return Objects.equals(otherNode._value, _value); } @Override public int hashCode() { - return _value.hashCode(); + return Objects.hashCode(_value); } } diff --git a/src/main/java/com/fasterxml/jackson/databind/node/DecimalNode.java b/src/main/java/com/fasterxml/jackson/databind/node/DecimalNode.java index 7c4365f0a4..ca9c85b0ee 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/DecimalNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/DecimalNode.java @@ -126,11 +126,23 @@ public boolean equals(Object o) if (o == this) return true; if (o == null) return false; if (o instanceof DecimalNode) { - return ((DecimalNode) o)._value.compareTo(_value) == 0; + DecimalNode otherNode = (DecimalNode) o; + if (otherNode._value == null) { + return _value == null; + } else if (_value == null) { + return false; + } + return otherNode._value.compareTo(_value) == 0; } return false; } @Override - public int hashCode() { return Double.valueOf(doubleValue()).hashCode(); } + public int hashCode() { + if (_value == null) { + // we need a stable hash code for _value == null + return 0; + } + return Double.valueOf(doubleValue()).hashCode(); + } } diff --git a/src/test/java/com/fasterxml/jackson/databind/node/NullDataEqualsTest.java b/src/test/java/com/fasterxml/jackson/databind/node/NullDataEqualsTest.java new file mode 100644 index 0000000000..a9f51a6bc7 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/databind/node/NullDataEqualsTest.java @@ -0,0 +1,35 @@ +package com.fasterxml.jackson.databind.node; + +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class NullDataEqualsTest { + @Test + void testNullBinaryNode() { + assertEquals(new BinaryNode(null), new BinaryNode(null)); + assertNotEquals(new BinaryNode(new byte[8]), new BinaryNode(null)); + assertNotEquals(new BinaryNode(null), new BinaryNode(new byte[8])); + assertEquals(-1, new BinaryNode(null).hashCode()); + } + + @Test + void testNullBigIntegerNode() { + assertEquals(new BigIntegerNode(null), new BigIntegerNode(null)); + assertNotEquals(new BigIntegerNode(BigInteger.ZERO), new BigIntegerNode(null)); + assertNotEquals(new BigIntegerNode(null), new BigIntegerNode(BigInteger.ZERO)); + assertEquals(0, new BigIntegerNode(null).hashCode()); + } + + @Test + void testNullDecimalNode() { + assertEquals(new DecimalNode(null), new DecimalNode(null)); + assertNotEquals(new DecimalNode(BigDecimal.ZERO), new DecimalNode(null)); + assertNotEquals( new DecimalNode(null), new DecimalNode(BigDecimal.ZERO)); + assertEquals(0, new DecimalNode(null).hashCode()); + } +}