Dart: null-guard nullable date fields in toJson#2879
Merged
Conversation
For a nullable "date" transformed-string field, the Dart renderer's
toJson emitted a template string over `x!.year` / `x!.month` / `x!.day`
with no null check, so serializing an instance whose date field is null
threw "Null check operator used on a null value" at runtime. Nullable
date-time fields were already handled correctly with
`?.toIso8601String()`.
Emit `x == null ? null : "${x!...}"` in the null-safe nullable branch,
mirroring the null guards used in fromJson.
Adds test/inputs/json/priority/bug2590.json, a fixture input with a
nullable yyyy-mm-dd date field, so the dart fixture round-trips it in
CI.
Fixes #2590
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Problem
Generated Dart code for a nullable date field (a
yyyy-mm-ddstring that is sometimesnull) crashes at runtime when serializing an instance whose date isnull:The generated
toJsonlooked like this:Nullable date-time fields were handled correctly (
?.toIso8601String()); only the "date" transformed-string kind used the null-assertion operator with no guard. This happens with default options, compiles fine, and only fails at runtime.Root cause
In
packages/quicktype-core/src/language/Dart/DartRenderer.ts,toDynamicExpression's"date"case has a dedicated branch for null-safe nullable dates, but that branch emitted the same unguarded template string as the non-nullable case, just with!inserted before.year/.month/.day.Fix
Prefix the template string with a null guard in the nullable null-safe branch, mirroring the
== null ? null :convention already used by the generatedfromJson:The non-null-safe (
--null-safety false) and--required-propscode paths are unchanged.Tests
The regression test was added first and demonstrated failing before the fix. New fixture input
test/inputs/json/priority/bug2590.json— an object with a small array whose elements have a nullable date ("2023-01-15"/null) and a nullable date-time. Generating Dart from it on unfixed master produced the unguardedwhich throws
Null check operator used on a null valuewhen the dart fixture'sparser.dartround-trips the second array element ("dueDate": null) throughtopLevelToJson. After the fix the generated line is null-guarded and the round trip yieldsnullfor that field.Verification
!.year) vs post-fix (dueDate == null ? null : "..."); nullable date-time (?.toIso8601String()) and non-nullable paths unchanged.FIXTURE=typescript,javascript,golang,rust script/test test/inputs/json/priority/bug2590.json— all pass, so the new priority input doesn't destabilize those languages.Fixes #2590
🤖 Generated with Claude Code