frhagn / Typewriter

Automatic TypeScript template generation from C# source files
http://frhagn.github.io/Typewriter
Apache License 2.0
536 stars 132 forks source link

Url parameters of type DateTime[Offset] should be urlencoded #327

Open rodro75 opened 4 years ago

rodro75 commented 4 years ago

This may be a corner case, but if you define a controller method like this one:

[Route("foo")]
public async Task FooAsync(DateTimeOffset? timestamp = null)

the typescript code generated by the built-in $Url directive is:

`/foo?timestamp=${timestamp}`

but it should really be:

`/foo?timestamp=${encodeURIComponent(String(timestamp))}`

because dates may contain the "+" character for the timezone part when serialized.

Now, because in urls the "+" character is historically interpreted as a space, when timestamps like "2020-02-04T19:25:00+01:00" or "Tue Feb 4 2020 19:25:00 GMT+0100" are sent back to the server ASP.Net would decode them as "2020-02-04T19:25:00 01:00" or "Tue Feb 4 2020 19:25:00 GMT 0100". In both cases the Json deserializer won't recognize the string as a valid date, so the WebApi infrastructure will just pass null into the parameter (or send back a 404 in case the parameter didn't have a default value).