json-diff icon indicating copy to clipboard operation
json-diff copied to clipboard

Incorrect paths for Remove operation

Open tg666 opened this issue 4 months ago • 0 comments

If multiple items are removed from an array, the individual changes have incorrect path.

<?php

use Swaggest\JsonDiff\JsonDiff;
use Swaggest\JsonDiff\JsonPatch;

$original = (object) [
    'letters' => ['A', 'B', 'C', 'D'],
];

$modified = (object) [
    'letters' => ['A', 'B'],
];

$jsonDiff = new JsonDiff(
    original: $original,
    new: $modified,
);

$changeset = JsonPatch::export($jsonDiff->getPatch());

var_dump($changeset);

The result (both changes have the path /letters/2):

array(2) {
  [0]=>
  object(stdClass)#802 (2) {
    ["op"]=>
    string(6) "remove"
    ["path"]=>
    string(10) "/letters/2"
  }
  [1]=>
  object(stdClass)#801 (2) {
    ["op"]=>
    string(6) "remove"
    ["path"]=>
    string(10) "/letters/2"
  }
}

Probably related to https://github.com/swaggest/json-diff/pull/70

tg666 avatar Sep 07 '25 02:09 tg666