Handlebars-Net / Handlebars.Net.Helpers

Handlebars.Net helpers in the categories: 'Boolean', 'Constants', 'Enumerable', 'Environment', 'Math', 'Regex', 'String', 'DateTime' and 'Url'.
MIT License
38 stars 14 forks source link

NullRef using JsonPath.SelectToken #40

Open StefH opened 2 years ago

StefH commented 2 years ago

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

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\"}}"
}
StefH commented 2 years ago

Hello @sethsouthern.

WireMock.Net uses HandlebarsDotNet.Helpers.JsonPathHelpers to handle JsonPath. This project is located here: https://github.com/Handlebars-Net/Handlebars.Net.Helpers

I did move the issue.

And can you please create a failing unit test ? I want to check if this is an issue in JsonPath, JsonPathHelpers or Handlebars.Net

StefH commented 11 months ago

@sethsouthern did you have time to create a unit test?