Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonPathMatcher does not match json body nested objects #965

Closed
timurnes opened this issue Jul 10, 2023 · 7 comments
Closed

JsonPathMatcher does not match json body nested objects #965

timurnes opened this issue Jul 10, 2023 · 7 comments
Assignees
Labels

Comments

@timurnes
Copy link
Contributor

Describe the bug

JsonPathMatcher example in official documentation does not match json body with nested fields:
Pattern: $.things[?(@.name == 'RequiredThing')]
Json: { "things": { "name": "RequiredThing" } }

First-level fields match works correctly

Expected behavior:

JsonPathMatcher correctly matches json body with nested fields

Test to reproduce

[Fact]
public void JsonPathMatcher_IsMatch_JsonStringWithNestedObjects()
{
	// Assign
	var json = "{ \"things\": { \"name\": \"RequiredThing\" } }";
	var matcher = new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]");

	// Act 
	double match = matcher.IsMatch(json);

	// Assert 
	Check.That(match).IsEqualTo(1);
}

The checked value is different from the expected one.
The checked value:
	[0]
The expected value:
	[1]

Other related info

The same pattern works correctly with another json from example:
{ "things": [ { "name": "RequiredThing" }, { "name": "Wiremock" } ] }

@timurnes timurnes added the bug label Jul 10, 2023
@StefH StefH self-assigned this Jul 11, 2023
@StefH
Copy link
Collaborator

StefH commented Jul 12, 2023

#966

@StefH
Copy link
Collaborator

StefH commented Jul 12, 2023

@timurnes
You can check version
1.5.31-ci-17598

https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@timurnes
Copy link
Contributor Author

timurnes commented Jul 13, 2023

@StefH
Thanks, 1.5.31-ci-17598 works correctly. Waiting for release package version :)

Update: It doesn't. Please take a look on my next comment

@timurnes
Copy link
Contributor Author

timurnes commented Jul 13, 2023

@StefH
The fix is working only with first-level nested objects. It seems that the root cause is deeper (looking on your commits - Newtonsoft.Json?).
This test fails:

[Fact]
public void JsonPathMatcher_IsMatch_JsonStringWithNestedObjects()
{
	// Assign
	var json = "{ \"things\": { \"nested\": { \"name\": \"RequiredThing\" } } }";
	var matcher = new JsonPathMatcher("$.things.nested[?(@.name == 'RequiredThing')]");

	// Act 
	double match = matcher.IsMatch(json);

	// Assert 
	Check.That(match).IsEqualTo(1);
}

@StefH
Copy link
Collaborator

StefH commented Jul 13, 2023

@timurnes
You are correct. My fix is just a workaround for the first level.
To make it work for all levels, I need to call the code recursive. I'll check this.

BTW you know that you also can use a https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching#dynamic-linq-linqmatcher

Which is much easier to use:

"things.nested.name == \"RequiredThing\""

@timurnes
Copy link
Contributor Author

@StefH
Sure, I used JsonPartialMatcher when I discovered that JsonPathMatcher does not work correctly - it looks better for my case

@StefH
Copy link
Collaborator

StefH commented Jul 15, 2023

@timurnes
Making the fix recursive is not that easy, so for now I'll merge this PR as-is.

@StefH StefH closed this as completed Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants