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

Response Templating complex objects #1108

Closed
dotnetprofessional opened this issue May 23, 2024 · 11 comments
Closed

Response Templating complex objects #1108

dotnetprofessional opened this issue May 23, 2024 · 11 comments
Assignees
Labels

Comments

@dotnetprofessional
Copy link

dotnetprofessional commented May 23, 2024

I'm trying to get templating working, taking data from the incoming request and mapping it to the response. I'm sure I'm not understanding the examples as I just can't get anything but a very simple {{request.path}} to work. So at least I know its transforming :)

Any advice on how to map complex objects would be helpful. I tried looking at the samples, but none seemed to cover this scenario. They dealt with collections. I'm also confused with when to use body or bodyAsJson, the samples use body but the request is json. I've tried both without success. I get this is using the JsonPath syntax, but from what I can tell the syntax I have should work.

Here is a sample, that returns the error:

{"Status":"Value cannot be null. (Parameter 'value')"}

formatted request body:

{
  "PricingContext": {
    "Market": "USA"
  }
}
        var requestJson = JsonConvert.SerializeObject(new { PricingContext = new { Market = "USA" } });
        var responseJson = JsonConvert.SerializeObject(new { Market = "{{JsonPath.SelectToken request.body \"$.pricingContext.market\"}}" });
        var _wireMockServer = Server.WireMockServer.Start(new WireMockServerSettings
        {
            Port = 9091,
            AllowPartialMapping = true,
        });
        _wireMockServer
            .Given(WireMock.RequestBuilders.Request.Create()
                    .WithPath("/pricing")
                    .WithBody(requestJson)
                    .UsingPost())
                .RespondWith(Response.Create()
                    .WithHeader("Content-Type", "application/json")
                    .WithBody(responseJson)
                    .WithTransformer(true)
                );
        var http = new HttpClient();
        var response = await http.GetAsync($"{_wireMockServer.Url}/pricing");
        var value = await response.Content.ReadAsStringAsync();
@StefH StefH self-assigned this May 23, 2024
@StefH
Copy link
Collaborator

StefH commented May 23, 2024

Did you try using BodyAsJson.

        var request = new { PricingContext = new { Market = "USA" } };
        var response = new { Market = "{{JsonPath.SelectToken request.bodyAsJson \"$.pricingContext.market\"}}" };
        var _wireMockServer = Server.WireMockServer.Start(new WireMockServerSettings
        {
            Port = 9091,
            AllowPartialMapping = true,
        });
        _wireMockServer
            .Given(WireMock.RequestBuilders.Request.Create()
                    .WithPath("/pricing")
                    .WithBodyAsJson(request)
                    .UsingPost())
                .RespondWith(Response.Create()
                    .WithHeader("Content-Type", "application/json")
                    .WithBodyAsJson(response)
                    .WithTransformer(true)
                );
        var http = new HttpClient();
        var response = await http.GetAsync($"{_wireMockServer.Url}/pricing");
        var value = await response.Content.ReadAsStringAsync();

@dotnetprofessional
Copy link
Author

Yeah, but I'm actually wanting to have this work with the .json files in the final solution. However, I've included this sample as it seems to have the same issue, so assuming its not specific to using .json files.

So I did update the code to use .WithBodyAsJson rather than .WithBody and I'm getting the same result. You should be able to paste the code above into your test project (that's what I'm doing) and validate the error.

@StefH
Copy link
Collaborator

StefH commented May 24, 2024

When you use .WithBody(requestJson), the requestJson is just a string, it's not possible to do any JsonPath.SelectToken on a string.

In your case you could try:
1:

var requestJson = @"{ 'Name': 'John Doe', 'Age': 30 }"; // read json file
var request = JObject.Parse(requestJson);

2:
And use .WithBodyAsJson(request)

@dotnetprofessional
Copy link
Author

