-
-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON generation not working for use of generic class #766
Comments
It seems to be an issue with json_serializable when generating the toJson. It tries to find the matching type but compares the type T from _$ApiResonse with the type T from ApiResponse and finds them not equal. It seems to be because of the use of mixin, because if I move the generated mixin into the ApiResponse class, everything works. There is no error if you @Freezed(genericArgumentFactories: true)
class ApiResponse<T> with _$ApiResponse<T> {
const factory ApiResponse.data(T data) = ApiResponseData;
factory ApiResponse.fromJson(
Map<String, dynamic> json, T Function(Object?) fromJsonT) =>
_$ApiResponseFromJson(json, fromJsonT);
@override
Map<String, dynamic> toJson(Object? Function(T) toJsonT) =>
throw _privateConstructorUsedError;
} |
I have confirmed that when running the build runner, I get |
Not caused error
Caused error
|
Any updates here? I am running into this issue as well. My use case is slightly difference from the above, but still deals with nested generics.
import 'package:freezed_annotation/freezed_annotation.dart';
part 'inner.freezed.dart';
part 'inner.g.dart';
@Freezed(genericArgumentFactories: true)
class Inner<T> with _$Inner<T> {
const factory Inner({
required T data,
}) = _Inner<T>;
factory Inner.fromJson(
Map<String, dynamic> json, T Function(Object?) fromJsonT) =>
_$InnerFromJson(json, fromJsonT);
}
import 'path/to/inner.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'outer.freezed.dart';
part 'outer.g.dart';
@Freezed(genericArgumentFactories: true)
class Outer<T, U> with _$Outer<T, U> {
const factory Outer({
required T firstType,
required Inner<U> secondType,
}) = _Outer<T, U>;
factory Outer.fromJson(
Map<String, Object?> json,
T Function(Object?) fromJsonT,
U Function(Object?) fromJsonU,
) =>
_$OuterFromJson(json, fromJsonT, fromJsonU);
} During codegen the
Note that if I change the type of I've seen this mentioned by @rrousselGit here: google/json_serializable.dart#870 (comment), but I am unclear on the solution (if any) to the issue. Thanks in advance! |
I created a test repo to make this easy to reproduce, and have also figured out a temporary workaround. There are more details in the readme, but here's the TL;DR: To make codegen work for a nested freezed class with generics, simply comment out the import for the nested class. Using my example above, comment out the line:
Run codegen, then uncomment the line. That should fix syntax errors, but freezed won't have generated the proper code for the You can see the differences in my example repository: The resulting code works as expected, but obviously this isn't a great workaround because it gets overwritten by subsequent code generation. Hopefully this helps pinpoint the issue! |
@ztaylor54 you legend, thanks for putting everything in one place! Confirming having the same issue running
Having the same issue with putting generic
Wrapping class
Removing the
Please fix this issue asap. |
I had the same problem, but I solved it after reading this comment #766 (comment) solved issue after remove import line of child class |
I am having this issue with a none generic version of Outer from above. It is a pretty common use case - any custom generic contained within a freezed class. Outer in my case looks like this: ` part 'outer.freezed.dart'; @freezed factory Outer.fromJson(Map<String, Object?> json) => _$OuterFromJson(json); Could someone please look at solving this one. |
Still no update on this problem ?
|
The issue can be worked around by adding class Foo<T> with _$Foo<T> {
...
toJson(toJsonT, /*other generics*/);
} To the generic class itself. This is a bug with how Json serializable tries and fails to notice the toJson in the mixin. This adds the function to the interface directly, so that the generator cannot miss it. No, you do not need a body. The mixin supplies that. We're just declaring that it exists. |
Describe the bug
JSON generation fails when using the generic class in another class. I've followed the instructions at https://pub.dev/packages/freezed#deserializing-generic-classes I think.
I get the following output:
To Reproduce
pubspec.yaml
bug.dart
The text was updated successfully, but these errors were encountered: