Skip to content

json::diff generates incorrect patch when removing multiple array elements. #269

Description

@tmyersjstar

Removing multiple elements from an array will generate a patch which is incorrect. Here is a unit test that will fail:

// Exists in TEST_CASE("JSON patch")
SECTION("remove multiple")
{
    json doc = R"(
        {
            "arr1": [1, 2, 3, 4]
        }
    )"_json;
    json expected = R"(
        {
            "arr1": [1, 2]
        }
    )"_json;

    // check roundtrip
    CHECK(doc.patch(json::diff(doc, expected)) == expected);
}

The patch that is generated is the following:

[
    {
        "op": "remove",
        "path": "/2"
    },
    {
        "op": "remove",
        "path": "/3"
    }
]

This fails obviously because first index 2 is removed, and then index 3 is attempted to be removed, but the size is only 2 at this point:

test/src/unit.cpp:13106: FAILED:
  CHECK( doc.patch(json::diff(doc, expected)) == expected )
due to unexpected exception with message:
  array index 3 is out of range

Sorry if this is already a known issue or not the right way to report this issue. I will try to take a look into creating a fix if I have time.

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

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions