SpectoLabs / hoverfly

Lightweight service virtualization/ API simulation / API mocking tool for developers and testers
https://hoverfly.io
Apache License 2.0
2.35k stars 208 forks source link

Filtering root level field with JsonPath Expression #627

Open ghost opened 7 years ago

ghost commented 7 years ago

Partial matching of request using jsonPath match is not working when I am trying to match the root element. For example: "body":{
"jsonMatch":"{\t\"itemId\": \”\",\t\"stockNumber\": \"AA323IK\",\t\"count\": \"324\"}" } As per TommySitu Tommy Situ @tommysitu Jun 22 10:26 @subham1112, the standard JSONPath expression to achieve what you need is "$[?(@.itemId == 123456)]” Unfortunatly the JSONPath go library we are using doesn’t work properly if the JSON node in the query is a root attibute.

Please help.

tommysitu commented 7 years ago

Firstly, jsonPathMatch doesn't work probably if the JSON node in the query is a root attribute. This is a known issue. Another thing is that your example is using jsonMatch, not jsonPathMatch

pradeepsingh1234 commented 7 years ago

Do we have any plan to fix this known issue?

maxtepkeev commented 7 years ago

Any news on this ?

benjih commented 7 years ago

Quickly looking at this, this looks like a duplicate issue of #636.

tommysitu commented 7 years ago

I don't think the Go JsonPath library that hoverfly uses (https://github.com/NodePrime/jsonpath) supports root level filtering, for example: https://stackoverflow.com/questions/25031187/jsonpath-selecting-root-level-field-if-satisfies-a-condition

It could be a problem for some users, and I will also raise this issue with that project.

maxtepkeev commented 7 years ago

Thanks for the quick reply.

The https://github.com/NodePrime/jsonpath project looks abandoned to me, so I don't think that raising an issue with that project will help.

JohnFDavenport commented 7 years ago

Have you got any better suggestions @maxtepkeev ?

maxtepkeev commented 7 years ago

At least 2 come to mind:

1) Try to find another active project that implements jsonpath or if there's none 2) Make a fork of the NodePrime/jsonpath, fix the bug there, make PR which will hopefully be merged someday. Until it's not merged vendor this fork.

tommysitu commented 5 years ago

We have tried several jsonpath libraries for Golang and none of them does all the jsonpath magics. Also NodePrime/jsonpath is deprecated, hoverfly is now using this jsonpath package: https://github.com/kubernetes/client-go/blob/master/util/jsonpath

There will be a workaround once the matcher chaining feature is implemeted: https://github.com/SpectoLabs/hoverfly/issues/761