fix(dart): use .cast<T>() for primitive list/map fromJson#1
Merged
Conversation
… cast) jsonDecode always yields List<dynamic> / Map<String,dynamic> at runtime, so a direct `expr as List<String>` (or `as Map<String,T>`) cast throws "List<dynamic> is not a subtype of List<String>" even when every element matches. This crashed deserialization of any model with a primitive array — e.g. the login response `roles` field, breaking authentication in consumers. model_generator now emits `(expr as List).cast<T>()` and `(expr as Map).cast<K,V>()`, matching the convention already used in type_utils.dart for query params. Adds a regression assertion on the petstore `photoUrls` List<String> field. Co-Authored-By: Claude Opus 4.8 <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
jsonDecode(dart:convert) always returnsList<dynamic>for JSON arrays andMap<String, dynamic>for objects — it cannot know element types. A generatedexpr as List<String>therefore throws at runtime (type 'List<dynamic>' is not a subtype of type 'List<String>') even when every element is a String. The same applies to primitive maps.This broke
fromJsonfor any model with a primitive array. Concretely it crashed the login response (PRPVUserBaseFullLoginResponse.roles), so consumers could not authenticate.Fix
model_generatornow emits the idiomatic, type-safe form:(expr as List).cast<T>()(expr as Map).cast<String, V>()This matches the convention already used in
type_utils.dart(query-param lists). Object/ref/enum list paths are unchanged (they already usedList<T>.generate(... fromJson ...)).Tests
photoUrls(List<String>) field: the generatedfromJsonmust contain(json['photoUrls'] as List).cast<String>()and must NOT contain a naiveas List<String>.(json['roles'] as List).cast<String>()with zero naive casts remaining across 1015 files; login + downstream calls succeed.🤖 Generated with Claude Code