Ok, so that didn't resolve my issue. I've been digging a little deeper, and it seems the issue is that the request body is null when it hits the Handlebars evaluation (btw curious why you're walking the node tree rather than just give it the full text). Anyway, I see that you serialize, when an object is passed, so using that so I'm using more of the library, here's the updated code I'm using:

        var requestJson = new { PricingContext = new { Market = "USA" } };
        var responseJson = new { Market = "{{JsonPath.SelectToken request.bodyAsJson \"$.PricingContext.Market\"}}" };
        var _wireMockServer = Server.WireMockServer.Start(new WireMockServerSettings
        {
            Port = 9091,
            AllowPartialMapping = true,
        });
        _wireMockServer
            .Given(WireMock.RequestBuilders.Request.Create()
                    .WithPath("/pricing")
                    .WithBodyAsJson(requestJson)
                    .UsingGet())
                .RespondWith(Response.Create()
                    .WithHeader("Content-Type", "application/json")
                    .WithBodyAsJson(responseJson)
                    .WithTransformer(true)
                );
        var http = new HttpClient();
        var response = await http.GetAsync($"{_wireMockServer.Url}/pricing");
        var value = await response.Content.ReadAsStringAsync();

So I can see that its correctly serializing the request object and adding it to the _requestMatchers:

image

However, when it evaluates the request and finds it, all the body values are null.

image

So when the handlebars attempt to evaluate there's no data to use. At least this is my understanding of the code. I'm unclear what I've done that is making the request data unavailable.

image

@StefH
Copy link
Collaborator

StefH commented May 24, 2024

The correct code is:

var requestJson = new { PricingContext = new { Market = "USA" } };
var responseJson = new { Market = "{{JsonPath.SelectToken request.body \"$.PricingContext.Market\"}}" };
server
    .Given(Request.Create()
        .WithBody(new JsonMatcher(requestJson))
        .WithPath("/pricing")
        .UsingPost()
    )
    .RespondWith(Response.Create()
        .WithHeader("Content-Type", "application/json")
        .WithBodyAsJson(responseJson)
        .WithTransformer(true)
    );

image

There were some issues in your example code:

  1. AllowPartialMapping = true --> this means that any mappng will be returned, and probably the wrong mapping was found (https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-Tips)
  2. use .UsingPost() instead of .UsingGet() because you can only send a body using POST

The .WithBodyAsJson(requestJson) is actually wrongly implemented, I will fix this.

@StefH
Copy link
Collaborator

StefH commented May 24, 2024

#1111

@dotnetprofessional
Copy link
Author

dotnetprofessional commented May 25, 2024

Thanks, appreciate your prompt replies. So I must apologize, I was so focused on the Wiremock code I ignored my actual request. I wasn't passing the request that I was trying to parse. Anyway your code helped me identify that. So, I was able to validate that using the WithBodyAsJson works as expected now. However, I think there's a bug when using the Admin files and just Json. I have two different tests, one which works, the other that doesnt. I'll explain where I see the issue at the end:

Working Sample

    [Fact]
    public async Task Handlebars_SelectToken_complex_object_using_fluentAPI()
    {
        var responseTemplate = new
        {
            market = "{{JsonPath.SelectToken request.bodyAsJson \"$.pricingContext.market\"}}",
            languages = "en"
        };

        var responseBody = new
        {
            pricingContext = new
            {
                market = "US"
            }
        };

        var _wireMockServer = Server.WireMockServer.Start(new WireMockServerSettings
        {
            Port = 9091,
            StartAdminInterface = true,
            AllowPartialMapping = true,
            DisableJsonBodyParsing = false,
        });

        _wireMockServer
            .Given(WireMock.RequestBuilders.Request.Create()
                    .WithPath("/prices")
                    .UsingPost())
            .RespondWith(Response.Create()
                .WithHeader("Content-Type", "application/json")
                .WithBodyAsJson(responseTemplate)
                .WithTransformer(true)
            );

        var http = new HttpClient();
        var httpContent = new StringContent(JsonConvert.SerializeObject(responseBody), Encoding.UTF8, "application/json");
        var response = await http.PostAsync($"{_wireMockServer.Url}/prices", httpContent);
        var value = await response.Content.ReadAsStringAsync();
    }

Failing Sample

    [Fact]
    public async Task Handlebars_SelectToken_complex_object_using_FileFromBody()
    {
        var responseJson = File.ReadAllText("c:\\temp\\generic-template-request-body.json");
        var _wireMockServer = Server.WireMockServer.Start(new WireMockServerSettings
        {
            Port = 9091,
            FileSystemHandler = new CustomLocalFileSystemHandler("c:\\temp"),
            StartAdminInterface = true,
            ReadStaticMappings = true,
            WatchStaticMappings = true,
            WatchStaticMappingsInSubdirectories = true,
            AllowPartialMapping = true,
            DisableJsonBodyParsing = false,
        });

        var http = new HttpClient();
        var httpContent = new StringContent(responseJson, Encoding.UTF8, "application/json");
        var response = await http.PostAsync($"{_wireMockServer.Url}/prices", httpContent);
        var value = await response.Content.ReadAsStringAsync();
    }

The issue appears to be in the parsing of the arguments (couldn't work out where this is happening). The image below shows how the argurments is populated for the working example:

image

In the sample that doesn't work we see extra trailing \. If I manually remove/fix this while debugging it works.

image

@StefH
Copy link
Collaborator

StefH commented May 25, 2024

Can you please provide the full C# code for Task Handlebars_SelectToken_complex_object_using_FileFromBody?
Because I don't see the mapping.

@dotnetprofessional
Copy link
Author

Sure, here's my custom file handler, mainly so I can change the name of the directory and ignore non .json file, so not sure I need that.

public class CustomLocalFileSystemHandler : LocalFileSystemHandler
{
    private string _serviceMockPath;
    // have to duplicate what's in the base as it's private
    private string _rootFolder = string.Empty;
    public CustomLocalFileSystemHandler() : this(Directory.GetCurrentDirectory(), "_serviceMocks") { }

    public CustomLocalFileSystemHandler(string serviceMocks) : this(Directory.GetCurrentDirectory(), serviceMocks) { }

    public CustomLocalFileSystemHandler(string rootPath, string serviceMocks) : base(rootPath)
    {
        _rootFolder = rootPath;
        _serviceMockPath = serviceMocks;
    }

    public override string GetMappingFolder()
    {
        return Path.Combine(_rootFolder, _serviceMockPath);
    }

    public override IEnumerable<string> EnumerateFiles(string path, bool includeSubdirectories)
    {
        return includeSubdirectories ? Directory.EnumerateFiles(path, "*.json", SearchOption.AllDirectories) : Directory.EnumerateFiles(path);
    }
}

I've attached the three files I'm using, that in the C:\temp. Let me know if you need any more details.

generic-template-request.json
generic-template-request-body.json
generic-template-response.json

@StefH
Copy link
Collaborator

StefH commented May 27, 2024

@dotnetprofessional
It's related to HandleBars.Net, it seems that the escaped " are not correct parsed.

A solution is to use ', like:

{
  "market": "{{JsonPath.SelectToken request.bodyAsJson '$.pricingContext.market'}}",
  "languages": "en"
}

See
https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/test/Handlebars.Net.Helpers.Tests/Templates/JsonPathHelpersTemplateTests.cs#L54

@dotnetprofessional
Copy link
Author

Thanks for your help, this was the issue! I'd like to recommend the docs be updated to use single quotes, it should avoid others experiencing the same issue.

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

No branches or pull requests

2 participants