[BUG] fastJsonPatch.compare with array generates wrong patches
Jeremias-Nater opened this issue · comments
error scenario
The update on field "FirstName" is missing in this example:
const originalObject = JSON.parse(JSON.stringify(testObject));
testObject.array[1].firstName = 'NEW FIRSTNAME'; //changes second element
testObject.array.splice(0, 1); //removes first element
const patchesCompare = fastJsonPatch.compare(testObject, originalObject); // misses firstName Update value
Resulting Patches:
[
{
"op": "replace",
"path": "/array/0/firstName",
"value": "Peter"
},
{
"op": "replace",
"path": "/array/0/id",
"value": "123zgasufczg2"
},
{
"op": "add",
"path": "/array/1",
"value": {
"id": "sad2241f12f12",
"firstName": "Fritz"
}
}
]
issue demo on runkit
https://runkit.com/embed/rn5u4rs7ypup
var fastJsonPatch = require("fast-json-patch")
const testObject = {
array: [
{
id: '123zgasufczg2',
firstName: 'Peter'
},
{
id: 'sad2241f12f12',
firstName: 'Fritz'
},
]
}
const originalObject = JSON.parse(JSON.stringify(testObject));
// observe
const patchObserver = fastJsonPatch.observe(testObject);
testObject.array[1].firstName = 'NEW FIRSTNAME'; //changes second element
testObject.array.splice(0, 1); //removes first element
// unobserve
const patchesObserve = fastJsonPatch.generate( patchObserver );
fastJsonPatch.unobserve( testObject, patchObserver );
// compare
const patchesCompare = fastJsonPatch.compare(testObject, originalObject);
// result, error
console.log("patches Observe:", patchesObserve);
console.log("patches Compare:", patchesCompare);
if (!patchesCompare.some(patch => patch.value == "NEW FIRSTNAME")) {
console.log("this is the problem");
throw new Error("COMPARE PATCH is missing change of 'NEW FIRSTNAME'")
}
Could it be that the parameters to compare
are in the wrong order? (i.e. compare(originalObject, testObject)
?)