Skip to content

Removing an item from the array when option REARRANGE_ARRAYS is enabled #43

@aachekalov

Description

@aachekalov

Hi!

I use JsonDiff with REARRANGE_ARRAYS option to minimize the difference. It works fine when it comes to adding a new item to the array. But with removing it produces cascade replaces.

For example, I have 2 JSON to compare:
Original:

{
    "my_array": [
        {
            "key": "qwerty"
        },
        {
            "key": "asdfg"
        },
        {
            "key": "zxcvb"
        }
    ]
}

New:

{
    "my_array": [
        {
            "key": "asdfg"
        },
        {
            "key": "zxcvb"
        }
    ]
}

I'd like to have JSON Patch like that:

[
    {
        "op": "remove",
        "path": "/my_array/0"
    }
]

but I get:

[
    {
        "value": "qwerty",
        "op": "test",
        "path": "/my_array/0/key"
    },
    {
        "value": "asdfg",
        "op": "replace",
        "path": "/my_array/0/key"
    },
    {
        "value": "asdfg",
        "op": "test",
        "path": "/my_array/1/key"
    },
    {
        "value": "zxcvb",
        "op": "replace",
        "path": "/my_array/1/key"
    },
    {
        "op": "remove",
        "path": "/my_array/2"
    }
]

I suggest not to use array_values here:
$newRearranged = array_values($newRearranged);
because it breaks original indexes

Can you fix it please?

Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions