Skip to content

Add operation with an object followed by replace doesn't get squashed. #2

@prabinv

Description

@prabinv

I was looking at options to squash the JSON Patches generated in my application (using mobx-state-tree, and I came across this library. When adding a child object, and then setting a value within that object, it generates an add operation (with the value as the object) followed by replace operation(s). If I squash the patches, I would expect it result in a single add operation patch with the value being the object with the last replaced value. But the library generates 2 operations, one add and one replace.

I would think that this is a common issue. Do you think the library can squash operations on a object?

const squash = require('json-squash');
 
const patch = [
  { "op": "add", "path": "/a/b", "value": {c: 1}},
  { "op": "replace", "path": "/a/b/c", "value": 12 },
  { "op": "replace", "path": "/a/b/c", "value": 123 },
  { "op": "replace", "path": "/a/b/c", "value": 1234 },
  { "op": "replace", "path": "/a/b/c", "value": 12345 },
];
 
const squashed = squash(patch);

Expected

[{"op":"add","path":"/a/b","value":{"c":12345}}]

Actual

[{"op":"add","path":"/a/b","value":{"c":1}},{"op":"replace","path":"/a/b/c","value":12345}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions