Skip to content

Commit

Permalink
fix(firestore): revert breaking change to where() API. null cannot …
Browse files Browse the repository at this point in the history
…be used for `isEqualTo` or `isNotEqualTo` in a query. (firebase#12164)
  • Loading branch information
russellwheatley authored Jan 25, 2024
1 parent 57ebd52 commit cff6f76
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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', () {
Expand Down
24 changes: 8 additions & 16 deletions packages/cloud_firestore/cloud_firestore/lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -634,8 +630,8 @@ class _JsonQuery implements Query<Map<String, dynamic>> {
@override
Query<Map<String, dynamic>> where(
Object fieldOrFilter, {
Object? isEqualTo = notSetQueryParam,
Object? isNotEqualTo = notSetQueryParam,
Object? isEqualTo,
Object? isNotEqualTo,
Object? isLessThan,
Object? isLessThanOrEqualTo,
Object? isGreaterThan,
Expand All @@ -650,8 +646,8 @@ class _JsonQuery implements Query<Map<String, dynamic>> {

if (fieldOrFilter is Filter) {
assert(
identical(isEqualTo, notSetQueryParam) &&
identical(isNotEqualTo, notSetQueryParam) &&
isEqualTo == null &&
isNotEqualTo == null &&
isLessThan == null &&
isLessThanOrEqualTo == null &&
isGreaterThan == null &&
Expand Down Expand Up @@ -694,12 +690,8 @@ class _JsonQuery implements Query<Map<String, dynamic>> {
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);
Expand Down Expand Up @@ -1067,8 +1059,8 @@ class _WithConverterQuery<T extends Object?> implements Query<T> {
@override
Query<T> where(
Object field, {
Object? isEqualTo = notSetQueryParam,
Object? isNotEqualTo = notSetQueryParam,
Object? isEqualTo,
Object? isNotEqualTo,
Object? isLessThan,
Object? isLessThanOrEqualTo,
Object? isGreaterThan,
Expand Down

0 comments on commit cff6f76

Please sign in to comment.