-
-
Notifications
You must be signed in to change notification settings - Fork 202
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
Comments
Did you try using 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(); |
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 |
When you use In your case you could try: var requestJson = @"{ 'Name': 'John Doe', 'Age': 30 }"; // read json file
var request = JObject.Parse(requestJson); 2: |
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)
); There were some issues in your example code:
The |
Can you please provide the full C# code for |
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 |
@dotnetprofessional A solution is to use {
"market": "{{JsonPath.SelectToken request.bodyAsJson '$.pricingContext.market'}}",
"languages": "en"
} |
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. |
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
orbodyAsJson
, the samples usebody
but the request is json. I've tried both without success. I get this is using theJsonPath
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:
The text was updated successfully, but these errors were encountered: