Repository navigation
TypeScript client: serialize date-only parameters as yyyy-MM-dd instead of toISOString() - #5406
Open
salahBouragba wants to merge 1 commit into
Open
salahBouragba wants to merge 1 commit into
salahBouragba wants to merge 1 commit into
Conversation
… of toISOString() (RicoSuter#5405)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the TypeScript client, a query, path or form parameter whose schema is
{ "type": "string", "format": "date" }is typedDatebut serialized withtoISOString()(ortoJSON()for form parameters), so the request carries a full instant where the contract says a date-only string. Servers binding a date-only type refuse it: ASP.NET CoreDateOnlyanswers an automatic 400 before the action runs, NodaTimeLocalDateand JavaLocalDatebehave the same. DTO properties with the same schema were already handled correctly through NJsonSchema'sformatDate(...).This change makes parameters behave like properties: a date-only parameter is serialized as its calendar day, a date-time parameter is unchanged.
Before:
After:
Closes #5405. Closes #2479. Closes #4295.
Why
Client.RequestUrl.liquidandClient.RequestBody.liquidbranch onparameter.IsDateOrDateTimeand emitparameter.GetDateTimeToString, which returnstoISOString()forDateTimeType.Datewithout looking at the schema format.ParameterModelBasealready exposesIsDateandIsDateArrayseparately from the date-time variants, so the templates now take those branches first:TypeScriptParameterModelgainsUseJsDate(dates are JavaScriptDateobjects, soformatDate()is used) andGetDateToString(the date-only call for MomentJS, DayJS and Luxon:format('YYYY-MM-DD'),toFormat('yyyy-MM-dd')), mirroring NJsonSchema'sDataConversionGeneratorfor properties.Client.RequestUrl.liquid:IsDateArrayandIsDatebranches for path and query parameters.Client.RequestBody.liquid: the same for url-encoded form parameters, and anIsDatebranch forFormData.TypeScriptClientGenerator.GenerateFileadds theformatDateutility artifact when client code references it and no DTO artifact already brought it. NJsonSchema'sTypeScriptGenerator.GenerateTypesonly scans DTO artifacts, so a client with a date-only parameter and no date-only property would otherwise call a function that does not exist.The
Stringdate type is not affected: such parameters are typedstring, soIsDateis false and they are passed through as before.Breaking changes
Generated clients change what they send for
format: dateparameters: the calendar day (2026-09-17, in the client's local time, asformatDatealready does for properties) instead of an instant (2026-09-17T07:55:42.262Z). A server that declaredformat: dateand binds a date-only type starts working; a server that declaredformat: datebut binds a date-time keeps working, since a date-only string parses as midnight. Only a server that relied on receiving the time part behind aformat: datedeclaration is affected, and its document was wrong.Two additive changes to public surface:
TypeScriptParameterModel.UseJsDateandTypeScriptParameterModel.GetDateToString. Custom templates that overrideClient.RequestUrl.liquidorClient.RequestBody.liquidkeep their own behavior.Verification
NSwag.CodeGeneration.TypeScript.Testsbuilds under the repository's warnings-as-errors settings and passes 63 of 63 tests, 4 of them new inDateParameterTests(path, query and array parameters withDate;formatDateemitted exactly once when a DTO property also needs it; DayJS formatting; url-encoded and multipart form parameters). Each generated client is also compiled withtscthroughTypeScriptCompiler.AssertCompile.format: dateparameter. The new tests assert the exact generated lines rather than snapshots.Documentation and samples updatedno document in the repository describes how parameters are serialized; the wiki page on TypeScript client settings could mention thatformat: dateparameters are sent as calendar days.