Skip to content

Comments

[dart] Preserve inner generic type for Map<String, List<T>> deserialization#22717

Merged
wing328 merged 3 commits intoOpenAPITools:masterfrom
EddieRydell:fix/dart-map-list-deserialization
Feb 19, 2026
Merged

[dart] Preserve inner generic type for Map<String, List<T>> deserialization#22717
wing328 merged 3 commits intoOpenAPITools:masterfrom
EddieRydell:fix/dart-map-list-deserialization

Conversation

@EddieRydell
Copy link
Contributor

@EddieRydell EddieRydell commented Jan 17, 2026

Fixes #19997

When generating Dart code for Map<String, List<T>> properties (e.g., Map<String, List<int>>), the fromJson deserialization was losing the inner generic type:

Previous code:

calendar: mapCastOfType<String, List>(json, r'calendar'),
// Results in Map<String, List<dynamic>> - inner type lost

Fixed code:

calendar: (json[r'calendar'] as Map<String, dynamic>).map(
  (k, v) => MapEntry(k, v == null ? const <T>[] : (v as List).cast<T>())),
  // Inner type T preserved

Changes

  • Updated native_class.mustache template to properly cast inner List types
  • Added map_of_array_integer test property to AdditionalPropertiesClass in the Dart petstore spec

Summary by cubic

Preserves the inner generic type when deserializing Map<String, List> in generated Dart models, preventing List in values. Fixes #19997 and ensures correct typing (e.g., Map<String, List>).

  • Bug Fixes
    • Updated dart2 native_class.mustache to map and cast list values (v as List).cast(), with null-safe defaults (empty list or null when nullable).
    • Added map_of_array_integer to the petstore spec to cover Map<String, List> and updated the generated model and docs.

Written for commit 923af7d. Summary will update on new commits.

@EddieRydell EddieRydell marked this pull request as ready for review January 17, 2026 18:53
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 4 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/resources/dart2/serialization/native/native_class.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/dart2/serialization/native/native_class.mustache:166">
P2: Map<String, List<Enum>> deserialization now casts lists to the enum type without converting JSON strings, causing a runtime TypeError for enum values.</violation>
</file>

<file name="samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/additional_properties_class.dart">

<violation number="1" location="samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/additional_properties_class.dart:38">
P2: hashCode uses map identity while == uses deep equality for mapOfArrayInteger, breaking equality contract</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@wing328 wing328 changed the title Preserve inner generic type for Map<String, List<T>> deserialization [dart] Preserve inner generic type for Map<String, List<T>> deserialization Jan 24, 2026
@EddieRydell
Copy link
Contributor Author

Hey @wing328, just checking in on this — CI is all green and I think I addressed the review comments. Is there anything else needed on my end?

I've got a couple more Dart deserialization fixes I'm planning to submit once this one lands, so would love to get it through when you get a chance. Thanks!

@wing328
Copy link
Member

wing328 commented Feb 19, 2026

thanks for the PR

cc @jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08)

@wing328 wing328 added this to the 7.21.0 milestone Feb 19, 2026
@wing328 wing328 merged commit 3252fcf into OpenAPITools:master Feb 19, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [Dart] Map<String,List<int>> wrong deserialization

2 participants