swaggest / json-diff

JSON diff/rearrange/patch/pointer library for PHP
MIT License
218 stars 29 forks source link

getPatch with REARRANGE_ARRAYS is not returning the correct paths when items are removed from an array #51

Open edknittel opened 2 years ago

edknittel commented 2 years ago

TEST

$previous = ['Apples', 'Oranges', 'Bananas', 'Grapes'];
$current = ['Apples', 'Grapes'];
$JsonDiff = new JsonDiff($previous, $current, JsonDiff::REARRANGE_ARRAYS);
$patch = json_decode(json_encode($JsonDiff->getPatch()), true) ?: [];
print_r($patch);

EXPECTED RESULT

When the [1] and [2] position items in the array are successfully removed the patch accurately reflects this Array ( [0] => Array ( [op] => remove [path] => /1 ) [1] => Array ( [op] => remove [path] => /2) )

ACTUAL RESULT

When [1] and [2] position items in the array are successfully removed the patch only reflects the first item and it's repeated twice. There is no reference to the second item anywhere in the patch response Array ( [0] => Array ( [op] => remove [path] => /1 ) [1] => Array ( [op] => remove [path] => /1) )

edknittel commented 2 years ago

Looks like this issue was fixed in this commit but for some reason it's not made it to master https://github.com/swaggest/json-diff/commit/74a30409254895d216ab27d10bba166558604a3d