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

jsonpatch formatter generate bad patches #326

Open
keelii opened this issue Nov 30, 2022 · 0 comments
Open

jsonpatch formatter generate bad patches #326

keelii opened this issue Nov 30, 2022 · 0 comments

Comments

@keelii
Copy link

keelii commented Nov 30, 2022

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/jsondiffpatch/dist/jsondiffpatch.umd.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/fast-json-patch@3.1.1/dist/fast-json-patch.min.js"></script>
</head>
<body>

<script>
    var differ = new jsondiffpatch.DiffPatcher({
        objectHash(o, i) {
            return o.id || '$$index:' + i
        },
    })

    var d1 = {
        children: [
            {
                id: 1,
                children: [
                    { id: 2, key: 1 }
                ]
            }
        ]
    }
    var d2 = {
        children: [
            { id: 2, key: 1 },
            {
                id: 1,
                children: []
            }
        ]
    }

    var delta = differ.diff(d1, d2)

    var jsonPatches = jsondiffpatch.formatters.jsonpatch.format(delta)
    /*                                   ↓ only one child
    [{"op": "remove", "path": "/children/1/children/0"},
    {"op": "add", "path": "/children/0", "value": {"id": 2, "key": 1 } } ]
    */

    jsonpatch.applyPatch(d1, jsonPatches)  
    // fast-json-patch.min.js:8 Uncaught TypeError: Cannot read properties of undefined (reading 'children')

</script>
    
</body>
</html>
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

1 participant