Skip to content

configOverride.setMergeable(false) not supported by ArrayNodeΒ #3338

@ejl888

Description

@ejl888

Describe the bug
When updating a JsonNode containing an array. An update will always add the data to the array, even when the
the mapper is configured to not merge Object arrays, ArrayNode, and Collections.

Version information
2.13.0

To Reproduce

Below a Junit5 test

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;

class ObjectMapperMergeTest {

    @Test
    void itShouldReplaceArrayNodeAndNotAddWhenConfiguredSo() throws IOException {
        // given
        ObjectMapper testSubject = new ObjectMapper();
        testSubject.configOverride(Collection.class).setMergeable(false);
        testSubject.configOverride(ArrayNode.class).setMergeable(false);
        testSubject.configOverride(ArrayList.class).setMergeable(false);
        testSubject.configOverride(Object[].class).setMergeable(false);

        JsonNode mergeTarget = testSubject.readTree("{"
                + "\"array\": [\"Mr.\", \"Ms.\" ],"
                + "\"number\": 888"
                + "}");

        // when
        JsonNode updateNode = testSubject.readTree("{"
                + "\"array\": [\"Mister\", \"Miss\" ]"
                + "}");
        JsonNode patched = testSubject.readerForUpdating(mergeTarget).readValue(updateNode);

        // then
        assertThatJson(patched).node("number").isIntegralNumber().isEqualTo(888);
       // fails because array contains "Mr." and "Ms." also
        assertThatJson(patched).node("array").isArray().containsExactly("Mister", "Miss");
    }
}

Expected behavior
I would like to have the mapper to replace the array, because now it will never replace it.

Additional context
Related issue JsonMerge got unexpected result for List #2315

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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions