Andrés Sánchez
05/17/2024, 4:55 PMassign
but I'm always getting back an exception about casting.
{
"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:
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!Andrés Sánchez
05/17/2024, 5:31 PMLee Turner
05/17/2024, 5:35 PMLee Turner
05/17/2024, 5:35 PMAndrés Sánchez
05/17/2024, 5:58 PMAndrés Sánchez
05/17/2024, 8:13 PMassign
always assigns as string, and when the if
executes it tries to compare a string with an integer.Andrés Sánchez
05/17/2024, 8:13 PMhandlebars
here.Andrés Sánchez
05/17/2024, 8:18 PMlt
or assign as numbers if `type='NUMERIC`' or things like randomInt
or similar are used within an assign
statement.Lee Turner
05/19/2024, 1:24 PMassign
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 usingLee Turner
05/19/2024, 8:29 PM{
"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:
{{#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:
{
"val": 34,
"text:": "Super low"
}
Andrés Sánchez
05/20/2024, 3:18 AMLee Turner
05/23/2024, 8:00 AMval
helper in the latest 3.6.0
release makes this a little more intuitive as it maintains the types:
{{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}}"
}