Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullRef using JsonPath.SelectToken #40

Open
StefH opened this issue Jul 30, 2021 · 4 comments
Open

NullRef using JsonPath.SelectToken #40

StefH opened this issue Jul 30, 2021 · 4 comments
Assignees

Comments

@StefH
Copy link
Collaborator

StefH commented Jul 30, 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

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
Copy link
Collaborator Author

StefH commented 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
Copy link
Collaborator Author

StefH commented Jul 19, 2023

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

@StefH StefH self-assigned this Oct 9, 2024
@StefH
Copy link
Collaborator Author

StefH commented Oct 9, 2024

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

1 similar comment
@StefH
Copy link
Collaborator Author

StefH commented Nov 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant