json_diff_rfc6902 icon indicating copy to clipboard operation
json_diff_rfc6902 copied to clipboard

diff calculated wrong

Open nebulaszlonemethi opened this issue 8 years ago • 1 comments

In the simple example, a weird "copy" patch is calculated as a diff what is wrong. Check it on codepen or here:

var a = {
  choices: [
    {id: "2"}
  ]
};
var b = {
  choices: [
    {id: "2"}
  ],
  description: "2"
};

var diff = jdr.diff(a, b);
jdr.apply(a, diff, {OBJ_COM:false});

console.info("diff:", diff); // results [{op: "copy", path: "/description", from: "/choices/0"}]
console.info("this should be b:", a); // results something else than b

nebulaszlonemethi avatar Nov 10 '17 13:11 nebulaszlonemethi

Ok, i think the problem is here (at least for my case): when you are building the unchanged array in transformArray(), you store hashes:

unchanged.push( path + '/' + y_sorted[j].index + "=" + JSON.stringify(x_sorted[i].hash));

Then, when you are searching in the unchanged array in generateObjectDiff(), you search for a simple value, not for hash:

var pointer = unchangedArea.findValueInUnchanged(JSON.stringify(newVal), unchanged);

so the pointer can be calculated wrong.

nebulaszlonemethi avatar Nov 15 '17 09:11 nebulaszlonemethi