Skip to content

Commit b0f6eb9

Browse files
committed
Fix #788 (JsonPointer.empty() should not match any index or property)
1 parent fa93b47 commit b0f6eb9

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ JSON library.
4545
#774: Add a feature to allow leading plus sign
4646
(`JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS`)
4747
(contributed by @pjfanning)
48+
#788: `JsonPointer.empty()` should NOT indicate match of a property
49+
with key of ""
4850

4951
2.13.3 (14-May-2022)
5052

src/main/java/com/fasterxml/jackson/core/JsonPointer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public class JsonPointer implements Serializable
8484
*/
8585
protected JsonPointer() {
8686
_nextSegment = null;
87-
_matchingPropertyName = "";
87+
// [core#788]: must be `null` to distinguish from Property with "" as key
88+
_matchingPropertyName = null;
8889
_matchingElementIndex = -1;
8990
_asString = "";
9091
}

src/test/java/com/fasterxml/jackson/core/TestJsonPointer.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testSimplePath() throws Exception
3838
assertTrue(ptr.matches());
3939
assertNull(ptr.tail());
4040
assertNull(ptr.head());
41-
assertEquals("", ptr.getMatchingProperty());
41+
assertNull(ptr.getMatchingProperty());
4242
assertEquals(-1, ptr.getMatchingIndex());
4343
}
4444

@@ -80,21 +80,37 @@ public void testLast()
8080
assertEquals("name", leaf.getMatchingProperty());
8181
}
8282

83-
public void testEmpty()
83+
public void testEmptyPointer()
8484
{
8585
assertSame(JsonPointer.EMPTY, JsonPointer.empty());
8686
assertSame(JsonPointer.EMPTY, JsonPointer.compile(""));
87+
final JsonPointer empty = JsonPointer.empty();
88+
assertEquals("", empty.toString());
89+
90+
// As per [core#788], should NOT match Property with "empty String"
91+
assertFalse(empty.mayMatchProperty());
92+
assertFalse(empty.mayMatchElement());
93+
assertEquals(-1, empty.getMatchingIndex());
94+
assertNull(empty.getMatchingProperty());
8795
}
8896

89-
public void testEmptyName()
97+
public void testPointerWithEmptyPropertyName()
9098
{
9199
// note: this is acceptable, to match property in '{"":3}', for example
92100
// and NOT same as what empty point, "", is.
93101
JsonPointer ptr = JsonPointer.compile("/");
94102
assertNotNull(ptr);
95103
assertNotSame(JsonPointer.EMPTY, ptr);
96-
104+
97105
assertEquals("/", ptr.toString());
106+
assertTrue(ptr.mayMatchProperty());
107+
assertFalse(ptr.mayMatchElement());
108+
assertEquals(-1, ptr.getMatchingIndex());
109+
assertEquals("", ptr.getMatchingProperty());
110+
assertTrue(ptr.matchesProperty(""));
111+
assertFalse(ptr.matchesElement(0));
112+
assertFalse(ptr.matchesElement(-1));
113+
assertFalse(ptr.matchesProperty("1"));
98114
}
99115

100116
// mostly for test coverage, really...

0 commit comments

Comments
 (0)