mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Response Templating complex objects #603
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @dotnetprofessional on GitHub (May 23, 2024).
Originally assigned to: @StefH on GitHub.
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
bodyorbodyAsJson, the samples usebodybut the request is json. I've tried both without success. I get this is using theJsonPathsyntax, 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:
@StefH commented on GitHub (May 23, 2024):
Did you try using
BodyAsJson.@dotnetprofessional commented on GitHub (May 23, 2024):
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
.WithBodyAsJsonrather than.WithBodyand 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 commented on GitHub (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:
2:
And use
.WithBodyAsJson(request)@dotnetprofessional commented on GitHub (May 24, 2024):
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:
So I can see that its correctly serializing the request object and adding it to the _requestMatchers:
However, when it evaluates the request and finds it, all the body values are null.
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.
@StefH commented on GitHub (May 24, 2024):
The correct code is:
There were some issues in your example code:
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).UsingPost()instead of.UsingGet()because you can only send a body using POSTThe
.WithBodyAsJson(requestJson)is actually wrongly implemented, I will fix this.@StefH commented on GitHub (May 24, 2024):
https://github.com/WireMock-Net/WireMock.Net/pull/1111
@dotnetprofessional commented on GitHub (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
WithBodyAsJsonworks 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
Failing Sample
The issue
appearsto 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:In the sample that doesn't work we see extra trailing \. If I manually remove/fix this while debugging it works.
@StefH commented on GitHub (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 commented on GitHub (May 25, 2024):
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.
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 commented on GitHub (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:See
https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/test/Handlebars.Net.Helpers.Tests/Templates/JsonPathHelpersTemplateTests.cs#L54
@dotnetprofessional commented on GitHub (May 29, 2024):
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.