Skip to content

ArrayNode.addAll() adds raw null values which cause NPE on deepCopy() and toString() #2442

@heshamMassoud

Description

@heshamMassoud

Version: 2.9.9

When I create an ArrayNode and use ArrayNode#addAll to add elements containing a raw null element as follows:

final List<ObjectNode> nodes = Arrays.asList(null, JsonNodeFactory.instance.objectNode());
final ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();
arrayNode.addAll(nodes);

Calling ArrayNode#deepCopy and ArrayNode#toString on such arrayNode will throw NullPointerExceptions.

However, if we add the nodes using ArrayNode#add as follows:

final ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();
arrayNode.add((JsonNode) null);
arrayNode.add(JsonNodeFactory.instance.objectNode());

Calling ArrayNode#deepCopy and ArrayNode#toString on such arrayNode will NOT throw any NullPointerExceptions.

This is because ArrayNode#add converts any raw null to a NullNode object:

public ArrayNode add(JsonNode value)
{
if (value == null) { // let's not store 'raw' nulls but nodes
value = nullNode();
}
_add(value);
return this;
}

But ArrayNode#addAll does not. But It should, otherwise how does one deepCopy an arrayNode that contains null raw elements?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions