JsonPathMatcher does not match json body nested objects #524

Closed
opened 2025-12-29 15:25:53 +01:00 by adam · 7 comments
Owner

Originally created by @timurnes on GitHub (Jul 10, 2023).

Originally assigned to: @StefH on GitHub.

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]

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

Originally created by @timurnes on GitHub (Jul 10, 2023). Originally assigned to: @StefH on GitHub. ### Describe the bug JsonPathMatcher example in [official documentation](https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-JsonPathMatcher) 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" } ] }`
adam added the bug label 2025-12-29 15:25:53 +01:00
adam closed this issue 2025-12-29 15:25:54 +01:00
Author
Owner

@StefH commented on GitHub (Jul 12, 2023):

https://github.com/WireMock-Net/WireMock.Net/pull/966

@StefH commented on GitHub (Jul 12, 2023): https://github.com/WireMock-Net/WireMock.Net/pull/966
Author
Owner

@StefH commented on GitHub (Jul 12, 2023):

@timurnes
You can check version
1.5.31-ci-17598

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

@StefH commented on GitHub (Jul 12, 2023): @timurnes You can check version `1.5.31-ci-17598` https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Author
Owner

@timurnes commented on GitHub (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 commented on GitHub (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
Author
Owner

@timurnes commented on GitHub (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);
}
@timurnes commented on GitHub (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); } ```
Author
Owner

@StefH commented on GitHub (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\""
@StefH commented on GitHub (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\"" ```
Author
Owner

@timurnes commented on GitHub (Jul 13, 2023):

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

@timurnes commented on GitHub (Jul 13, 2023): @StefH Sure, I used JsonPartialMatcher when I discovered that JsonPathMatcher does not work correctly - it looks better for my case
Author
Owner

@StefH commented on GitHub (Jul 15, 2023):

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

@StefH commented on GitHub (Jul 15, 2023): @timurnes Making the fix recursive is not that easy, so for now I'll merge this PR as-is.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#524