chbrown / rfc6902

Complete implementation of RFC6902 in TypeScript
https://chbrown.github.io/rfc6902/
322 stars 39 forks source link

'MissingError' when patching array path with duplicate values #3

Closed dhritzkiv closed 9 years ago

dhritzkiv commented 9 years ago

Hard to explain, so here's a simple test case:

const rfc6902 = require("rfc6902");

const obj1 = {
    arr: ["1", "2", "2"]//duplicate values on the end
};

const obj2 = {
    arr: ["1"]
};

const diff = rfc6902.createPatch(obj1, obj2);
/*
`diff` is equal to:
     [ 
         { op: 'remove', path: '/arr/1' },
         { op: 'remove', path: '/arr/2' }
     ]
but should be
     [ 
         { op: 'remove', path: '/arr/1' },
         { op: 'remove', path: '/arr/1' }
     ]
*/

const obj1Clone = {//clone of `obj1`
    arr: ["1", "2", "2"]
};

const result = rfc6902.applyPatch(obj1Clone, diff);
/*
`result` is equal to:
    [
        null,
        { [MissingError] name: 'MissingError', path: '/arr/2' }
    ]
*/

/*
`obj1Clone` is equal to:
    {
        arr: ["1", "2"]
    }
*/

It seems somewhere, duplicate values are being filtered out, and as a result, the path is missing/no longer valid.

Any idea what might be causing this?

dhritzkiv commented 9 years ago

:clap: thank you!

chbrown commented 9 years ago

Rebuilding dist.js and co. now — don't grab the latest quite yet.

chbrown commented 9 years ago

Okay, fixed in v1.0.5. I simply reverse the order of operations produced by diffArrays, since they are generated from lower to higher array indices, which certainly resolves this issue, but I would be glad for any attempts to find an edge case where order of operations is still broken.