Search code examples
jsontemplatesparametersresponsewiremock

How can I embed JSON from WireMock transformerParameters?


I have the following WireMock mapping file including transformerParameters. I want to embed JSON in body file.

mappings/parameters.json:

{
    "request": {
        "urlPath": "/parameters"
    },
    "response": {
        "bodyFileName": "parameters.json",
        "transformers": ["response-template"],
        "headers": {
            "Content-Type": "application/json"
        },
        "transformerParameters": {
            "keyValue": { "key": "Value" }
        }
    }
}

__files/parameters.json:

{{ parameters.keyValue }}

The response is:

{key=Value}

I need the following response:

{"key":"Value"}

How can I embed JSON from WireMock transformerParameters?


Solution

  • (self answer)

    I found a template, if fields are not nested.

    __files/parameters.json: (.hbs)

    { {{#each parameters.keyValue as |value key|
    }}{{#if @last
        }}"{{key}}":"{{value}}"{{else
        }}"{{key}}":"{{value}}",{{/if}}{{/each}} }
    

    https://handlebarsjs.com/guide/builtin-helpers.html#each