Skip to content

Commit c7b6c64

Browse files
committed
Fix #3882 (JsonNode.withArray() fail)
1 parent d47d1b6 commit c7b6c64

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

release-notes/VERSION-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.14.4 (not yet released)
8+
9+
#3882: Error in creating nested `ArrayNode`s with `JsonNode.withArray()`
10+
(reported by @SaiKrishna369)
11+
712
2.14.3 (05-May-2023)
813

914
#3784: `PrimitiveArrayDeserializers$ByteDeser.deserialize` ignores

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ protected ArrayNode _withArrayAddTailElement(JsonPointer tail, boolean preferInd
188188
_withXxxSetArrayElement(index, next);
189189
return next._withArrayAddTailElement(tail, preferIndex);
190190
}
191-
ArrayNode next = this.arrayNode();
191+
ObjectNode next = this.objectNode();
192192
_withXxxSetArrayElement(index, next);
193-
return next._withArrayAddTailElement(tail, preferIndex);
193+
return next._withArrayAddTailProperty(tail, preferIndex);
194194
}
195195

196196
protected void _withXxxSetArrayElement(int index, JsonNode value) {

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.fasterxml.jackson.databind.*;
55
import com.fasterxml.jackson.databind.JsonNode.OverwriteMode;
66

7-
// for [databuind#1980] implementation
7+
// for [databind#1980] implementation
88
public class WithPathTest extends BaseMapTest
99
{
1010
private final ObjectMapper MAPPER = sharedMapper();
@@ -296,4 +296,23 @@ private void _verifyArrayReplaceFail(JsonNode doc, JsonPointer ptr, OverwriteMod
296296
verifyException(e, "(mode `OverwriteMode."+mode.name()+"`)");
297297
}
298298
}
299+
300+
// [databind#3882]
301+
public void testWithArray3882() throws Exception
302+
{
303+
ObjectNode root = MAPPER.createObjectNode();
304+
ArrayNode aN = root.withArray("/key/0/a",
305+
JsonNode.OverwriteMode.ALL, true);
306+
aN.add(123);
307+
assertEquals(a2q("{'key':[{'a':[123]}]}"),
308+
root.toString());
309+
310+
// And then the original case
311+
root = MAPPER.createObjectNode();
312+
aN = root.withArray(JsonPointer.compile("/key1/array1/0/element1"),
313+
JsonNode.OverwriteMode.ALL, true);
314+
aN.add("v1");
315+
assertEquals(a2q("{'key1':{'array1':[{'element1':['v1']}]}}"),
316+
root.toString());
317+
}
299318
}

0 commit comments

Comments
 (0)