Skip to content

Commit 451e7a4

Browse files
authored
fix(fdc): add support Int64 to nativeFromJson (#17673)
1 parent eb8716f commit 451e7a4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/firebase_data_connect/firebase_data_connect/lib/src/optional.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ T nativeFromJson<T>(dynamic input) {
9797
return DateTime.parse(input) as T;
9898
} else if (T == String) {
9999
return input as T;
100+
} else if (T == int) {
101+
// Int64 is transmitted as String.
102+
final value = BigInt.parse(input);
103+
if (value.isValidInt) {
104+
return value.toInt() as T;
105+
} else {
106+
throw UnsupportedError('BigInt ($input) too large for Dart int');
107+
}
100108
}
101109
} else if (input is num) {
102110
if (input is double && T == int) {
@@ -105,7 +113,7 @@ T nativeFromJson<T>(dynamic input) {
105113
return input.toDouble() as T;
106114
}
107115
}
108-
throw UnimplementedError('This type is unimplemented: ${T.runtimeType}');
116+
throw UnimplementedError('This type is unimplemented: $T');
109117
}
110118

111119
DynamicDeserializer<List<T>> listDeserializer<T>(

packages/firebase_data_connect/firebase_data_connect/test/src/optional_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ void main() {
8989
expect(nativeFromJson<int>(42), equals(42));
9090
expect(nativeFromJson<bool>(true), equals(true));
9191
expect(nativeFromJson<String>('Test'), equals('Test'));
92+
expect(nativeFromJson<int>('42000000000000'), equals(42000000000000));
93+
});
94+
95+
test('nativeFromJson throws UnsupportedError for bigint’s too big for int',
96+
() {
97+
expect(() => nativeFromJson<int>('42000000000000000000'),
98+
throwsUnsupportedError);
9299
});
93100

94101
test('nativeToJson correctly serializes null primitive types', () {

0 commit comments

Comments
 (0)