mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Type conversion when using bodyAsJson #394
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @vovka15 on GitHub (Dec 30, 2021).
Originally assigned to: @StefH on GitHub.
Description:
Wiremock changes data types in response fields.
For example:
I have following response template:
If I am making request with field that containts text then wiremock send my text back as string type as it should be:
"text": "abc"- >"text": "abc"but if I am making request with field that contains string but it is a number then wiremock responses with the Int type:
"text": "1"->"text": 1Expected behavior:
Wiremock does not change data types in response fields.
Test to reproduce
"test": "1")"test": 1Other related info
Wiremock version - 1.4.30
@StefH commented on GitHub (Dec 31, 2021):
Hello @v1le thank you for this issue.
I'll investigate.
(Note that I first thought that this could be related to Handlebars: https://dotnetfiddle.net/b3WCwx, but I think that's ok.)
@StefH commented on GitHub (Jan 3, 2022):
https://github.com/WireMock-Net/WireMock.Net/pull/710
@StefH commented on GitHub (Jan 3, 2022):
@v1le
Try preview version
1.4.30-ci-15745.(https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)
@vovka15 commented on GitHub (Jan 4, 2022):
@StefH
Hello, just tested it.
Tried some cases:
"text": "1"->"text": "1""text": "sometext"->"text": "sometext""text": 1->"text": "1"(expected"text": 1)Well, bug is fixed, but I think would be good if these 3 cases would pass.
@StefH commented on GitHub (Jan 4, 2022):
@v1le
Thanks for testing.
However, I need to revert this fix because it's not correct.
The problem is that when
{{request.bodyAsJson.text}}is transformed, the result is always a string. It does not matter if the the source was a string or an integer.Then the string value
"1"is parsed usingJToken.Parse, and the result is an integer.So the string
"1"becomes an integer1.The only way to make sure that a string stays a string, is adding two extra quotes, like this:
When this is done, all options return a string, so:
"text": "1"->"text": "1""text": "sometext"->"text": "sometext""text": 1->"text": "1"There is no other/easy way without breaking the current logic from WireMock.
However I will think about this some more...
@vovka15 commented on GitHub (Jan 5, 2022):
Thank you for providing workaround - did not think about adding extra quotes.
I will use extra quotes on places where I am expecting string and will not where I am not.
@StefH commented on GitHub (Jan 5, 2022):
Closing this question.
https://github.com/WireMock-Net/WireMock.Net/pull/710
@StefH commented on GitHub (Jan 6, 2022):
@v1le
I see that Scriban does have the method
Evaluatewhich will return the real value, so an int stays an int.See this example:
https://dotnetfiddle.net/sZGbaX
However, currently I'm using the
Rendermethod, also for Scriban.So in order to get this working, I need to add a new setting which can switch between Evaluate or Render and then you need to choose Scriban instead of Handlebars.
Maybe this could work, however I'll not build this in a short time...