Skip to content
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

Generic argument factories does not work with toJson when using mixin #1208

Open
norpan opened this issue Sep 23, 2022 · 4 comments
Open

Generic argument factories does not work with toJson when using mixin #1208

norpan opened this issue Sep 23, 2022 · 4 comments

Comments

@norpan
Copy link

norpan commented Sep 23, 2022

The following file throws in the generator (probably because it tries to compare the type Child<T> with ChildMixin<T>).
I also reported this at rrousselGit/freezed#766 but perhaps it's something that can be fixed in json_serializable.

If I move the toJson method to the Child class, it works fine.

build_runner build -v

[SEVERE] json_serializable on lib/src/bug.dart:

RangeError (index): Invalid value: Only valid value is 0: -1
dart:core                                                           List.[]
package:json_serializable/src/type_helpers/json_helper.dart 158:39  _helperParams
package:json_serializable/src/type_helpers/json_helper.dart 51:9    JsonHelper.serialize
package:json_serializable/src/type_helper_ctx.dart 56:31            TypeHelperCtx.serialize.<fn>
dart:core                                                           Iterable.firstWhere
package:json_serializable/src/type_helper_ctx.dart 88:46            TypeHelperCtx._run
package:json_serializable/src/type_helper_ctx.dart 53:64            TypeHelperCtx.serialize
package:json_serializable/src/encoder_helper.dart 153:12            EncodeHelper._serializeField
package:json_serializable/src/encoder_helper.dart 81:42             EncodeHelper._writeToJsonSimple.<fn>
dart:core                                                           StringBuffer.writeAll
package:json_serializable/src/encoder_helper.dart 78:9              EncodeHelper._writeToJsonSimple
package:json_serializable/src/encoder_helper.dart 66:7              EncodeHelper.createToJson
dart:_internal                                                      WhereIterator.moveNext
package:json_serializable/src/json_part_builder.dart 64:27          _UnifiedGenerator.generate
package:source_gen/src/builder.dart 352:23                          _generate

pubspec.yaml

environment:
  sdk: '>=2.18.1 <3.0.0'

dev_dependencies:
  build_runner: ^2.2.1
  json_serializable: ^6.4.0
dependencies:
  json_annotation: ^4.7.0

bug.dart

import 'package:json_annotation/json_annotation.dart';

part 'bug.g.dart';

@JsonSerializable()
class Person {
  final Child<String> firstName;

  Person({required this.firstName});

  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
  Map<String, dynamic> toJson() => _$PersonToJson(this);
}

@JsonSerializable(genericArgumentFactories: true)
class Child<T> with ChildMixin<T> {
  final T content;

  Child({required this.content});

  factory Child.fromJson(
          Map<String, dynamic> json, T Function(Object? json) fromJsonT) =>
      _$ChildFromJson<T>(json, fromJsonT);
}

mixin ChildMixin<T> {
  Map<String, dynamic> toJson(Object Function(T) toJsonT) =>
      _$ChildToJson(this, toJsonT);
}
@kevmoo
Copy link
Collaborator

kevmoo commented Sep 29, 2022

Gah! You're doing crazy stuff here, yo! I'll look. But this is pretty weird...

@kevmoo
Copy link
Collaborator

kevmoo commented Sep 30, 2022

I dug in longer than I should have. We have to map generic types across to the mixin, which is CRAZY complex. There isn't a clean, clear way to resolve this, sadly. 🤷

@norpan
Copy link
Author

norpan commented Sep 30, 2022

I dug in longer than I should have. We have to map generic types across to the mixin, which is CRAZY complex. There isn't a clean, clear way to resolve this, sadly. 🤷

Thanks for looking into this! Yeah, I tried to understand it too, but I'm pretty new to Dart :)
For now it works to @override the toJson method in the Child class, because mixin is a form of inheritance. I guess it's something that needs to be documented in freezed.

@TekExplorer
Copy link

Its pretty weird that generic argument factories doesnt work when the toJson is specified in a mixin, but without generic factories it works fine?

shouldnt it look up the method first so it doesnt break?

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

No branches or pull requests

3 participants