Coder Social home page Coder Social logo

json-patch-query's People

Contributors

carlosingles avatar semantic-release-bot avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

json-patch-query's Issues

Remove operation fails for complex JSON Path query

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.

Question - TMForum JSON Patch Query

Hi @carlosingles,

I was happy to find your repository on the TMForum JSON Patch Query + JSON Path. Congratulations on the work done.
I'm one of the authors/contributors to the TMForum Design Guidelines.

I was intriqued by the edge case you highlighted, good catch.

I was wondering if the following solution will work.

As far the limitation is focused on the JSON Path inability to go up the tree, I'm thinking that maybe it can be addressed by a logical operator condition in the json patch query it self.

Something along the way:
filter=orderItem[?(@.productOffering.id=="1513")]&filter=orderItem[*].product.relatedParty[?(@.id==7894)]
(note the & acting as a logical AND between the two conditions)

Now, filter is a selector defined in TMF DG Part 6 - for JSON Path and i've omitted the "$"

However, if we allow this, the JSON Patch Query, it should look like this

{
op: 'replace',
path: '/orderItem/quantity?filter=$.orderItem[?(@.productOffering.id=="1513")]&filter=$.orderItem[*].product.relatedParty[?(@.id==7894)]',
value: '3',
}

Which should be reflected in: "update the quantity where the filter condition A and filter condition B are sadisfied".

As per JSON Patch Query:

Operation in a JSON Patch Query document are handled in the same way as
for a JSON Patch document but the query parameter in the β€œpath” member
is used to identify the element within an array that is impacted by the
operation

So, the "query" part will come after ? where the condition of the query is specified.

I do believe this might require a small update in the TMF DG Part 5, to allow for the "filter" selector as right now it is implicitly passed in the path value.

What do you think ?

And again congrats on the great job on this repo.

Thanks,
Florin

Add support for test operation

Test patch operation hasn't seem to be supported

Steps to reproduce:

import patch from 'json-patch-query'

const item = { id: 342 }
console.log(patch.apply(item, [{ op: 'test', path: '$.id', value: 2 }]))

Expected behavior:
Error, since id with such value is missing
Actual behavior:
Ignored operation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.