mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-07-31 01:08:41 +02:00
How to generate json text field with random numeric content #486
Closed
opened 2025-12-29 15:24:54 +01:00 by adam
·
24 comments
No Branch/Tag Specified
master
1341-mapping-json
stef-aspire-tests-updates
bug/973-TinyMapper
bug/1149-AppendGuidToSavedMappingFile
feature/1150-DockerImageVersion
61
stef-1108
stef-1097
stef-1083-MessageOptions_Type_Conflict
stef-1062-logger
stef-IgnoreOpenApiErrors
stef-928-TypeLoadException-FluentAssertions-net472
nunit
stef-849
stef-847-regex-questionmark
http_verb
WireMockServerContext
webapp
ai
CommandLineArgumentsParser
2.13.0
2.12.0
2.11.0
2.10.0
2.9.0
2.8.0
2.7.0
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.0
2.0.0
1.25.0
1.24.0
1.23.0
1.22.0
1.21.0
1.20.0
1.19.0
1.18.0
1.17.0
1.16.0
1.15.0
1.14.0
1.13.0
1.12.0
1.11.2
1.11.0
1.10.1
1.10.0
1.9.1
1.9.0
1.8.18
1.8.17
1.8.16
1.8.15
1.8.14
1.8.13
1.8.12
1.8.11
1.8.10
1.8.9
1.8.8
1.8.7
1.8.6
1.8.5
1.8.3
1.8.2
1.8.1
1.8.0
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.12
1.6.11
1.6.10
1.6.9
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.62
1.5.61
1.5.60
1.5.59
1.5.58
1.5.57
1.5.56
1.5.55
1.5.54
1.5.53
1.5.52
1.5.51
1.5.50
1.5.49
1.5.48
1.5.47
1.5.46
1.5.45
1.5.44
1.5.43
1.5.42
1.5.41
1.5.40
1.5.39
1.5.38
1.5.37
1.5.36
1.5.35
1.5.34
1.5.33
1.5.32
1.5.31
1.5.30
1.5.29
1.5.28
1.5.27
1.5.26
1.5.25
1.5.24
1.5.23
1.5.22
1.5.21
1.5.20
1.5.19
1.5.18
1.5.17
1.5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
1.0.4.9
1.0.4.8
1.0.4.7
1.0.4.6
1.0.4.5
1.0.4.4
1.0.4.3
1.0.4.2
1.0.4.1
1.0.4.0
1.0.3.20
1.0.3.19
1.0.3.18
1.0.3.17
1.0.3.16
1.0.3.15
1.0.3.14
1.0.3.13
1.0.3.12
1.0.3.11
1.0.3.10
1.0.3.9
1.0.3.8
1.0.3.7
1.0.3.6
1.0.3.5
1.0.3.4
1.0.3.3
1.0.3.2
1.0.3.1
1.0.3.0
1.0.2.13
1.0.2.12
1.0.2.11
1.0.2.10
1.0.2.9
1.0.2.8
1.0.2.7
1.0.2.6
1.0.2.5
1.0.2.4
1.0.2.1
1.0.2.0
1.0.1.5
1.0.1.3
1.0.1.2
1.0.1.1
1.0.0.0
Milestone
No items
No Milestone
Projects
Clear projects
No projects
Assignees
adam (Adam Melkus)
Clear assignees
No Assignees
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: starred/WireMock.Net-wiremock#486
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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 !