Reputation: 53
I am attempting to use Wiremock standalone with global response templating to dynamically map an input value (user id
) with a response value (username
).
My issue in summary is: unable to fetch a value from transformerParameters.*
dynamically.
I have this in the response mapping:
{
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"transformers": [
"response-template"
],
"transformerParameters": {
"usernameMapping": {
"1": "foo",
"2": "bar",
"3": "baz"
}
},
"jsonBody": {
"id": "{{request.pathSegments.[1]}}",
"username": "{{parameters.usernameMapping.2}}"
}
}
}
My goal is to populate the username
field in the JSON response based dynamically on the input user ID, by using the JSON map under transformerParameters.usernameMapping
.
I have tried the following for the username
field:
{{parameters.usernameMapping.2}}
<-- works but hardcoded so does not solve our problem{{parameters.usernameMapping[request.pathSegments.[1]]}}
<-- breaks{{parameters.usernameMapping[{{request.pathSegments.[1]}}]}}
<-- breaks{{parameters.usernameMapping.{{request.pathSegments.[1]}}}}
<-- breaks{{parameters.usernameMapping.[userId]}}
<-- (userId
assigned previously) does not break but prints nothingI've set up an example repo that quickly recreates my setup and problem, which you can clone from here: https://github.com/francocm/wiremock-template-value-mappings
Am I missing something? I'm trying to avoid writing custom extensions or compiling custom versions of Wiremock, and it feels like I am doing a trivial mistake here.
Thanks
Upvotes: 1
Views: 2139
Reputation: 53
Use lookup
in this manner:
{{lookup parameters.usernameMapping userId}}
The git repo has been updated with the working version: https://github.com/francocm/wiremock-template-value-mappings/blob/main/stubs/mappings/example-get-user.json
Upvotes: 1