Skip to content

Improve error message when generating code for Record types with unsupported fields#1561

Draft
kevmoo wants to merge 3 commits intomasterfrom
i1559_record_fun
Draft

Improve error message when generating code for Record types with unsupported fields#1561
kevmoo wants to merge 3 commits intomasterfrom
i1559_record_fun

Conversation

@kevmoo
Copy link
Collaborator

@kevmoo kevmoo commented Feb 18, 2026

Fixes #1559

json_serializable/lib/src/utils.dart: Updated typeToCode to properly handle RecordType. Now correctly formats record types (including nullable and nested records) and falls back to getDisplayString() for unsupported types like FunctionType instead of throwing UnimplementedError.

json_serializable/test/src/_json_serializable_test_input.dart: Added regression tests for records with unsupported types and multiple converters on records.

json_serializable/test/json_serializable_test.dart: Added new tests to the expected test list.

json_serializable/CHANGELOG.md: Added changelog entry.

…pported fields

Fixes #1559

json_serializable/lib/src/utils.dart: Updated typeToCode to properly handle RecordType. Now correctly formats record types (including nullable and nested records) and falls back to getDisplayString() for unsupported types like FunctionType instead of throwing UnimplementedError.

json_serializable/test/src/_json_serializable_test_input.dart: Added regression tests for records with unsupported types and multiple converters on records.

json_serializable/test/json_serializable_test.dart: Added new tests to the expected test list.

json_serializable/CHANGELOG.md: Added changelog entry.
@gemini-code-assist
Copy link

Summary of Changes

Hello @kevmoo, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the json_serializable package's handling of Dart Record types. It refines the code generation process to provide more informative error messages when unsupported field types are encountered within records and ensures proper formatting for various record structures. Additionally, it expands the test suite to cover these new error conditions and converter interactions, improving the overall reliability and developer experience when working with records.

Highlights

  • Enhanced Record Type Error Handling: Improved the error message generated when json_serializable encounters Record types with unsupported fields, providing clearer diagnostics.
  • typeToCode Function Update: Modified the typeToCode utility function to correctly format RecordType instances, including nullable and nested records, and to gracefully handle unsupported types like FunctionType by using getDisplayString() instead of throwing an UnimplementedError.
  • Comprehensive Record Type Testing: Introduced new regression tests covering Record types with unsupported field types (e.g., functions) and scenarios involving multiple JsonConverter annotations on record fields, ensuring robust behavior.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • json_serializable/CHANGELOG.md
    • Added a changelog entry for improved error messages for Record types.
  • json_serializable/lib/src/utils.dart
    • Updated typeToCode to properly handle RecordType and fall back to getDisplayString() for unsupported types.
  • json_serializable/test/json_serializable_test.dart
    • Added new test names to the list of expected annotated tests.
  • json_serializable/test/src/_json_serializable_test_input.dart
    • Added new test classes for records with unsupported function types and multiple converters.
Activity
  • No human activity (comments, reviews, etc.) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request improves error handling for Record types with unsupported fields by updating typeToCode to fall back to getDisplayString() instead of throwing an UnimplementedError. This allows for more informative error messages. The change also includes a robust implementation for converting RecordType to its string representation and adds comprehensive regression tests for records with unsupported types and for cases with multiple converters. My feedback focuses on improving the maintainability of the new code.

@kevmoo kevmoo marked this pull request as draft February 18, 2026 21:29
}

@ShouldThrow(
'Could not generate `fromJson` code for `record` because of type '
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Too many test cases in here!

Really only need one for the bad type and one for the converter bit!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typeToCode breaks when presented with records

1 participant