From c7b6c64da52761549e4585cf725e4cbf9d0464af Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 16 May 2023 12:35:44 -0700 Subject: [PATCH] Fix #3882 (JsonNode.withArray() fail) --- release-notes/VERSION-2.x | 5 +++++ .../jackson/databind/node/ArrayNode.java | 4 ++-- .../jackson/databind/node/WithPathTest.java | 21 ++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 033acad844..228df09ccb 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -4,6 +4,11 @@ Project: jackson-databind === Releases === ------------------------------------------------------------------------ +2.14.4 (not yet released) + +#3882: Error in creating nested `ArrayNode`s with `JsonNode.withArray()` + (reported by @SaiKrishna369) + 2.14.3 (05-May-2023) #3784: `PrimitiveArrayDeserializers$ByteDeser.deserialize` ignores diff --git a/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java b/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java index 0f1ac43b7e..06e2800e68 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java @@ -188,9 +188,9 @@ protected ArrayNode _withArrayAddTailElement(JsonPointer tail, boolean preferInd _withXxxSetArrayElement(index, next); return next._withArrayAddTailElement(tail, preferIndex); } - ArrayNode next = this.arrayNode(); + ObjectNode next = this.objectNode(); _withXxxSetArrayElement(index, next); - return next._withArrayAddTailElement(tail, preferIndex); + return next._withArrayAddTailProperty(tail, preferIndex); } protected void _withXxxSetArrayElement(int index, JsonNode value) { diff --git a/src/test/java/com/fasterxml/jackson/databind/node/WithPathTest.java b/src/test/java/com/fasterxml/jackson/databind/node/WithPathTest.java index f3e86cc986..73cbf1e1e2 100644 --- a/src/test/java/com/fasterxml/jackson/databind/node/WithPathTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/node/WithPathTest.java @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.JsonNode.OverwriteMode; -// for [databuind#1980] implementation +// for [databind#1980] implementation public class WithPathTest extends BaseMapTest { private final ObjectMapper MAPPER = sharedMapper(); @@ -296,4 +296,23 @@ private void _verifyArrayReplaceFail(JsonNode doc, JsonPointer ptr, OverwriteMod verifyException(e, "(mode `OverwriteMode."+mode.name()+"`)"); } } + + // [databind#3882] + public void testWithArray3882() throws Exception + { + ObjectNode root = MAPPER.createObjectNode(); + ArrayNode aN = root.withArray("/key/0/a", + JsonNode.OverwriteMode.ALL, true); + aN.add(123); + assertEquals(a2q("{'key':[{'a':[123]}]}"), + root.toString()); + + // And then the original case + root = MAPPER.createObjectNode(); + aN = root.withArray(JsonPointer.compile("/key1/array1/0/element1"), + JsonNode.OverwriteMode.ALL, true); + aN.add("v1"); + assertEquals(a2q("{'key1':{'array1':[{'element1':['v1']}]}}"), + root.toString()); + } }