Skip to content

Commit

Permalink
fix(firestore, web): fix an issue where nested object could be incorr…
Browse files Browse the repository at this point in the history
…ectly decoded from JSObjects (#12289)
  • Loading branch information
Lyokone committed Feb 6, 2024
1 parent 22742ae commit 991f5bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ void runFieldValueTests() {
expect(snapshot.data()!['timestamp'], isA<Timestamp>());
expect(snapshot.data()!['nested']['timestamp'], isA<Timestamp>());
});

testWidgets('query should restore nested Timestamp in List', (_) async {
DocumentReference<Map<String, dynamic>> doc =
await initializeTest('nested-timestamp');
await doc.set({
'timestamp': Timestamp.fromDate(DateTime.now()),
'logs': [
{
'createdAt': Timestamp.fromDate(DateTime.now()),
},
],
});

final snapshot = await doc.get();

expect(snapshot.data()!['timestamp'], isA<Timestamp>());
expect(snapshot.data()!['logs'][0]['createdAt'], isA<Timestamp>());
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../firestore.dart';

/// Returns Dart representation from JS Object.
dynamic dartify(dynamic object) {
// Convert JSObject to Dart equivalents directly
if (object is! JSObject) {
return object;
}
Expand All @@ -32,28 +33,24 @@ dynamic dartify(dynamic object) {
return jsObject as BytesJsImpl;
}

// Convert nested structures
final dartObject = jsObject.dartify();
if (dartObject is List) {
return dartObject.map(dartify).toList();
}
return convertNested(dartObject);
}

if (dartObject is Map) {
final Map<String, dynamic> map = {};
for (final key in dartObject.keys) {
final value = dartObject[key];
if (value is Map) {
map[key as String] =
value.map((key, value) => MapEntry(key, dartify(value)));
} else if (value is List) {
map[key as String] = value.map(dartify).toList();
} else {
map[key as String] = dartify(value);
}
}
dynamic convertNested(dynamic object) {
if (object is List) {
return object.map(convertNested).toList();
} else if (object is Map) {
var map = <String, dynamic>{};
object.forEach((key, value) {
map[key] = convertNested(value);
});
return map;
} else {
// For non-nested types, attempt to convert directly
return dartify(object);
}

return dartObject;
}

/// Returns the JS implementation from Dart Object.
Expand Down

0 comments on commit 991f5bd

Please sign in to comment.