From 35ae59e222baab22069914103f4b679615b0b3b3 Mon Sep 17 00:00:00 2001 From: Amr Yousef Date: Sun, 9 Feb 2020 21:44:11 +0000 Subject: [PATCH] [cloud_firestore] Fix crashes when using DocumentReference with Query (#1955) * Encode values using in Query#where --- .../cloud_firestore/CHANGELOG.md | 4 ++ .../example/test_driver/cloud_firestore.dart | 62 +++++++++++++++++++ .../cloud_firestore/lib/src/query.dart | 20 +++--- .../cloud_firestore/pubspec.yaml | 2 +- 4 files changed, 78 insertions(+), 10 deletions(-) diff --git a/packages/cloud_firestore/cloud_firestore/CHANGELOG.md b/packages/cloud_firestore/cloud_firestore/CHANGELOG.md index 44d409c0ab68..b6ba3c4ae580 100644 --- a/packages/cloud_firestore/cloud_firestore/CHANGELOG.md +++ b/packages/cloud_firestore/cloud_firestore/CHANGELOG.md @@ -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`. diff --git a/packages/cloud_firestore/cloud_firestore/example/test_driver/cloud_firestore.dart b/packages/cloud_firestore/cloud_firestore/example/test_driver/cloud_firestore.dart index 3cf96d55d199..85b31b057c3e 100644 --- a/packages/cloud_firestore/cloud_firestore/example/test_driver/cloud_firestore.dart +++ b/packages/cloud_firestore/cloud_firestore/example/test_driver/cloud_firestore.dart @@ -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({ + 'children': [ + ref.document("test-docRef-1"), + ref.document("test-docRef-2") + ], + }); + + final QuerySnapshot snapshot = await ref.where('children', + arrayContainsAny: [ + ref.document("test-docRef-1"), + ref.document("test-docRef-2") + ]).getDocuments(); + final List 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: [ + ref.document("test-docRef-1"), + ref.document("test-docRef-2") + ]).getDocuments(); + final List 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({ + 'children': [ + 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 results = snapshot.documents; + expect(results.length, 1); + final DocumentSnapshot actual = results[0]; + expect(actual.documentID, 'test-docRef'); + }); }); } diff --git a/packages/cloud_firestore/cloud_firestore/lib/src/query.dart b/packages/cloud_firestore/cloud_firestore/lib/src/query.dart index 513cfce5b554..70eb596ebc4c 100644 --- a/packages/cloud_firestore/cloud_firestore/lib/src/query.dart +++ b/packages/cloud_firestore/cloud_firestore/lib/src/query.dart @@ -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); diff --git a/packages/cloud_firestore/cloud_firestore/pubspec.yaml b/packages/cloud_firestore/cloud_firestore/pubspec.yaml index 3b2af29f6834..1c1765b5c65a 100755 --- a/packages/cloud_firestore/cloud_firestore/pubspec.yaml +++ b/packages/cloud_firestore/cloud_firestore/pubspec.yaml @@ -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: