Skip to content

Generated patch incorrect for Array replacement #7

@tamalsaha

Description

@tamalsaha

Thanks for this handy library. I am trying to generate JSON patch . Here is a case that produces buggy patch:

src = {
  "spec": {
    "loadBalancerSourceRanges": [
      "192.101.0.0/16",
      "192.0.0.0/24"
    ]
  }
}

dst = {
  "spec": {
    "loadBalancerSourceRanges": [
      "192.101.0.0/24"
    ]
  }
}

The generated patch is:

[
  {
    "op": "remove",
    "path": "/spec/loadBalancerSourceRanges/0"
  },  
  {
    "op": "remove",
    "path": "/spec/loadBalancerSourceRanges/1"
  },
  {
    "op": "add",
    "path": "/spec/loadBalancerSourceRanges/0",
    "value": "192.101.0.0/24"
  }
]

This results in an error. The problem seems to be that index 1 should be removed before index 0. If I reverse the order the generated patch works.

I tested patch generation using https://github.com/stefankoegl/python-json-patch library . This produces a valid patch:

[{"path": "/spec/loadBalancerSourceRanges/1", "op": "remove"}, {"path": "/spec/loadBalancerSourceRanges/0", "value": "192.101.0.0/24", "op": "replace"}]

https://gist.github.com/tamalsaha/e8b313aec8f8d8191bc01ba504247e30

I also tested against a node.js library here: https://json8.github.io/patch/demos/apply/ . I get the same issue.

I wonder if you can fix this or give us pointers on how to fix this.

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