Skip to content

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

Open
@norpan

Description

@norpan

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);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions