From 4eb45395b74f68c16f51111e4214d284fbc46b66 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 14 Dec 2023 20:12:50 -0800 Subject: [PATCH] Backport #1168 in 2.16(.1) --- release-notes/VERSION-2.x | 2 ++ .../fasterxml/jackson/core/JsonPointer.java | 8 +++--- .../core/jsonptr/JsonPointer1168Test.java | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/fasterxml/jackson/core/jsonptr/JsonPointer1168Test.java diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index f2f72615a4..d2fa652275 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -20,6 +20,8 @@ a pure JSON library. (reported by @TimSchweers) #1161: NPE in "FastDoubleParser", method "JavaBigDecimalParser.parseBigDecimal()" (contributed by @pjfanning) +#1168: `JsonPointer.append(JsonPointer.tail())` includes the original pointer + (contributed by Robert E) 2.16.0 (15-Nov-2023) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonPointer.java b/src/main/java/com/fasterxml/jackson/core/JsonPointer.java index 17c0cbcb08..9054f18d61 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonPointer.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonPointer.java @@ -374,12 +374,12 @@ public JsonPointer append(JsonPointer tail) { // 21-Mar-2017, tatu: Not superbly efficient; could probably improve by not concatenating, // re-decoding -- by stitching together segments -- but for now should be fine. - String currentJsonPointer = _asString; + String currentJsonPointer = toString(); if (currentJsonPointer.endsWith("/")) { //removes final slash currentJsonPointer = currentJsonPointer.substring(0, currentJsonPointer.length()-1); } - return compile(currentJsonPointer + tail._asString); + return compile(currentJsonPointer + tail.toString()); } /** @@ -408,7 +408,7 @@ public JsonPointer appendProperty(String property) { if (property.charAt(0) != SEPARATOR) { property = SEPARATOR + property; } - String currentJsonPointer = _asString; + String currentJsonPointer = toString(); if (currentJsonPointer.endsWith("/")) { //removes final slash currentJsonPointer = currentJsonPointer.substring(0, currentJsonPointer.length()-1); @@ -432,7 +432,7 @@ public JsonPointer appendIndex(int index) { if (index < 0) { throw new IllegalArgumentException("Negative index cannot be appended"); } - String currentJsonPointer = _asString; + String currentJsonPointer = toString(); if (currentJsonPointer.endsWith("/")) { //removes final slash currentJsonPointer = currentJsonPointer.substring(0, currentJsonPointer.length()-1); diff --git a/src/test/java/com/fasterxml/jackson/core/jsonptr/JsonPointer1168Test.java b/src/test/java/com/fasterxml/jackson/core/jsonptr/JsonPointer1168Test.java new file mode 100644 index 0000000000..cb4f3f89a5 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/core/jsonptr/JsonPointer1168Test.java @@ -0,0 +1,26 @@ +package com.fasterxml.jackson.core.jsonptr; + +import com.fasterxml.jackson.core.BaseTest; +import com.fasterxml.jackson.core.JsonPointer; + +public class JsonPointer1168Test extends BaseTest +{ + // [core#1168] + public void testAppendWithTail() + { + JsonPointer original = JsonPointer.compile("/a1/b/c"); + JsonPointer tailPointer = original.tail(); + assertEquals("/b/c", tailPointer.toString()); + + JsonPointer other = JsonPointer.compile("/a2"); + assertEquals("/a2", other.toString()); + + assertEquals("/a2/b/c", other.append(tailPointer).toString()); + + // And the other way around too + assertEquals("/b/c/a2", tailPointer.append(other).toString()); + + // And with `appendProperty()` + assertEquals("/b/c/xyz", tailPointer.appendProperty("xyz").toString()); + } +}