Skip to content

Commit 13fe0c8

Browse files
committed
Fix issue with nested generics and genericArgumentFactories: true
Fixes #1047
1 parent c03e84b commit 13fe0c8

File tree

6 files changed

+110
-1
lines changed

6 files changed

+110
-1
lines changed

json_serializable/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 6.1.2
2+
3+
- Fix issue with nested generics and `genericArgumentFactories: true`.
4+
([#1047](https://github.com/google/json_serializable.dart/issues/1047))
5+
16
## 6.1.1
27

38
- Fix `JsonKey.readValue` support to allow static functions.

json_serializable/lib/src/utils.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ String typeToCode(
205205
(type.isNullableType || forceNullable) ? '?' : '',
206206
].join();
207207
}
208+
209+
if (type is TypeParameterType) {
210+
return type.getDisplayString(withNullability: false);
211+
}
208212
throw UnimplementedError('(${type.runtimeType}) $type');
209213
}
210214

json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.1.1
2+
version: 6.1.2
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.

json_serializable/test/generic_files/generic_class.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,35 @@ class Issue980ParentClass {
143143
@override
144144
int get hashCode => const DeepCollectionEquality().hash(list);
145145
}
146+
147+
@JsonSerializable(genericArgumentFactories: true)
148+
class Issue1047ParentClass<T> {
149+
Issue1047ParentClass({required this.edges});
150+
151+
factory Issue1047ParentClass.fromJson(
152+
Map<String, dynamic> json, T Function(Object? json) fromJsonT) =>
153+
_$Issue1047ParentClassFromJson<T>(json, fromJsonT);
154+
155+
final List<Issue1047Class<T>> edges;
156+
157+
Map<String, dynamic> toJson(Object? Function(T value) toJsonT) =>
158+
_$Issue1047ParentClassToJson(this, toJsonT);
159+
}
160+
161+
@JsonSerializable(genericArgumentFactories: true)
162+
class Issue1047Class<T> {
163+
Issue1047Class({
164+
required this.node,
165+
});
166+
167+
factory Issue1047Class.fromJson(
168+
Map<String, dynamic> json,
169+
T Function(Object? json) fromJsonT,
170+
) =>
171+
_$Issue1047ClassFromJson<T>(json, fromJsonT);
172+
173+
final T node;
174+
175+
Map<String, dynamic> toJson(Object? Function(T value) toJsonT) =>
176+
_$Issue1047ClassToJson(this, toJsonT);
177+
}

json_serializable/test/generic_files/generic_class.g.dart

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/generic_files/generic_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,33 @@ void main() {
326326
(json) => Issue980ParentClass.fromJson(json),
327327
);
328328
});
329+
330+
test('issue 1047 regression', () {
331+
_roundTrip(
332+
sourceJson: {
333+
'edges': [
334+
{'node': '42'}
335+
]
336+
},
337+
genericEncode: (o) => o.toString(),
338+
genericDecode: (o) => int.parse(o as String),
339+
);
340+
});
341+
}
342+
343+
void _roundTrip<T>({
344+
required Map<String, dynamic> sourceJson,
345+
required Object? Function(T) genericEncode,
346+
required T Function(Object?) genericDecode,
347+
}) {
348+
final json = jsonEncode(sourceJson);
349+
350+
final instance2 = Issue1047ParentClass<T>.fromJson(
351+
jsonDecode(json) as Map<String, dynamic>,
352+
genericDecode,
353+
);
354+
355+
final json2 = jsonEncode(instance2.toJson(genericEncode));
356+
357+
expect(json2, json);
329358
}

0 commit comments

Comments
 (0)