Skip to content

Commit

Permalink
[cloud_firestore] Fix crashes when using DocumentReference with Query (
Browse files Browse the repository at this point in the history
…firebase#1955)

* Encode values using in Query#where
  • Loading branch information
amrfarid140 authored Feb 9, 2020
1 parent 1b45082 commit 35ae59e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/cloud_firestore/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.1+1

* Fixed crashes when using `Query#where` with `DocumentReference` objects

## 0.13.1

* Migrate to `cloud_firestore_platform_interface`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,67 @@ void main() {
expect(snapshot1.documentID, 'la');
expect(snapshot2.documentID, 'tokyo');
});

test('Query.whereArrayContainsAny using DocumentReference', () async {
final CollectionReference ref = firestore.collection('messages');
await ref.document('test-docRef-1').setData({"message": "1"});
await ref.document('test-docRef-2').setData({"message": "2"});

await ref.document('test-docRef').setData(<String, dynamic>{
'children': <DocumentReference>[
ref.document("test-docRef-1"),
ref.document("test-docRef-2")
],
});

final QuerySnapshot snapshot = await ref.where('children',
arrayContainsAny: <DocumentReference>[
ref.document("test-docRef-1"),
ref.document("test-docRef-2")
]).getDocuments();
final List<DocumentSnapshot> results = snapshot.documents;
expect(results.length, 1);
final DocumentSnapshot actual = results[0];
expect(actual.documentID, 'test-docRef');
});

test('Query.whereIn using DocumentReference', () async {
final CollectionReference ref = firestore.collection('messages');
await ref.document('test-docRef-1').setData({"message": "1"});
await ref.document('test-docRef-2').setData({"message": "2"});

final QuerySnapshot snapshot = await ref.where(FieldPath.documentId,
whereIn: <DocumentReference>[
ref.document("test-docRef-1"),
ref.document("test-docRef-2")
]).getDocuments();
final List<DocumentSnapshot> results = snapshot.documents;
expect(results.length, 2);
expect(results.where((item) => item.documentID == "test-docRef-1").length,
equals(1));
expect(results.where((item) => item.documentID == "test-docRef-2").length,
equals(1));
});

test('Query.arrayContains using DocumentReference', () async {
final CollectionReference ref = firestore.collection('messages');
await ref.document('test-docRef-1').setData({"message": "1"});
await ref.document('test-docRef-2').setData({"message": "2"});

await ref.document('test-docRef').setData(<String, dynamic>{
'children': <DocumentReference>[
ref.document("test-docRef-1"),
ref.document("test-docRef-2")
],
});

final QuerySnapshot snapshot = await ref
.where('children', arrayContains: ref.document("test-docRef-1"))
.getDocuments();
final List<DocumentSnapshot> results = snapshot.documents;
expect(results.length, 1);
final DocumentSnapshot actual = results[0];
expect(actual.documentID, 'test-docRef');
});
});
}
20 changes: 11 additions & 9 deletions packages/cloud_firestore/cloud_firestore/lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ class Query {
bool isNull,
}) =>
Query._(
_delegate.where(field,
isEqualTo: isEqualTo,
isLessThan: isLessThan,
isLessThanOrEqualTo: isLessThanOrEqualTo,
isGreaterThan: isGreaterThan,
isGreaterThanOrEqualTo: isGreaterThanOrEqualTo,
arrayContainsAny: arrayContainsAny,
arrayContains: arrayContains,
whereIn: whereIn,
_delegate.where(_CodecUtility.valueEncode(field),
isEqualTo: _CodecUtility.valueEncode(isEqualTo),
isLessThan: _CodecUtility.valueEncode(isLessThan),
isLessThanOrEqualTo:
_CodecUtility.valueEncode(isLessThanOrEqualTo),
isGreaterThan: _CodecUtility.valueEncode(isGreaterThan),
isGreaterThanOrEqualTo:
_CodecUtility.valueEncode(isGreaterThanOrEqualTo),
arrayContainsAny: _CodecUtility.valueEncode(arrayContainsAny),
arrayContains: _CodecUtility.valueEncode(arrayContains),
whereIn: _CodecUtility.valueEncode(whereIn),
isNull: isNull),
firestore);

Expand Down
2 changes: 1 addition & 1 deletion packages/cloud_firestore/cloud_firestore/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description:
Flutter plugin for Cloud Firestore, a cloud-hosted, noSQL database with
live synchronization and offline support on Android and iOS.
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/cloud_firestore/cloud_firestore
version: 0.13.1
version: 0.13.1+1

flutter:
plugin:
Expand Down

0 comments on commit 35ae59e

Please sign in to comment.