swaggest / json-diff

JSON diff/rearrange/patch/pointer library for PHP
MIT License
220 stars 30 forks source link

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

Closed aachekalov closed 3 years ago

aachekalov commented 3 years ago

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!

vearutop commented 3 years ago

Thank you for the issue and suggestion!

Please try v3.8.3.

aachekalov commented 3 years ago

Works fine! Thanx!