Skip to content

Passing variable as ISO 8601 string generates invalid text output #6

@madmox

Description

@madmox

If I use a JsonElement object as the template parameters and set a variable as a ISO 8601 string, it generates an invalid output:

var handlebars = Handlebars.Create();
handlebars.Configuration.UseJson();

string template = @"{{Foo}}";
JsonElement parameters = JsonSerializer.SerializeToElement(new { Foo = "2023-05-05" });
HandlebarsTemplate<object, string> compiledTemplate = handlebars.Compile(template);
string output = compiledTemplate(parameters);

Console.WriteLine(output); // "2023-05-05T00:00:00.0000000" instead of "2023-05-05"

Incriminated portion of code is here : JSON strings should always be parsed as C# strings, because there is no unique string representation of the other types (DateTime, DateTimeOffset, Guid) and this necessarily results in altered textual representations in some cases. As a fix, I would just remove lines 40 to 47 in the previous method.

If someone uses 2023-05-05 as a username in my application, I don't want it to show as 2023-05-05T00:00:00.0000000 in the emails I send to him... Same is true if I register a key as C5B9CB1A-1370-4215-ABA1-560DB55B575A, I don't want it to be written as c5b9cb1a-1370-4215-aba1-560db55b575a (case might be sensitive!).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions