Skip to content

Commit af82671

Browse files
authored
Correctly handle nullable Map and Iterable JSON types... (#813)
..exposed by both class- and function-based converters Release v4.0.2
1 parent 2b796be commit af82671

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
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+
## 4.0.2
2+
3+
- Correctly handle nullable `Map` and `Iterable` JSON types exposed by both
4+
class- and function-based converters.
5+
16
## 4.0.1
27

38
- Allow latest `package:analyzer`.

json_serializable/lib/src/shared_checkers.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ String asStatement(DartType type) {
4343
return '';
4444
}
4545

46+
final nullableSuffix = type.isNullableType ? '?' : '';
47+
4648
if (coreIterableTypeChecker.isAssignableFromType(type)) {
4749
final itemType = coreIterableGenericType(type);
4850
if (isLikeDynamic(itemType)) {
49-
return ' as List';
51+
return ' as List$nullableSuffix';
5052
}
5153
}
5254

@@ -55,7 +57,7 @@ String asStatement(DartType type) {
5557
assert(args.length == 2);
5658

5759
if (args.every(isLikeDynamic)) {
58-
return ' as Map';
60+
return ' as Map$nullableSuffix';
5961
}
6062
}
6163

json_serializable/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: json_serializable
2-
version: 4.0.1
2+
version: 4.0.2
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.
6-
repository: https://github.com/google/json_serializable.dart
6+
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!
99
sdk: '>=2.11.99 <3.0.0'

json_serializable/test/json_serializable_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const _expectedAnnotatedTests = {
4747
'FinalFields',
4848
'FinalFieldsNotSetInCtor',
4949
'FromDynamicCollection',
50+
'FromNullableDynamicCollection',
5051
'GeneralTestClass1',
5152
'GeneralTestClass2',
5253
'GenericArgumentFactoriesFlagWithoutGenericType',

json_serializable/test/src/to_from_json_test_input.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,35 @@ class FromDynamicCollection {
170170
late String iterableField;
171171
}
172172

173+
String _fromNullableDynamicMap(Map? input) => '';
174+
175+
String _fromNullableDynamicList(List? input) => 'null';
176+
177+
String _fromNullableDynamicIterable(Iterable? input) => 'null';
178+
179+
@ShouldGenerate(
180+
r'''
181+
FromNullableDynamicCollection _$FromNullableDynamicCollectionFromJson(
182+
Map<String, dynamic> json) {
183+
return FromNullableDynamicCollection()
184+
..mapField = _fromNullableDynamicMap(json['mapField'] as Map?)
185+
..listField = _fromNullableDynamicList(json['listField'] as List?)
186+
..iterableField =
187+
_fromNullableDynamicIterable(json['iterableField'] as List?);
188+
}
189+
''',
190+
configurations: ['default'],
191+
)
192+
@JsonSerializable(createToJson: false)
193+
class FromNullableDynamicCollection {
194+
@JsonKey(fromJson: _fromNullableDynamicMap)
195+
late String mapField;
196+
@JsonKey(fromJson: _fromNullableDynamicList)
197+
late String listField;
198+
@JsonKey(fromJson: _fromNullableDynamicIterable)
199+
late String iterableField;
200+
}
201+
173202
String _noArgs() => throw UnimplementedError();
174203

175204
@ShouldThrow(

0 commit comments

Comments
 (0)