Skip to content

generic support#1552

Open
ManuelLeitner wants to merge 2 commits intogoogle:masterfrom
ManuelLeitner:master
Open

generic support#1552
ManuelLeitner wants to merge 2 commits intogoogle:masterfrom
ManuelLeitner:master

Conversation

@ManuelLeitner
Copy link

add support for generic JsonConverter, toJson and fromJson with arbitrary generic arguments

@google-cla
Copy link

google-cla bot commented Jan 31, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link

Summary of Changes

Hello @ManuelLeitner, 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 json_serializable's capabilities by introducing robust support for generic types in both custom JsonConverter implementations and direct toJson/fromJson functions. The changes ensure that the code generation process correctly interprets and applies generic arguments, leading to more accurate and flexible serialization/deserialization logic for complex data structures.

Highlights

  • Generic Function Type Handling: The _convertData function in type_helper_ctx.dart has been updated to correctly identify and instantiate generic function types, ensuring that toJson and fromJson methods with generic arguments are properly processed.
  • Generic JsonConverter Support: The _compatibleMatch function in json_converter_helper.dart now supports generic JsonConverter implementations by instantiating the converter's class type with its generic arguments, allowing for more flexible custom converters.
  • Analyzer Package Imports: New imports from the analyzer package (package:analyzer/src/dart/constant/value.dart and package:analyzer/dart/element/nullability_suffix.dart) were added to facilitate advanced type analysis for generic types.

🧠 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.

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 adds support for generic arguments in JsonConverter, toJson, and fromJson functions. The changes to support generic JsonConverters look good. However, to support generic toJson and fromJson functions, the implementation relies on internal APIs from the analyzer package, which is a significant maintenance risk. I've added a high-severity comment about this, along with a couple of medium-severity suggestions for code improvements.

Comment on lines +129 to +134
final state = (objectValue as DartObjectImpl).state;
if (((state as FunctionState).typeArguments?.length ?? 0) > 0) {
final generics = state.typeArguments!.cast<DartType>();
executableType = executableType.instantiate(generics);
qualifiedName += "<${generics.join(',')}>";
}

Choose a reason for hiding this comment

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

high

These changes rely on internal APIs from the analyzer package (DartObjectImpl, FunctionState), which are imported via package:analyzer/src/dart/constant/value.dart. These APIs are not stable and can break this package on any analyzer update, creating a significant maintenance risk.

Could you investigate if there's an alternative approach using only public APIs to retrieve the generic type arguments for the function? If this is the only way, the dependency on analyzer's internals should be clearly documented as a known risk.

var executableType = executableElement.type;
var qualifiedName = executableElement.qualifiedName;
final state = (objectValue as DartObjectImpl).state;
if (((state as FunctionState).typeArguments?.length ?? 0) > 0) {

Choose a reason for hiding this comment

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

medium

This condition can be expressed more idiomatically using isNotEmpty.

Suggested change
if (((state as FunctionState).typeArguments?.length ?? 0) > 0) {
if ((state as FunctionState).typeArguments?.isNotEmpty == true) {

final converterClassElement = converterClassType.element as ClassElement;
String? genericTypeArg;

final generics = (constantValue.type as InterfaceType).typeArguments;

Choose a reason for hiding this comment

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

medium

The converterClassType variable is already assigned constantValue.type as InterfaceType on line 285. You can reuse it here to avoid the redundant cast and access.

Suggested change
final generics = (constantValue.type as InterfaceType).typeArguments;
final generics = converterClassType.typeArguments;


import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/constant/value.dart';
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is NOT ideal...

Copy link
Collaborator

@kevmoo kevmoo left a comment

Choose a reason for hiding this comment

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

at a minimum we need a changelog entry and tests here!

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.

2 participants