WireMock.Net icon indicating copy to clipboard operation
WireMock.Net copied to clipboard

JsonPathMatcher filter does not work with non array JSON

Open bettyybei opened this issue 4 years ago • 3 comments

Describe the bug

JsonPathMatcher does not allow the usage of filters with non array JSON. In the example provided in the wiki, the first matching example listed { "things": { "name": "RequiredThing" } } does not actually request match with the body matcher pattern "$.things[?(@.name == 'RequiredThing')]".

Expected behavior:

JsonPathMatcher filters should work with non array JSON

Test to reproduce

  • Set body matcher pattern to "$.things[?(@.name == 'RequiredThing')]"
  • Send request with JSON body { "things": { "name": "RequiredThing" } }

Expected behavior: Returns request match Actual behavior: Does not match

Other related info

  • This behavior may have been broken in https://github.com/WireMock-Net/WireMock.Net/pull/311
  • The other example in the wiki { "things": [ { "name": "RequiredThing" }, { "name": "Wiremock" } ] } works correctly because { "name": "RequiredThing" } is in an array.
  • I believe outside of wiremock, JSONPath filters are actually only supposed to be used with arrays, so this may not actually be a bug. If so, the wiki should be updated. However, I believe there is still value in allowing the ability to use filters for non-array JSON matches, otherwise there does not seem to be a way to request match multiple JSON properties without doing an exact match of all properties using JsonMatcher. (I believe JsonPartialMatcher only allows you to match 1 property, but let me know if I'm wrong about this).

bettyybei avatar Oct 16 '21 04:10 bettyybei

Hello @bettyybei, thank you for this issue.

I did some research, and I think that the JsonPathMatcher works fine, but the examples are wrong.

The following code works fine

var things = JObject.Parse("{ \"things\": { \"name\": \"RequiredThing\" } }");
JToken wiremockOK = things.SelectToken("[?($.things.name == 'RequiredThing')]");
wiremockOK.Dump();

image

Does this make sense?

StefH avatar Oct 23 '21 07:10 StefH

@bettyybei Did you have time to verify this? Else I will close this question.

StefH avatar Feb 20 '22 12:02 StefH

@bettyybei Did you have time to verify this? Else I will close this question.

StefH avatar May 03 '22 06:05 StefH

Hello @bettyybei,

I'll close this issue for now, in case you still have questions, please reply to this comment.

StefH avatar Aug 25 '22 06:08 StefH