Skip to content

Error in creating nested ArrayNodes with JsonNode.withArray() #3882

Description

@SaiKrishna369

Describe the bug
Attempt to create a new path to an ArrayNode with withArray method where the pointer would include an array index in between is failing with java.lang.UnsupportedOperationException.

Version information
2.14.2

To Reproduce

@Test
public static void test_nestedArray() {
        ObjectMapper mapper = new ObjectMapper();
        String json = "{}";
        try {
            JsonNode jsonObject = mapper.readTree(json);
            ArrayNode aN = jsonObject.withArray("/key1/array1/0/element1", JsonNode.OverwriteMode.ALL, true);
            aN.add("v1");
            aN.add("v2");
            aN.add("v3");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

Executing this method is throwing:

java.lang.UnsupportedOperationException: Cannot replace context node (of type `com.fasterxml.jackson.databind.node.ObjectNode`) using `withArray()` with  JSON Pointer '/key1/array1/0/element1'
	at com.fasterxml.jackson.databind.node.BaseJsonNode._reportWrongNodeType(BaseJsonNode.java:257)
	at com.fasterxml.jackson.databind.node.BaseJsonNode.withArray(BaseJsonNode.java:193)
	at com.fasterxml.jackson.databind.JsonNode.withArray(JsonNode.java:1319)

Expected behavior
A new path with key1 as object, containing array1 whose first element is an object containing an array with name element1 should be created, i.e. the resultant json should be{"key1": { "array1": [ {"element1": ["v1", "v2", "v3"] } ] } }.

Additional context
ArrayNode aN = jsonObject.withArray("/key1/array1/element1", JsonNode.OverwriteMode.ALL, true) and ObjectNode oN = jsonObject.withObject("/key1/array1/0/element1", JsonNode.OverwriteMode.ALL, true) are working as expected.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions