Skip to content

NullRef using JsonPath.SelectToken #40

Description

@StefH

Describe the bug

When using the JsonPath.SelectToken feature in the handlebar transformer, I get a null ref exception. The exception occurs in HandlebarsDotNet.Helpers.JsonPathHelpers.SelectToken(object, string). Decompiling shows that the value of the string parameter is "name\" including the backslash. If I remove the backslash then everything works but the backslash is just the escape character for the double-quote. if this is the good old ID10T or pebcak issue, then please close. My guess is it's an off-by-one in some string parsing.

Expected behavior:

Handlebar transformation should function as described in example at https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating#jsonpath.

Test to reproduce

  • 1 Run the code below
  • 2 Get the Null Ref exception
  • Add watch statements
    • ParseAsJToken(value, "SelectToken").SelectToken("name") - evaluates correctly
    • ParseAsJToken(value, "SelectToken").SelectToken("name\") - evaluates to null

Other related info

Unit Test Class

using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RestSharp;
using WireMock.Server;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;

namespace Sample
{
    [TestClass]
    public class JsonPath
    {
        [TestMethod]
        public void SelectToken()
        {
            var server = WireMockServer.Start();
            server.Given(Request.Create()
                .WithPath("/JsonPathSelectToken/")
                .UsingPost())
            .RespondWith(Response.Create()
                .WithStatusCode(System.Net.HttpStatusCode.OK)
                .WithHeader("Content-Type", "application/json")
                .WithBody(File.ReadAllText("response.json"))
                .WithTransformer());

            var client = new RestClient($"{server.Urls[0]}/JsonPathSelectToken/");
            var request = new RestRequest(Method.POST);
            request.AddJsonBody(new { name = "findme" });
            var response = client.Execute(request);

            server.Stop();
        }
    }
}

response.json

{
  "name": "{{JsonPath.SelectToken request.bodyAsJson \"name\"}}"
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions