From cff6f7674014037688815bdbe3198dd903a4b08e Mon Sep 17 00:00:00 2001 From: Russell Wheatley Date: Thu, 25 Jan 2024 15:36:11 +0000 Subject: [PATCH] fix(firestore): revert breaking change to where() API. `null` cannot be used for `isEqualTo` or `isNotEqualTo` in a query. (#12164) --- .../example/integration_test/query_e2e.dart | 126 ------------------ .../cloud_firestore/lib/src/query.dart | 24 ++-- 2 files changed, 8 insertions(+), 142 deletions(-) diff --git a/packages/cloud_firestore/cloud_firestore/example/integration_test/query_e2e.dart b/packages/cloud_firestore/cloud_firestore/example/integration_test/query_e2e.dart index 37179758e6ad..120066ce12a4 100644 --- a/packages/cloud_firestore/cloud_firestore/example/integration_test/query_e2e.dart +++ b/packages/cloud_firestore/cloud_firestore/example/integration_test/query_e2e.dart @@ -1813,132 +1813,6 @@ void runQueryTests() { expect(snapshot.docs.length, equals(1)); expect(snapshot.docs[0].get('foo'), equals(ref)); }); - - testWidgets('pass `null` to `isEqualTo`', (_) async { - CollectionReference> collection = - await initializeTest('where-null-isEqualTo'); - - await Future.wait([ - collection.add({ - 'foo': 1, - }), - collection.add({'foo': 2}), - collection.add({ - 'foo': null, - }), - collection.add({ - 'foo': null, - }), - ]); - - QuerySnapshot> snapshot = - await collection.where('foo', isEqualTo: null).get(); - - expect(snapshot.docs.length, equals(2)); - expect(snapshot.docs[0].get('foo'), equals(null)); - expect(snapshot.docs[1].get('foo'), equals(null)); - }); - - testWidgets('pass `null` to `isNotEqualTo`', (_) async { - CollectionReference> collection = - await initializeTest('where-null-isNotEqualTo'); - - await Future.wait([ - collection.add({ - 'foo': 11, - }), - collection.add({'foo': 11}), - collection.add({ - 'foo': null, - }), - collection.add({ - 'foo': null, - }), - ]); - - QuerySnapshot> snapshot = - await collection.where('foo', isNotEqualTo: null).get(); - - expect(snapshot.docs.length, equals(2)); - expect(snapshot.docs[0].get('foo'), equals(11)); - expect(snapshot.docs[1].get('foo'), equals(11)); - }); - - testWidgets( - 'pass `null` to `isNotEqualTo` and ignore other `null` value queries', - (_) async { - CollectionReference> collection = - await initializeTest( - 'where-null-isNotEqualTo-ignore-other-queries', - ); - - await Future.wait([ - collection.add({ - 'foo': 11, - }), - collection.add({'foo': 11}), - collection.add({ - 'foo': null, - }), - collection.add({ - 'foo': null, - }), - ]); - - QuerySnapshot> snapshot = await collection - .where( - 'foo', - isNotEqualTo: null, - isGreaterThan: null, - isLessThan: null, - isLessThanOrEqualTo: null, - arrayContains: null, - isGreaterThanOrEqualTo: null, - ) - .get(); - - expect(snapshot.docs.length, equals(2)); - expect(snapshot.docs[0].get('foo'), equals(11)); - expect(snapshot.docs[1].get('foo'), equals(11)); - }); - - testWidgets( - 'pass `null` to `isEqualTo` and ignore other `null` value queries', - (_) async { - CollectionReference> collection = - await initializeTest( - 'where-null-isEqualTo-ignore-other-queries', - ); - - await Future.wait([ - collection.add({ - 'foo': 1, - }), - collection.add({'foo': 2}), - collection.add({ - 'foo': null, - }), - collection.add({ - 'foo': null, - }), - ]); - - QuerySnapshot> snapshot = await collection - .where( - 'foo', - isEqualTo: null, - isGreaterThan: null, - isLessThan: null, - isLessThanOrEqualTo: null, - arrayContains: null, - isGreaterThanOrEqualTo: null, - ) - .get(); - - expect(snapshot.docs.length, equals(2)); - expect(snapshot.docs[0].get('foo'), equals(null)); - expect(snapshot.docs[1].get('foo'), equals(null)); - }); }); group('Query.where() with Filter class', () { diff --git a/packages/cloud_firestore/cloud_firestore/lib/src/query.dart b/packages/cloud_firestore/cloud_firestore/lib/src/query.dart index 8057b578dd9e..272a4c279d12 100644 --- a/packages/cloud_firestore/cloud_firestore/lib/src/query.dart +++ b/packages/cloud_firestore/cloud_firestore/lib/src/query.dart @@ -4,10 +4,6 @@ part of cloud_firestore; -/// Sentinel value to check whether user passed values explicitly through .where() method -@internal -const notSetQueryParam = Object(); - /// Represents a [Query] over the data at a particular location. /// /// Can construct refined [Query] objects by adding filters and ordering. @@ -634,8 +630,8 @@ class _JsonQuery implements Query> { @override Query> where( Object fieldOrFilter, { - Object? isEqualTo = notSetQueryParam, - Object? isNotEqualTo = notSetQueryParam, + Object? isEqualTo, + Object? isNotEqualTo, Object? isLessThan, Object? isLessThanOrEqualTo, Object? isGreaterThan, @@ -650,8 +646,8 @@ class _JsonQuery implements Query> { if (fieldOrFilter is Filter) { assert( - identical(isEqualTo, notSetQueryParam) && - identical(isNotEqualTo, notSetQueryParam) && + isEqualTo == null && + isNotEqualTo == null && isLessThan == null && isLessThanOrEqualTo == null && isGreaterThan == null && @@ -694,12 +690,8 @@ class _JsonQuery implements Query> { conditions.add(condition); } - if (!identical(isEqualTo, notSetQueryParam)) { - addCondition(field, '==', isEqualTo); - } - if (!identical(isNotEqualTo, notSetQueryParam)) { - addCondition(field, '!=', isNotEqualTo); - } + if (isEqualTo != null) addCondition(field, '==', isEqualTo); + if (isNotEqualTo != null) addCondition(field, '!=', isNotEqualTo); if (isLessThan != null) addCondition(field, '<', isLessThan); if (isLessThanOrEqualTo != null) { addCondition(field, '<=', isLessThanOrEqualTo); @@ -1067,8 +1059,8 @@ class _WithConverterQuery implements Query { @override Query where( Object field, { - Object? isEqualTo = notSetQueryParam, - Object? isNotEqualTo = notSetQueryParam, + Object? isEqualTo, + Object? isNotEqualTo, Object? isLessThan, Object? isLessThanOrEqualTo, Object? isGreaterThan,