Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(firestore): revert breaking change to where() API. null cannot be used for isEqualTo or isNotEqualTo in a query. #12164

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading