Skip to content

Commit

Permalink
fix(fdc): Fix serializing errors (#13450)
Browse files Browse the repository at this point in the history
* Fixed issues where int and double weren't being respected and errors weren't thrown from execute

* Added logging
  • Loading branch information
maneesht authored Oct 3, 2024
1 parent 2c86505 commit 9a5aba2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:developer';

import '../../firebase_data_connect.dart';
import '../common/common_library.dart';
Expand Down Expand Up @@ -111,7 +112,6 @@ class QueryRef<Data, Variables> extends OperationRef<Data, Variables> {
serializer(variables as Variables), this, res.data, null);
return res;
} on Exception catch (e) {
print(e);
await _queryManager.triggerCallback<Data, Variables>(
operationName, serializer(variables as Variables), this, null, e);
rethrow;
Expand All @@ -124,7 +124,12 @@ class QueryRef<Data, Variables> extends OperationRef<Data, Variables> {
.addQuery(operationName, variables, varsSerialized)
.cast<QueryResult<Data, Variables>>();
if (_queryManager.containsQuery(operationName, variables, varsSerialized)) {
this.execute().ignore();
try {
this.execute();
} catch (_) {
// Call to `execute` should properly pass the error to the Stream.
log("Error thrown by execute. The error will propagate via onError.");
}
}
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ T nativeFromJson<T>(dynamic input) {
} else if (T == String) {
return input as T;
}
} else if (input is num) {
if (input is double && T == int) {
return input.toInt() as T;
} else if (input is int && T == double) {
return input.toDouble() as T;
}
}
throw UnimplementedError('This type is unimplemented: ${T.runtimeType}');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ void main() {
expect(nativeFromJson<bool>(true), equals(true));
expect(nativeFromJson<String>('Test'), equals('Test'));
});

// Since protobuf doesn't distinguish between int and double, we need to do the parsing outselves
test('nativeFromJson correctly matches int to int and double to double',
() {
double expectedDouble = 42;
int expectedInt = 42;
expect(nativeFromJson<double>(42), equals(expectedDouble));
expect(nativeFromJson<int>(expectedDouble), equals(expectedInt));
});
test('nativeFromJson correctly deserializes DateTime strings', () {
expect(nativeFromJson<DateTime>('2024-01-01'),
equals(DateTime.parse('2024-01-01')));
Expand Down

0 comments on commit 9a5aba2

Please sign in to comment.