https://linen.dev logo
#general
Title
# general
v

Victor Baulac

11/28/2023, 12:42 PM
Hello. I am using WireMock (not WireMock Cloud) in standalone on a docker. So I configure my mappings via JSON files. Is it possible to use each, and conditions, in the JSON to make it dynamic ? (eg : I receive an array of X element, so I return another array of X elements) I struggle to find documentation of response templating for WireMock standalone on docker. Thanks !
t

Tom

11/28/2023, 12:50 PM
Hi Victor, the templating language is the same regardless of which edition so everything in https://wiremock.org/docs/response-templating/ applies. The
each
,
if
and other conditional helpers are all available.
v

Victor Baulac

11/28/2023, 1:02 PM
Hi Tom thanks for this quick answer. It seems that I was missing the ""transformers": ["response-template"]". However when I am trying to do something slightly more complicated than just a single if, when I do it on, several lines, I have issues on startup : "Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value". Do I need to set an option to allow "coding" on several lines ?"
t

Tom

11/28/2023, 1:14 PM
No, you shouldn't need that. If you post the code you're trying to implement I might be able to see what's wrong.
v

Victor Baulac

11/28/2023, 1:16 PM
{
"request": {
"method": "GET",
"url": "/conditional/stuff"
},
"response": {
"status": 200,
"body": "{{#if (contains 'abcde' 'abc')}}
YES
{{/if}}",
"transformers": ["response-template"],
"headers": {
"Content-Type": "text/json"
}
}
}
As simple as that 🙂 When runing the docker I get :
probably something to do with tabulations ? or LF/CRLF ?
So after transforming tabulations into spaces, and CRLF to LF, I still have the issue at the end of the line :
"body": "{{#if (contains 'abcde' 'abc')}}
t

Tom

11/28/2023, 1:29 PM
Yeah, it looks like you’ve got some UTF16 characters in there maybe?
Ah sorry - missed the obvious thing - yes, in JSON you can’t have multi-line strings
Copy code
{
  "request": {
    "method": "GET",
    "url": "/conditional/stuff"
  },
  "response": {
    "status": 200,
    "body": "{{#if (contains 'abcde' 'abc')}}\nYES\n{{/if}}",
    "transformers": [
      "response-template"
    ],
    "headers": {
      "Content-Type": "text/json"
    }
  }
}
v

Victor Baulac

11/28/2023, 1:34 PM
So I have to keep all my "code" in a single line... =(
Not easy to read/maintain 😕
But this is what I was afraid of, thx for confirming
t

Tom

11/28/2023, 1:41 PM
This is standard JSON unfortunately, not a lot we can do about that. We have a PR open to expand to JSON5 and another to add YAML support so these will help in cases like this.
v

Victor Baulac

11/28/2023, 1:42 PM
I'll be happy to define it using something else. My needs are for it to be easy and simple. A docker running with files in the mapping folder. I don't wan't to create a java app for this mock
t

Tom

11/28/2023, 1:43 PM
Do you know you can also extract the response body to its own file?
v

Victor Baulac

11/28/2023, 1:43 PM
Yeah I think I read that but in this extracted file, I won't be able to have multi-line right ?
t

Tom

11/28/2023, 1:43 PM
So you create a file called e.g.
my-response-body.json
under
__files
then reference it
"bodyFileName": "my-response-body.json"
in your stub instead of
body
v

Victor Baulac

11/28/2023, 1:44 PM
What I need is to have some "complicated" stuff like :
Copy code
"body": "[ {{#each (jsonPath request.body '$') as |todo|}}
                  { \"todo_id\":
                      {{#if (todo.todo_id == null) }}
                          1{{randomValue length=1 type='NUMERIC'}}
                      {{else}}
                          {{todo.todo_id}}
                      {{/if}}
                },
            {{/each}} ]"
On multi-line so it can be on source control, and be reviewed
t

Tom

11/28/2023, 1:44 PM
Then you can format the body however you like
Nothing you stop you source controlling from the parent folder of
mappings
and
__files
This is what lots of teams do
v

Victor Baulac

11/28/2023, 1:44 PM
Can I really format it the way I want ? is is still a json "my-response-body*.json*"
t

Tom

11/28/2023, 1:45 PM
Yes, although it can be whatever format you’re using
It’s just a text file that’s used in place of the
body
value
v

Victor Baulac

11/28/2023, 1:45 PM
I'm in a meeting rn but I'll give that a try after 🙂 thanks !
👍 1
t

Tom

11/28/2023, 1:45 PM
The
Content-Type
response header is what actually determines the type, as far as your calling client is concerned
v

Victor Baulac

11/28/2023, 1:50 PM
Yep it works ! thanks for that, it is not perfect, but it will definitly cover my needs
5-star support here 😉
t

Tom

11/28/2023, 1:50 PM
No probs 👍