Type conversion when using bodyAsJson #394

Closed
opened 2025-12-29 15:22:40 +01:00 by adam · 8 comments
Owner

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:

[
  {
    "Guid": "77ae335b-5d79-42dc-8ca7-236280ab9111",
    "Request": {
      "Path": {
        "Matchers": [
          {
            "Name": "WildcardMatcher",
            "Pattern": "/hello"
          }
        ]
      },
      "Methods": [
        "post"
      ]
  },
  "Response": {
    "StatusCode": 200,
    "BodyAsJson": { 
      "text": "{{request.bodyAsJson.text}}"
    },
    "Headers": {
        "Content-type": "application/json"
    },
    "UseTransformer": true
  }
}
]

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": 1

Expected behavior:

Wiremock does not change data types in response fields.

Test to reproduce

  1. Use template from example
  2. Create request with string that contains number (for example: "test": "1")
  3. Wiremock sends back response - "test": 1

Wiremock version - 1.4.30

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: ``` json [ { "Guid": "77ae335b-5d79-42dc-8ca7-236280ab9111", "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/hello" } ] }, "Methods": [ "post" ] }, "Response": { "StatusCode": 200, "BodyAsJson": { "text": "{{request.bodyAsJson.text}}" }, "Headers": { "Content-type": "application/json" }, "UseTransformer": true } } ] ``` 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": 1` ### Expected behavior: Wiremock does not change data types in response fields. ### Test to reproduce 1. Use template from example 2. Create request with string that contains number (for example: `"test": "1"`) 3. Wiremock sends back response - `"test": 1` ### Other related info Wiremock version - 1.4.30
adam added the question label 2025-12-29 15:22:40 +01:00
adam closed this issue 2025-12-29 15:22:40 +01:00
Author
Owner

@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 (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.)
Author
Owner

@StefH commented on GitHub (Jan 3, 2022):

https://github.com/WireMock-Net/WireMock.Net/pull/710

@StefH commented on GitHub (Jan 3, 2022): https://github.com/WireMock-Net/WireMock.Net/pull/710
Author
Owner

@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)

@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)
Author
Owner

@vovka15 commented on GitHub (Jan 4, 2022):

@StefH
Hello, just tested it.
Tried some cases:

  • Number in string - OK:
    "text": "1" -> "text": "1"
  • Just string - OK:
    "text": "sometext" -> "text": "sometext"
  • Number - Not OK:
    "text": 1 -> "text": "1" (expected "text": 1)

Well, bug is fixed, but I think would be good if these 3 cases would pass.

@vovka15 commented on GitHub (Jan 4, 2022): @StefH Hello, just tested it. Tried some cases: - Number in string - **OK**: `"text": "1"` -> `"text": "1"` - Just string - **OK**: `"text": "sometext"` -> `"text": "sometext"` - Number - **Not OK**: `"text": 1` -> `"text": "1"` (expected `"text": 1`) Well, bug is fixed, but I think would be good if these 3 cases would pass.
Author
Owner

@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 using JToken.Parse, and the result is an integer.
So the string "1" becomes an integer 1.

The only way to make sure that a string stays a string, is adding two extra quotes, like this:

"BodyAsJson": { 
      "text": "\"{{request.bodyAsJson.text}}\""
    },

When this is done, all options return a string, so:

  • Number in string:
    "text": "1" -> "text": "1"
  • Just string:
    "text": "sometext" -> "text": "sometext"
  • Number:
    "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...

@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 using `JToken.Parse`, and the result is an integer. So the string `"1"` becomes an integer `1`. The only way to make sure that a string stays a string, is adding two extra quotes, like this: ``` json "BodyAsJson": { "text": "\"{{request.bodyAsJson.text}}\"" }, ``` When this is done, all options return a string, so: - Number in string: `"text": "1"` -> `"text": "1"` - Just string: `"text": "sometext"` -> `"text": "sometext"` - Number: `"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...
Author
Owner

@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.

@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.
Author
Owner

@StefH commented on GitHub (Jan 5, 2022):

Closing this question.

https://github.com/WireMock-Net/WireMock.Net/pull/710

@StefH commented on GitHub (Jan 5, 2022): Closing this question. https://github.com/WireMock-Net/WireMock.Net/pull/710
Author
Owner

@StefH commented on GitHub (Jan 6, 2022):

@v1le

I see that Scriban does have the method Evaluate which will return the real value, so an int stays an int.

See this example:
https://dotnetfiddle.net/sZGbaX

However, currently I'm using the Render method, 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...

@StefH commented on GitHub (Jan 6, 2022): @v1le I see that Scriban does have the method `Evaluate` which will return the real value, so an int stays an int. See this example: https://dotnetfiddle.net/sZGbaX However, currently I'm using the `Render` method, 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...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#394