carlosingles / json-patch-query

Implementation of JSON Patch Query as proposed by the TM Forum
2 stars 0 forks source link

Remove operation fails for complex JSON Path query #3

Closed dkaleshkoska closed 2 years ago

dkaleshkoska commented 2 years ago

I have used JSONPatchQuery to remove multiple nodes at once.

Input JSON:

{
    "swagger": "2.0",
    "tags": [{
            "name": "catalog"
        },
        {
            "name": "category"
        },
        {
            "name": "productOffering"
        },
        {
            "name": "productOfferingPrice"
        },
        {
            "name": "productSpecification"
        },
        {
            "name": "importJob"
        },
        {
            "name": "exportJob"
        },
        {
            "name": "notification listeners (client side)"
        },
        {
            "name": "events subscription"
        }
    ]
}

Operations:

[
  {
    "op": "remove",
    "path": "$.tags[?(@.name!='catalog' && @.name!='productOffering' && @.name!='productSpecification')]"
  }
]

My expected result is:

{
    "swagger": "2.0",
    "tags": [{
            "name": "catalog"
        },

        {
            "name": "productOffering"
        },

        {
            "name": "productSpecification"
        }
    ]
}

However, I don't get this result because there is a bug in the apply function. Most likely the problem is that you are modifying the same document object, that is being checked for the different paths. This is particularly problematic for the remove operation. A potential fix would be to create a clone of the document object, make the modifications there and then return that object.

carlosingles commented 2 years ago

thank you for the report, you may find details of the fix in the reference PR above. I have published a new version with the fix to npm as v1.1.2 🙂

note: there was an issue with publishing v1.1.1, so please use v1.1.2