mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
How to generate json text field with random numeric content #484
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 @robertmircea on GitHub (Feb 12, 2023).
Originally assigned to: @StefH on GitHub.
I am trying to generate a json string field with random integers inside using response templating. Unfortunately, I cannot obtain the expected reply:
produces a numeric field instead of a string:
instead of
How can I generate a random integer with min/max constraints formatted as a string?
Alternatively, I've tried
"{{Random Type=\"TextRegex\" Pattern=\"[0-9]{10}\"}}"and the result is the same: numeric json field instead of string. It seems that the library is converting automatically any numeric string to numeric field value.@StefH commented on GitHub (Feb 12, 2023):
Did you try adding
\"?@robertmircea commented on GitHub (Feb 12, 2023):
It formats like this:
@StefH commented on GitHub (Mar 9, 2023):
Hello @robertmircea , did you have time to verify this?
@StefH commented on GitHub (Mar 21, 2023):
Hello @robertmircea , did you have time to verify this?
@StefH commented on GitHub (May 12, 2023):
Hello @robertmircea , did you have time to verify this?
@maivanquanbk commented on GitHub (Aug 25, 2023):
Hi @StefH, when can I expect to have this feature? Thanks a lot
@StefH commented on GitHub (Nov 1, 2023):
@maivanquanbk
Did you have time to check that preview?
@felipetofoli commented on GitHub (Dec 8, 2023):
As described in the issue #1028 , I am facing the same situation when parsing a DateTime
"{{ DateTime.UtcNow \"yyMMddhhmmss\" }}"(after opening the issue I noticed the same applies for other dynamic fields, like outputs that resulted fromlookupoperation, etc.).Just adding the comment here, so I can be notified with updates about this issue.
@StefH commented on GitHub (Dec 9, 2023):
https://github.com/WireMock-Net/WireMock.Net/pull/1036
@StefH commented on GitHub (Dec 9, 2023):
@robertmircea
@maivanquanbk
@felipetofoli
I've decided to change the logic as follows:
By default, the value from Random or DateTime.Now or another Handlebars function will be converted to a primitive type. This is done to not break current functionality.
So for example when a value can be converted to a string, it will be converted.
If you do not want this, use this c# code :
.WithTransformer(ReplaceNodeOptions.Evaluate)This will generate the puk code like:
And the timestamps like:
A preview version with this logic is :
1.5.42-ci-18041@felipetofoli commented on GitHub (Dec 11, 2023):
Hi @StefH , thanks for providing this new preview version!
I believe in topic
1.you meant: "So for example when a value can be converted to a primitive type, it will be converted.", right?I tested the preview package and I received the timestamps as a string when applying the
ReplaceNodeOptions.Evaluateoption!A question: What if we have a scenario where we need different behaviors at the "property" level?
Example: We need to return the current timestamp as a string, but a random number as an integer
How can we achieve the above result?
Maybe it would be interesting to have a way to convert(cast), format or force a certain type at the "property" level?
What do you think?
@StefH commented on GitHub (Dec 12, 2023):
@felipetofoli
I understand your request, I'm working on this, but it's difficult because the return value from Handlebars is always a string.
I'm working on a work-around :
Adding an Handlebars method named
String.FormatAsStringwhich you can use to convert the string to a wrapped / encoded string value which can be understood by WireMock so that in that case, no automatic conversion will be attempted.A unit test for this could be:
However I need to think some more on this.
Maybe also a another method like
Object.FormatAsValueshould be added to always make sure that the string value is converted to a supported type....@felipetofoli commented on GitHub (Dec 12, 2023):
Thanks for your quick reply, @StefH !
Yes, this approach (at the property level) is flexible and it seems interesting!
Let me know if I can help you with anything!
Thanks!
@StefH commented on GitHub (Dec 15, 2023):
You can test preview 1.5.43-ci-18089
@felipetofoli commented on GitHub (Dec 19, 2023):
Hi @StefH !
I tested and the timestamps are returned as a
stringwhen using.WithTransformer(ReplaceNodeOptions.Evaluate).Is there anything else I should test? Am I missing any new scenario that was implemented in this preview release?
@felipetofoli commented on GitHub (Dec 19, 2023):
I checked the Handlebars repo, and I noticed that the
String.FormatAsStringmethod was implemented.The
String.FormatAsStringis working! 😃Test
Server config
Response
Question
For a scenario where we don't have to specify a format (like a simple
ToString()), is the expected passing an empty string?Or we would like to have a new overload without a "format" param (
{{ String.FormatAsString (request.PathSegments.[3]) }})?@StefH commented on GitHub (Dec 19, 2023):
I think you can just omit the format. No need for passing an empty string.
Can you try that?
@felipetofoli commented on GitHub (Dec 19, 2023):
I'd tried that. It's not possible.
Test
Server config
Response
@StefH commented on GitHub (Dec 20, 2023):
@felipetofoli
I see, this is not yet supported.
I've update Handlebars.Net.Helpers (https://github.com/Handlebars-Net/Handlebars.Net.Helpers/pull/87) and I'll release a new NuGet and build a new preview for WireMock.Net
Stay tuned....
@felipetofoli commented on GitHub (Dec 20, 2023):
@StefH
Thanks for all your support! 😃
@StefH commented on GitHub (Dec 20, 2023):
Can you try preview 1.5.43-ci-18158 ?
@felipetofoli commented on GitHub (Dec 21, 2023):
@StefH I tried the preview package:
Tests
EvaluateAndTryToConvert (or default:
.WithTransformer())HTTP Request/Response:
Evaluate
HTTP Request/Response:
Results
✅
String.FormatAsStringlooks good!⚠️
Object.FormatAsObjectdoesn't seem to work when used withReplaceNodeOptions.Evaluate(checkrandom_number_as_number,number_from_body_as_number,numeric_string_from_body_as_number).I am not sure if I am missing something here... But I think the following unit test should be satisfied, right? Or do you want to deal with
FormatAsNumberin another moment?@StefH commented on GitHub (Dec 21, 2023):
FormatAsObject does indeed not work as expected.
However I need to think on that, probably will be fixed in future.
I'll merge the PR and create a new official NuGet.
@felipetofoli commented on GitHub (Dec 21, 2023):
Thanks, @StefH !