Skip to content

Commit

Permalink
fix(cloud_firestore): startAfterDocument could throw when used with a…
Browse files Browse the repository at this point in the history
… DocumentReference (#10339)

* fix(cloud_firestore): encode values for Document query operation

* fix(cloud_firestore): Add unit test

* Move test to integration tests
  • Loading branch information
agent3bood committed Feb 2, 2023
1 parent 1d48f80 commit 8224acb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Expand Up @@ -1873,5 +1873,29 @@ void runQueryTests() {
},
);
});

group('startAfterDocument', () {
test('startAfterDocument() accept DocumentReference in query parameters',
() async {
final collection = await initializeTest('start-after-document');

final doc1 = collection.doc('1');
final doc2 = collection.doc('2');
final doc3 = collection.doc('3');
final doc4 = collection.doc('4');
await doc1.set({'ref': doc1});
await doc2.set({'ref': doc2});
await doc3.set({'ref': doc3});
await doc4.set({'ref': null});

final q = collection
.where('ref', isNull: false)
.orderBy('ref')
.startAfterDocument(await doc1.get());

final res = await q.get();
expect(res.docs.map((e) => e.reference), [doc2, doc3]);
});
});
});
}
4 changes: 3 additions & 1 deletion packages/cloud_firestore/cloud_firestore/lib/src/query.dart
Expand Up @@ -262,7 +262,9 @@ class _JsonQuery implements Query<Map<String, dynamic>> {
// All order by fields must exist within the snapshot
if (field != FieldPath.documentId) {
try {
values.add(documentSnapshot.get(field));
final codecValue =
_CodecUtility.valueEncode(documentSnapshot.get(field));
values.add(codecValue);
} on StateError {
throw "You are trying to start or end a query using a document for which the field '$field' (used as the orderBy) does not exist.";
}
Expand Down

0 comments on commit 8224acb

Please sign in to comment.