quicktype
quicktype copied to clipboard
[BUG]: Dart enum parsing throws on nullable values
Issue Type
Output Generation
Context (Environment, Version, Language)
Input Format: JSON Schema
Output Language: Dart
CLI, npm, or app.quicktype.io: app.quicktype.io (also reproducible via CLI)
Version: Latest as of April 2025
Description
When generating Dart classes from a JSON Schema where a property is a nullable enum (e.g., CommandShortcutType?), the generated fromMap() function forcefully accesses the enum map with a non-null assertion operator (!), which throws a runtime exception if the value is null in the input JSON.
This makes the output unusable for any optional enum fields, even though the Dart class correctly marks the property as nullable.
Input Data
{
"type": "object",
"properties": {
"shortcutType": {
"type": "string",
"enum": ["standard", "view"]
}
},
"required": []
}
Expected Behaviour / Output
Generated Dart code should respect nullability in fromMap() by safely handling null values for optional enum fields. Avoid using ! when accessing the enum map if the corresponding property is nullable.
Expected snippet:
shortcutType: json["shortcutType"] != null
? shortcutTypeValues.map[json["shortcutType"]]
: null,
or
shortcutType: shortcutTypeValues.map[json["shortcutType"]]
// expect(json["shortcutType"], null)
// expect(shortcutTypeValues.map[null], null)
Current Behaviour / Output
The current output forcefully unwraps the enum value, even when the Dart type is nullable:
commandShortcutType: commandShortcutTypeValues.map[json["commandShortcutType"]]!,
This causes a runtime crash when the input JSON omits or explicitly sets the value to null.
Steps to Reproduce
Open https://app.quicktype.io/ . Paste the provided JSON Schema. Select output language as Dart. Observe that the generated fromMap() uses ! with optional enum types.
Possible Solution
packages/quicktype-core/src/language/Dart/DartRenderer.ts
-- this._options.nullSafety ? "]!" : "]"
++ (this._options.nullSafety && !isNullable) ? "]!" : "]"