Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] fastJsonPatch.compare with array generates wrong patches #275

Open
Jeremias-Nater opened this issue May 25, 2021 · 1 comment
Open

Comments

@Jeremias-Nater
Copy link

Jeremias-Nater commented May 25, 2021

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'")
}
@Jeremias-Nater Jeremias-Nater changed the title Compare with array generates wrong patches [BUG] Compare with array generates wrong patches May 25, 2021
@Jeremias-Nater Jeremias-Nater changed the title [BUG] Compare with array generates wrong patches [BUG] fastJsonPatch.compare with array generates wrong patches May 25, 2021
@pixelspark
Copy link

Could it be that the parameters to compare are in the wrong order? (i.e. compare(originalObject, testObject)?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants