NullRef using JsonPath.SelectToken #353

Closed
opened 2025-12-29 15:21:34 +01:00 by adam · 1 comment
Owner

Originally created by @sethsouthern on GitHub (Jul 27, 2021).

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

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\"}}"
}
Originally created by @sethsouthern on GitHub (Jul 27, 2021). ### 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 ``` c# 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 ``` json { "name": "{{JsonPath.SelectToken request.bodyAsJson \"name\"}}" } ```
adam added the duplicate label 2025-12-29 15:21:34 +01:00
adam closed this issue 2025-12-29 15:21:35 +01:00
Author
Owner

@StefH commented on GitHub (Jul 30, 2021):

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 on GitHub (Jul 30, 2021): 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
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#353