Hi all ! I am writing a dynamic json response te...
# help
m
Hi all ! I am writing a dynamic json response template in which I want to do the following : As part of a bigger json .. return the following key value pair only if the if statement is satisfied. Otherwise I don’t want the key/value returned but rest of the json returned. I’ve tried the following , however it still returns the key: “Foo”: “{{#if (contains request.headers.bar.0 ‘MQ’)}}Q{{/if}}” However this will return “Foo”:‘’ Where as I don’t want the key/value returned at all. Is there a way of doing conditional json keys ? Thanks so much!
b
In cases like this I think it’s better to just create two separate responses. Keep it simple.
l
I agree with @Bas Dijkstra, you could create two separate stubs and one of them will match on the header containing
MQ
and include the
Foo
field in the response and the other stub won't include the field. If that isn't possible you could just wrap the
if
around the whole field to determine whether it is included or not:
Copy code
{
  "status": "OK",
  {{#if (contains request.headers.bar.0 'MQ')}}
  "Foo": "Q",
  {{/if}}
  "message": "Hello"
}
m
Hi Bas and Lee For the first approach - that would mean I have 2 separate response json files and then my mocking logic will be spread between 1. Request.json that directs the request based on this field to the proper response.json 2. Response.json file that Has other dynamic response templating The second approach doesn’t work inside my response.json file as it expects property:value pair and you can’t write if statements before Any other thoughts?
b
Wouldn't something like this be enough?
Copy code
{
  "mappings": [
    {
      "request": {
        "method": "GET",
        "url": "/something",
        "headers": {
          "bar": {
            "contains": "MQ"
          }
        }
      },
      "response": {
        "status": 200,
        "body": "response_including_foo"
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/something",
        "headers": {
          "bar": {
            "doesNotContain": "MQ"
          }
        }
      },
      "response": {
        "status": 200,
        "body": "response_without_foo"
      }
    }
  ]
}
Or am I missing something here?
l
The second approach doesn’t work inside my response.json file as it expects property:value pair and you can’t write if statements before
This only doesn't work in your file because your response file is a json file. You are wanting to send a json response but that doesn't mean your response file has to be json. Wiremock will still handle it fine even if your IDE/editor shows that there are errors in the json due to the handlebars
Just as an example, take a looks that this mapping. It links to this response file which is json. In my editor, this shows errors because it isn't actually valid json with the handlebars in there. WireMock still handles it OK though. I could rename the response file to have a
.txt
extension which would remove the errors from my editor and WireMock would still handle it fine