carlosingles / json-patch-query

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

Question - TMForum JSON Patch Query #12

Closed florintene closed 7 months ago

florintene commented 8 months ago

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

carlosingles commented 7 months ago

Hey @florintene thanks for your interest in trying to solve the edge case outlined in the README, however the implementation of JSON Path queries is not implemented by this library and is instead implemented in this library: https://github.com/JSONPath-Plus/JSONPath which is used in this repo.

Additionally, if you would like to propose a syntax for JSON Path query operations, it is best to look at the following repo: https://github.com/ietf-wg-jsonpath/draft-ietf-jsonpath-base/ where an emerging draft proposal is being designed.

As a side note, the edge case outlined in the README can currently be solved with the following operation:

$.orderItem[?(@.productOffering.id=="1513" && @.product.relatedParty.some(({ id }) => id == "7894"))].quantity

However, this is utilising the fact that the underlying library allows for JavaScript operations to be performed which may not be in the final standard that emerges for JSON Path queries.

I hope this points you in the right direction and perhaps clarifies where the JSON Path specification is moving towards, which TMF could potentially become more involved with.