Request Matching JsonPathMatcher Partially #605

Open
opened 2025-12-29 15:28:06 +01:00 by adam · 2 comments
Owner

Originally created by @hmiguel on GitHub (Jun 4, 2024).

Originally assigned to: @StefH on GitHub.

Hello, I'm trying to match a json path partially, but I'm not sure how to do it.

Here's what I have:

        var client = new HttpClient();
        var server = WireMockServer.Start();

        // this match
        var test1 = new { things = new { name = "RequiredThing" } };

        // this does not match
        var test2 = new { things = new { name = "RequiredThing" }, soup = "carrot" };
   
        server.Given(
            WireMock.RequestBuilders.Request.Create()
            .WithPath("/test1")
                .UsingPost()
                .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))
            )
            .RespondWith(
                WireMock.ResponseBuilders.Response.Create()
                   .WithHeader("Content-Type", "application/json")
                   .WithStatusCode(200)
                   .WithDelay(TimeSpan.FromMilliseconds(50))
            );

        server.Given(
           WireMock.RequestBuilders.Request.Create()
           .WithPath("/test2")
               .UsingPost()
               .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))
           )
           .RespondWith(
               WireMock.ResponseBuilders.Response.Create()
                  .WithHeader("Content-Type", "application/json")
                  .WithStatusCode(200)
                  .WithDelay(TimeSpan.FromMilliseconds(50))
           );

        var response1 = await client.PostAsJsonAsync($"http://localhost:{server.Port}/test1", test1);
        response1.EnsureSuccessStatusCode();

        var response2 = await client.PostAsJsonAsync($"http://localhost:{server.Port}/test2", test2);
        response2.EnsureSuccessStatusCode();

My expectation is that both requests match, but the second one fails.

The same approach using JmesPathMatching works fine.

server.Given(
    WireMock.RequestBuilders.Request.Create()
    .WithPath("/test2")
        .UsingPost()
       //.WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))
       .WithBody(new JmesPathMatcher("things.name == 'RequiredThing'"))
    )
    .RespondWith(
        WireMock.ResponseBuilders.Response.Create()
           .WithHeader("Content-Type", "application/json")
           .WithStatusCode(200)
           .WithDelay(TimeSpan.FromMilliseconds(50))
    );

WireMock.Net: 1.5.56

Originally created by @hmiguel on GitHub (Jun 4, 2024). Originally assigned to: @StefH on GitHub. Hello, I'm trying to match a json path partially, but I'm not sure how to do it. Here's what I have: ```c# var client = new HttpClient(); var server = WireMockServer.Start(); // this match var test1 = new { things = new { name = "RequiredThing" } }; // this does not match var test2 = new { things = new { name = "RequiredThing" }, soup = "carrot" }; server.Given( WireMock.RequestBuilders.Request.Create() .WithPath("/test1") .UsingPost() .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")) ) .RespondWith( WireMock.ResponseBuilders.Response.Create() .WithHeader("Content-Type", "application/json") .WithStatusCode(200) .WithDelay(TimeSpan.FromMilliseconds(50)) ); server.Given( WireMock.RequestBuilders.Request.Create() .WithPath("/test2") .UsingPost() .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")) ) .RespondWith( WireMock.ResponseBuilders.Response.Create() .WithHeader("Content-Type", "application/json") .WithStatusCode(200) .WithDelay(TimeSpan.FromMilliseconds(50)) ); var response1 = await client.PostAsJsonAsync($"http://localhost:{server.Port}/test1", test1); response1.EnsureSuccessStatusCode(); var response2 = await client.PostAsJsonAsync($"http://localhost:{server.Port}/test2", test2); response2.EnsureSuccessStatusCode(); ``` My expectation is that both requests match, but the second one fails. The same approach using JmesPathMatching works fine. ```C# server.Given( WireMock.RequestBuilders.Request.Create() .WithPath("/test2") .UsingPost() //.WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")) .WithBody(new JmesPathMatcher("things.name == 'RequiredThing'")) ) .RespondWith( WireMock.ResponseBuilders.Response.Create() .WithHeader("Content-Type", "application/json") .WithStatusCode(200) .WithDelay(TimeSpan.FromMilliseconds(50)) ); ``` WireMock.Net: 1.5.56
adam added the question label 2025-12-29 15:28:06 +01:00
Author
Owner
@StefH commented on GitHub (May 2, 2025): @hmiguel In that case, did you try these? - https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-JsonPartialMatcher - https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-JsonPartialWildcardMatcher
Author
Owner
@StefH commented on GitHub (Dec 5, 2025): @hmiguel In that case, did you try these? https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-JsonPartialMatcher https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-JsonPartialWildcardMatcher
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#605