Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type conversion when using bodyAsJson #709

Closed
vovka15 opened this issue Dec 30, 2021 · 8 comments
Closed

Type conversion when using bodyAsJson #709

vovka15 opened this issue Dec 30, 2021 · 8 comments
Assignees
Labels

Comments

@vovka15
Copy link

vovka15 commented Dec 30, 2021

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

Other related info

Wiremock version - 1.4.30

@vovka15 vovka15 added the bug label Dec 30, 2021
@StefH
Copy link
Collaborator

StefH commented 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 StefH self-assigned this Jan 3, 2022
@StefH
Copy link
Collaborator

StefH commented Jan 3, 2022

#710

@StefH
Copy link
Collaborator

StefH commented Jan 3, 2022

@V1le

Try preview version 1.4.30-ci-15745.

(https:/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)

@vovka15
Copy link
Author

vovka15 commented 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.

@StefH
Copy link
Collaborator

StefH commented 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...

@vovka15
Copy link
Author

vovka15 commented 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
Copy link
Collaborator

StefH commented Jan 5, 2022

Closing this question.

#710

@StefH StefH closed this as completed Jan 5, 2022
@StefH StefH added question and removed bug labels Jan 6, 2022
@StefH
Copy link
Collaborator

StefH commented 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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants