Can I include a modified datetime in the response via a JSON definition? #495

Closed
opened 2025-12-29 15:25:15 +01:00 by adam · 7 comments
Owner

Originally created by @MichaelIDS on GitHub (Feb 24, 2023).

Originally assigned to: @StefH on GitHub.

I'm trying to add an hour to the current DateTime and include that new DateTime in the response.

I know I can get the current DateTime via
{{DateTime.Now}}

But I can't see how I can manipulate this.

I wondered about using Handlebars and splitting the string, then using an each and if's to manipulate it, but I can't seem to nest the response value of {{DateTime.Now}} into a handlebars expression.

Is nesting {{}} within each other across handlers not supported I take it, i.e. the inner customer handler for {{DateTime}} isn't completed and substituted before the result is passed to handlebars for the {{each}} to be processed by that library.

EDIT: I originally misunderstood what Humanizer DateTime did and initially thought it could help with adding an hour to the time. hence the title and content change.

Originally created by @MichaelIDS on GitHub (Feb 24, 2023). Originally assigned to: @StefH on GitHub. I'm trying to add an hour to the current DateTime and include that new DateTime in the response. I know I can get the current DateTime via ```{{DateTime.Now}}``` But I can't see how I can manipulate this. I wondered about using Handlebars and splitting the string, then using an each and if's to manipulate it, but I can't seem to nest the response value of `{{DateTime.Now}}` into a handlebars expression. Is nesting `{{}}` within each other across handlers not supported I take it, i.e. the inner customer handler for `{{DateTime}}` isn't completed and substituted before the result is passed to handlebars for the `{{each}}` to be processed by that library. EDIT: I originally misunderstood what Humanizer DateTime did and initially thought it could help with adding an hour to the time. hence the title and content change.
adam added the question label 2025-12-29 15:25:15 +01:00
adam closed this issue 2025-12-29 15:25:16 +01:00
Author
Owner

@StefH commented on GitHub (Feb 26, 2023):

This is not yet possible, I'm working on an update on the DynamicLinq Handlebars helper.

@StefH commented on GitHub (Feb 26, 2023): This is not yet possible, I'm working on an update on the DynamicLinq Handlebars helper.
Author
Owner

@StefH commented on GitHub (Mar 1, 2023):

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

@StefH commented on GitHub (Mar 1, 2023): https://github.com/WireMock-Net/WireMock.Net/pull/893
Author
Owner

@StefH commented on GitHub (Mar 1, 2023):

@MichaelIDS

If you use version 1.5.17-ci-17109, you can now use DynamicLinq to manipulate e.g. DateTime or do other logic.

Example mapping json:

"BodyAsJson": {
  "DynamicLinq.Expression 'DateTime.Now.AddHours(1)'": "{{DynamicLinq.Expression 'DateTime.Now.AddHours(1)'}}",
  "Path.Lookup data request.PathSegments.[1] 'N/A'": "{{Path.Lookup data request.PathSegments.[1] 'N/A'}}",
  "Path.Lookup data request.PathSegments.[4] 'N/A'": "{{Path.Lookup data request.PathSegments.[4] 'N/A'}}",
  "Linq data 'it[\"1\"] ?? \"n/a\"'": "{{Linq data 'it[\"1\"] ?? \"n/a\"'}}",
  "Linq data 'it[\"9\"] ?? \"n/a\"'": "{{Linq data 'it[\"9\"] ?? \"n/a\"'}}",
  "DynamicLinq.Expression data 'it[\"1\"] ?? \"n/a\"'": "{{DynamicLinq.Expression data 'it[\"1\"] ?? \"n/a\"'}}",
  "DynamicLinq.Expression data 'it[\"9\"] ?? \"n/a\"'": "{{DynamicLinq.Expression data 'it[\"9\"] ?? \"n/a\"'}}"
},

And when calling the url: http://localhost/data/1

Results into response body:

{
    "DynamicLinq.Expression 'DateTime.Now.AddHours(1)'": "2023-03-01T19:10:04.2367715+01:00",
    "Path.Lookup data request.PathSegments.[1] 'N/A'": "one",
    "Path.Lookup data request.PathSegments.[4] 'N/A'": "N/A",
    "Linq data 'it[\"1\"] ?? \"n/a\"'": "one",
    "Linq data 'it[\"9\"] ?? \"n/a\"'": "n/a",
    "DynamicLinq.Expression data 'it[\"1\"] ?? \"n/a\"'": "one",
    "DynamicLinq.Expression data 'it[\"9\"] ?? \"n/a\"'": "n/a"
}
@StefH commented on GitHub (Mar 1, 2023): @MichaelIDS If you use version `1.5.17-ci-17109`, you can now use DynamicLinq to manipulate e.g. DateTime or do other logic. Example mapping json: ``` json "BodyAsJson": { "DynamicLinq.Expression 'DateTime.Now.AddHours(1)'": "{{DynamicLinq.Expression 'DateTime.Now.AddHours(1)'}}", "Path.Lookup data request.PathSegments.[1] 'N/A'": "{{Path.Lookup data request.PathSegments.[1] 'N/A'}}", "Path.Lookup data request.PathSegments.[4] 'N/A'": "{{Path.Lookup data request.PathSegments.[4] 'N/A'}}", "Linq data 'it[\"1\"] ?? \"n/a\"'": "{{Linq data 'it[\"1\"] ?? \"n/a\"'}}", "Linq data 'it[\"9\"] ?? \"n/a\"'": "{{Linq data 'it[\"9\"] ?? \"n/a\"'}}", "DynamicLinq.Expression data 'it[\"1\"] ?? \"n/a\"'": "{{DynamicLinq.Expression data 'it[\"1\"] ?? \"n/a\"'}}", "DynamicLinq.Expression data 'it[\"9\"] ?? \"n/a\"'": "{{DynamicLinq.Expression data 'it[\"9\"] ?? \"n/a\"'}}" }, ``` And when calling the url: `http://localhost/data/1` Results into response body: ``` json { "DynamicLinq.Expression 'DateTime.Now.AddHours(1)'": "2023-03-01T19:10:04.2367715+01:00", "Path.Lookup data request.PathSegments.[1] 'N/A'": "one", "Path.Lookup data request.PathSegments.[4] 'N/A'": "N/A", "Linq data 'it[\"1\"] ?? \"n/a\"'": "one", "Linq data 'it[\"9\"] ?? \"n/a\"'": "n/a", "DynamicLinq.Expression data 'it[\"1\"] ?? \"n/a\"'": "one", "DynamicLinq.Expression data 'it[\"9\"] ?? \"n/a\"'": "n/a" } ```
Author
Owner

@StefH commented on GitHub (Mar 8, 2023):

@MichaelIDS Did you have time to verify this?

@StefH commented on GitHub (Mar 8, 2023): @MichaelIDS Did you have time to verify this?
Author
Owner

@MichaelIDS commented on GitHub (Mar 9, 2023):

Unfortunately, I am still waiting to get time allocated to development/testing of these new WireMock features.
We haven't reached the point when we specifically need to do this, I was just looking ahead when I asked the question :)

@MichaelIDS commented on GitHub (Mar 9, 2023): Unfortunately, I am still waiting to get time allocated to development/testing of these new WireMock features. We haven't reached the point when we specifically need to do this, I was just looking ahead when I asked the question :)
Author
Owner

@StefH commented on GitHub (Mar 9, 2023):

@MichaelIDS
No problem, I'll just merge the changes to master, and in the next days I'll create a new NuGet.

I'll close this issue and if you have another question, just raise a new issue.

@StefH commented on GitHub (Mar 9, 2023): @MichaelIDS No problem, I'll just merge the changes to master, and in the next days I'll create a new NuGet. I'll close this issue and if you have another question, just raise a new issue.
Author
Owner

@MichaelIDS commented on GitHub (Apr 3, 2023):

I know it's very late, but yes. the date modification works nicely, thanks. 👍

@MichaelIDS commented on GitHub (Apr 3, 2023): I know it's very late, but yes. the date modification works nicely, thanks. 👍
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#495