[typescript-fetch] guard required date properties against null - #24509
Open
mvhenten wants to merge 1 commit into
Open
[typescript-fetch] guard required date properties against null#24509mvhenten wants to merge 1 commit into
mvhenten wants to merge 1 commit into
Conversation
A required date or date-time property was deserialized with an unguarded new Date(json[...]), turning a null value into 1970-01-01 and an absent value into Invalid Date, which then throws RangeError on serialization. Pass a nullish value through unchanged in both directions, as the template already does for every other required property type.
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.
A server returns
nullfor a property the schema marks required withformat: date. The generated client turns it into1970-01-01T00:00:00.000Z, a validDatethat flows into business logic unnoticed. An absent value becomesInvalid Date, and serializing that same payload back throwsRangeError: Invalid time value.#24215 added guards for the optional and nullable date cases. Required non-nullable dates were left unguarded; this completes that work.
Required date and date-time are the only property kinds in
modelGeneric.mustachethat fabricate a value fromnull. Every other required property passes the raw value through ('x': json['x'],'x': XFromJSON(json['x'])). Dates now do the same:nullstaysnull, absent stays absent. Passing the value through instead of forcingundefinedalso keeps the output compiling understrictNullChecks, sincejsonisanyand no cast is needed.A required date arriving as
nulldoes violate the schema. The failure mode for that should not be a plausible-looking wrong date.Generated code
export function FormatTestFromJSONTyped(json: any, ignoreDiscriminator: boolean): FormatTest { - 'date': (new Date(json['date'])), + 'date': (json['date'] == null ? json['date'] : new Date(json['date'])),export function FormatTestToJSONTyped(value?: FormatTest | null, ignoreDiscriminator: boolean = false): any { - 'date': value['date'].toISOString().substring(0,10), + 'date': value['date'] == null ? value['date'] : value['date'].toISOString().substring(0,10),Executed behaviour
Client generated from a spec with a required
dateand a requireddate-time, run throughFromJSONand back throughToJSON:{"requiredDate": null}1970-01-01T00:00:00.000Z→"1970-01-01"null→null{}Invalid Date→ throwsRangeError: Invalid time valueundefined→undefined{"requiredDate": "2024-03-05"}2024-03-05T00:00:00.000Z→"2024-03-05"Optional, nullable and required-nullable dates are unaffected, and both variants typecheck under
--strict.Sample output changes are limited to those four lines in
FormatTest(default-v3.0,kebab-case,snakecase-discriminator). No other typescript-fetch sample output changed.TypeScriptFetchClientCodegenTest.testRequiredDatesAreNullGuardedcovers the required date-time case, which no existing sample spec exercises.PR checklist
@leonyu
Summary by cubic
Fix null handling for required
dateanddate-timefields intypescript-fetchsonullor missing values pass through unchanged, avoiding fabricated dates and runtime errors.date/date-timeinmodelGeneric.mustachefor bothFromJSONandToJSON.nullstaysnull, absent staysundefined; prevents1970-01-01T00:00:00.000ZandRangeError: Invalid time value.testRequiredDatesAreNullGuardedandrequired-date.yaml; updatedFormatTestsamples only.Written for commit 28165e9. Summary will update on new commits.