Skip to content

Commit ae33c43

Browse files
authored
Handle nullable values with genericArgumentFactories (#826)
Fixes #803
1 parent 3bf4564 commit ae33c43

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

json_serializable/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.0.3-dev
2+
3+
- Correctly handle nullable values with `genericArgumentFactories`.
4+
15
## 4.0.2
26

37
- Correctly handle nullable `Map` and `Iterable` JSON types exposed by both

json_serializable/lib/src/encoder_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract class EncodeHelper implements HelperCore {
3030
final helperName = toJsonForType(
3131
arg.instantiate(nullabilitySuffix: NullabilitySuffix.none),
3232
);
33-
buffer.write(',Object Function(${arg.name} value) $helperName');
33+
buffer.write(',Object? Function(${arg.name} value) $helperName');
3434
}
3535
if (element.typeParameters.isNotEmpty) {
3636
buffer.write(',');

json_serializable/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name: json_serializable
2-
version: 4.0.2
2+
version: 4.0.3-dev
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.
66
repository: https://github.com/google/json_serializable.dart/tree/master/json_serializable
77
environment:
88
# Keeping this <2.12.0 because the code is not null safe – yet!
9+
# https://github.com/google/json_serializable.dart/issues/821
910
sdk: '>=2.11.99 <3.0.0'
1011

1112
dependencies:

json_serializable/test/generic_files/generic_argument_factories.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class GenericClassWithHelpers<T, S> {
3030
_$GenericClassWithHelpersFromJson(json, fromJsonT, fromJsonS);
3131

3232
Map<String, dynamic> toJson(
33-
Object Function(T value) toJsonT,
34-
Object Function(S value) toJsonS,
33+
Object? Function(T value) toJsonT,
34+
Object? Function(S value) toJsonS,
3535
) =>
3636
_$GenericClassWithHelpersToJson(this, toJsonT, toJsonS);
3737
}
@@ -42,7 +42,10 @@ class ConcreteClass {
4242

4343
final GenericClassWithHelpers<double, BigInt> value2;
4444

45-
ConcreteClass(this.value, this.value2);
45+
// Regression scenario for google/json_serializable.dart#803
46+
final GenericClassWithHelpers<double?, BigInt?> value3;
47+
48+
ConcreteClass(this.value, this.value2, this.value3);
4649

4750
factory ConcreteClass.fromJson(Map<String, dynamic> json) =>
4851
_$ConcreteClassFromJson(json);

json_serializable/test/generic_files/generic_argument_factories.g.dart

Lines changed: 10 additions & 2 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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,56 @@ void main() {
108108
"someSet": [
109109
"2"
110110
]
111+
},
112+
"value3": {
113+
"value": 3.14,
114+
"list": [
115+
3.14
116+
],
117+
"someSet": [
118+
"2"
119+
]
120+
}
121+
}''';
122+
123+
final instance = ConcreteClass.fromJson(
124+
jsonDecode(inputJson) as Map<String, dynamic>,
125+
);
126+
127+
expect(loudEncode(instance), inputJson);
128+
});
129+
130+
test('round trip decode/decode with null', () {
131+
const inputJson = r'''
132+
{
133+
"value": {
134+
"value": 5,
135+
"list": [
136+
5
137+
],
138+
"someSet": [
139+
"string"
140+
]
141+
},
142+
"value2": {
143+
"value": 3.14,
144+
"list": [
145+
3.14
146+
],
147+
"someSet": [
148+
"2"
149+
]
150+
},
151+
"value3": {
152+
"value": null,
153+
"list": [
154+
3.14,
155+
null
156+
],
157+
"someSet": [
158+
"2",
159+
null
160+
]
111161
}
112162
}''';
113163

0 commit comments

Comments
 (0)