Hello, team! I'm trying to template a response wi...
# help
a
Hello, team! I'm trying to template a response with a value that can be used multiple times. The idea is to generate a random integer, display it and then display a text based on that value, for that, I'm using
assign
but I'm always getting back an exception about casting.
Copy code
{
  "mappings": [
    {
      "request": {
        "urlPattern": "/test",
        "method": "GET"
      },
      "response": {
        "status": 200,
        "body": "{{#assign 'randomVal'}}{{randomInt lower=20 upper=80}}{{/assign}}{\"val\":{{randomVal}},\"text:\":\"{{#if (lt randomVal 40)}}Super low{{else}}{{#if (lt randomVal 50)}}Somewhat low{{else}}{{#if (lt randomVal 60)}}Getting there{{else}}{{#if (lt randomVal 70)}}Looking good{{else}}Wow{{/if}}{{/if}}{{/if}}{{/if}}\"}\"}",
        "headers": {
          "Content-Type": "application/json"
        },
        "transformers": [
          "response-template"
        ]
      }
    }
  ]
}
And when calling the endpoint, I receive the following error:
Copy code
curl <http://localhost:8236/test>
1:98: java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
I'm using version
3.5.4
Do you have any ideas on how to avoid this? Or different approaches to achieve what I need. Thank you very much!
I see I might need to use something like this
l
I think that is for when you want to use the same value in your response and a callback. It doesn’t look like you are trying to do that.
I’ll try to have a look at your issue but it might have to be over the weekend
a
Thank you @Lee Turner I appreciate all the help!
I did a bit of digging, it seems
assign
always assigns as string, and when the
if
executes it tries to compare a string with an integer.
The exception is thrown from
handlebars
here.
Wiremock should try and convert these values to numbers when using things like
lt
or assign as numbers if `type='NUMERIC`' or things like
randomInt
or similar are used within an
assign
statement.
l
Yes, I think you are spot on. I am not sure there is an easy way around this given the
assign
helper always assigns a string like you say. I will have to give this more thought. The
assign
helper is not part of the WireMock code either, it is part of the java handlebars implementation we are using
OK, how about this. I separated the body out into a separate file but you can put them all in the same mapping file if you want. My mapping file is like this:
Copy code
{
  "mappings": [
    {
      "request": {
        "urlPattern": "/test",
        "method": "GET"
      },
      "response": {
        "status": 200,
        "bodyFileName": "randomInt.txt",
        "headers": {
          "Content-Type": "application/json"
        },
        "transformers": [
          "response-template"
        ]
      }
    }
  ]
}
My body files (
randomInt.txt
) looks like this:
Copy code
{{#assign 'randomVal'}}{{randomInt lower=20 upper=80}}{{/assign}}

{{#assign 'randomValText'}} 
{{#lt randomVal '40'}}
Super low
{{else}}
{{#lt randomVal '50'}}
Somewhat low
{{else}}
{{#lt randomVal '60'}}
Getting there
{{else}}
{{#lt randomVal '70'}}
Looking good
{{else}}
Wow
{{/lt}}
{{/lt}}
{{/lt}}
{{/lt}}
{{/assign}}

{
"val":{{randomVal}},
"text:":"{{randomValText}}"
}
From my tests this seems to work OK but it would be worth you checking it out:
Copy code
{
  "val": 34,
  "text:": "Super low"
}
a
Thank you, @Lee Turner that worked perfectly. I guess the comparison is lexicographic and would not work with larger numbers. But for what I need is perfect. A really appreciate your help 🙏
👍 1
l
The new
val
helper in the latest
3.6.0
release makes this a little more intuitive as it maintains the types:
Copy code
{{val (randomInt lower=20 upper=80) assign='randomVal'}}

{{#assign 'randomValText'}} 
{{#lt randomVal 40}}
Super low
{{else}}
{{#lt randomVal 50}}
Somewhat low
{{else}}
{{#lt randomVal 60}}
Getting there
{{else}}
{{#lt randomVal 70}}
Looking good
{{else}}
Wow
{{/lt}}
{{/lt}}
{{/lt}}
{{/lt}}
{{/assign}}

{
"val":{{randomVal}},
"text:":"{{randomValText}}"
}