Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.82k stars 215 forks source link

Check if the two operands are of the same type before interpreting a missing prop as a remove op #205

Closed alshakero closed 5 years ago

alshakero commented 6 years ago

Fixes #31

robertdumitrescu commented 6 years ago

When will be this merged? I created a test in mocha and the array replacement is not the only problem. ` it('should getDiff and by applying the diff changes to get the modified object', async () => {

        let firstNestedObject = [
            {
                title: 'Products',
                title2: 'Products2',
                children: [
                    {
                        title: 'Electronics',
                        children: [
                            {
                                title: 'Audio-Video',
                                children: [
                                    {
                                        title: 'Game consoles',
                                        children: [
                                            {
                                                title: 'Xbox 360',
                                                date: new Date(2018, 11, 24, 10, 33, 30)
                                            },
                                            {
                                                title: 'PlayStation 4',
                                            },
                                            {
                                                title: 'Sega',
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                title: 'Mobile devices'
                            }
                        ]
                    },
                    {
                        title: 'Cars'
                    }
                ]
            },
            {
                title: 'News',
                randomDate: new Date(2018, 11, 24, 10, 33, 30),
                children: [
                    {
                        title: 'Technology',
                        children: [
                            {
                                title: 'CES 2018'
                            },
                            {
                                title: 'Why PHP sucks'
                            }
                        ]
                    },
                    {
                        title: 'Environment',
                        subObj: {
                            prop1: 'randomProp'
                        }
                    }
                ]
            }
        ];

        let secondModifiedNestedObject = [
            {
                title: 'Products Modified - =====>HEEEREE',
                title2: false,
                children: [
                    {
                        title: 'Electronics',
                        children: [
                            {
                                title: 'Audio-Video',
                                extraProp: 1,
                                extraProp00: 1413431431431413,
                                extraProp2: [
                                    'bla',
                                    {
                                        extraSubSneakyProp: 'Super Sneaky Prop =====> HEEEERE'
                                    }
                                ],
                                children: [
                                    {
                                        title: 'Game consoles',
                                        children: [
                                            {
                                                title: 'Xbox 360',
                                                date: null
                                            },
                                            {
                                                title22: 'Sega',
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                title: 'Mobile devices'
                            }
                        ]
                    },
                    {
                        title: true
                    }
                ]
            },
            {
                title: 'News',
                randomDate: new Date(2018, 11, 9, 10, 36, 30),
                children: [
                    {
                        title: null,
                        children: [
                            {
                                title: 'CES 2018'
                            },
                            {
                                title: 'Why PHP sucks'
                            },
                            {
                                title: 'Some new article,',
                                customSUbArray: [
                                    {
                                        subObject: 'nananana',
                                        _weirdProp: 'nanana2'
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        title: 'Environment',
                        subObj: [
                            1, 3, 5
                        ]
                    }
                ]
            }
        ];

        let differences = GenericObjectHelper.getRFC6902Diff(firstNestedObject, secondModifiedNestedObject);
        let actualModifiedObject = GenericObjectHelper.applyRFC6902Diff(firstNestedObject, differences);

        expect(actualModifiedObject).to.deep.equal(secondModifiedNestedObject);
    });

`

And it fails on both getting the diff out of a Date object and also when an object is replace with an array. I am currently doing a spike for this package since we need to develop something similar to git versioning but for Elastic Search documents. Any update?

P.S. get and apply are literally just wrappers around functions.

` static getRFC6902Diff(firstObject, secondObject) { return Jsonpatch.compare(firstObject, secondObject); }

static applyRFC6902Diff(object, differences) {
    let patchedObject = Jsonpatch.applyPatch(object, differences).newDocument;
    return patchedObject;
}

`

robertdumitrescu commented 6 years ago

Ah... I saw your comments in another issue about Date objects. Fair enough. Is not the focus of this plugin. Apologies. But the array/object replace is a must have though.