Skip to content

Commit 02b57ff

Browse files
committed
fix #104 generate valid lists in strong mode
1 parent 08299eb commit 02b57ff

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

example/example.g.dart

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

lib/generators/json_serializable_generator.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ String _writeAccessToVar(String varExpression, DartType searchType,
232232

233233
var itemVal = "v$depth";
234234

235-
var output = "($varExpression as List)?.map(($itemVal) =>"
235+
var output = "($varExpression as List)?.map(($itemVal) => "
236236
"${_writeAccessToVar(itemVal, iterableGenericType, depth: depth+1)}"
237237
")";
238238

@@ -243,6 +243,10 @@ String _writeAccessToVar(String varExpression, DartType searchType,
243243
return output;
244244
}
245245

246+
if (!searchType.isDynamic) {
247+
return "$varExpression as $searchType";
248+
}
249+
246250
return varExpression;
247251
}
248252

test/json_serializable_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ void main() {
101101

102102
expect(output, contains('(json[\'children\'] as List)?.map('));
103103
});
104+
105+
test('class with list of int is cast for strong mode', () async {
106+
var element = await _getClassForCodeString('Person');
107+
var output = await _generator.generate(element, null);
108+
109+
expect(output, isNotNull);
110+
111+
expect(output,
112+
contains("json['listOfInts'] as List)?.map((v0) => v0 as int)"));
113+
});
104114
});
105115

106116
test('reads JsonKey annotations', () async {
@@ -154,6 +164,7 @@ class Person {
154164
DateTime dateOfBirth;
155165
dynamic dynamicType;
156166
var varType;
167+
List<int> listOfInts;
157168
}
158169
159170
@JsonSerializable()

test/test_files/json_test_example.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Item extends Object with _$ItemSerializerMixin {
5454
final int price;
5555
int itemNumber;
5656
List<DateTime> saleDates;
57+
List<int> rates;
5758

5859
Item([this.price]);
5960

test/test_files/json_test_example.g.dart

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

0 commit comments

Comments
 (0)