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

Issue: JsonPathMatcher - not working for rootless jsons?

Open FSatmar opened this issue 8 years ago • 5 comments

Hi, Could you pls help me with defining a JsonPathMatcher for a json of the type:

{
   id: 1,
   name: "AwesomeName"
}

I have tried "$.[?(@.id == 1)]" but it doesn't seem to work.

I tried it with the following test as well:

            // given
            var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.[?(@.id == 1)]"));

            // when
            string bodyAsString = "{  \"id\": 1, \"name\": \"AwesomeName\"  }";
            byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
            var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);

            // then
            var requestMatchResult = new RequestMatchResult();
            Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);

FSatmar avatar Mar 01 '18 13:03 FSatmar

When testing at http://jsonpath.herokuapp.com/

This works... json

So actually, I don't know what's wrong?

StefH avatar Mar 02 '18 11:03 StefH

I tried it like this:


        [Test]
        public void Request_WithBodyJsonPathMatcher_true()
        {
            // given
            var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.[?(@.Id == 1)]"));

            var requestData = new JsonObject()
            {
                Id = 1,
                Name = "AwesomeName"
            };

            var bodyData = new BodyData {BodyAsJson = requestData};

            // when
            var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", "::1", bodyData);

            // then
            var requestMatchResult = new RequestMatchResult();
            spec.GetMatchingScore(request, requestMatchResult).Should().Be(1);
        }

        [DataContract]
        public sealed class JsonObject
        {
            [DataMember(Order = 1)]
            public int Id { get; set; }
            [DataMember(Order = 2)]
            public string Name { get; set; }
        }

But still no luck. Also tried it against a real mapping with a real request using Postman & JaSon and only got the "No matching found" response.

FSatmar avatar Mar 02 '18 12:03 FSatmar

For some reason:

It should be "$..[?(@.Id == 1)]". See linked unit-test.---> https://github.com/WireMock-Net/WireMock.Net/commit/2b8a58c68ce53e94070e61f08e28ca0a354f3686

Maybe a bit related to https://github.com/JamesNK/Newtonsoft.Json/issues/1256 ?

StefH avatar Mar 02 '18 13:03 StefH

@FSatmar This is now fixed, please check new NuGet 1.0.3.12

StefH avatar Mar 24 '18 17:03 StefH

Great, it's working with the double dots, thank you very much!

FSatmar avatar Mar 26 '18 08:03 FSatmar