Skip to content

Commit 7bd6369

Browse files
authored
fix(data_connect): avoid calling toJson on raw JSON map or null object (#17356)
* fix(data_connect): avoid calling toJson on raw JSON map or null object * chore: add null values to test case
1 parent 1c680eb commit 7bd6369

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/firebase_data_connect/firebase_data_connect/lib/src/any_value.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class AnyValue {
2727
return value;
2828
} else {
2929
if (value is List) {
30-
return (value as List).map((e) => e.toJson()).toList();
30+
return (value as List).map((e) => AnyValue(e).toJson()).toList();
3131
} else if (value is Map) {
3232
// TODO(mtewani): Throw an error if this is the wrong type.
3333
return convertMap(value as Map<String, dynamic>);

packages/firebase_data_connect/firebase_data_connect/test/src/any_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,28 @@ void main() {
5959
final any = AnyValue(map);
6060
expect(any.toJson(), equals(map));
6161
});
62+
test('constructor serializes List of map', () {
63+
final listOfMap = [
64+
{'a': 1, 'b': 2.0},
65+
{'c': 3, 'd': 4.0},
66+
{'e': 5, 'f': null},
67+
];
68+
final any = AnyValue(listOfMap);
69+
expect(any.toJson(), equals(listOfMap));
70+
});
71+
test('constructor serializes List of primitive type', () {
72+
final cases = [
73+
[1, 2, 3, 4, 5],
74+
[1.0, 2.0, 3.0, 4.0, 5.0],
75+
['a', 'b', 'c', 'd', 'e'],
76+
[true, false, true, false],
77+
[1, 2.0, null, 4],
78+
];
79+
80+
for (final list in cases) {
81+
final any = AnyValue(list);
82+
expect(any.toJson(), equals(list));
83+
}
84+
});
6285
});
6386
}

0 commit comments

Comments
 (0)