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

Forbid queries endAt an uncommitted server timestamp. #2382

Merged
merged 2 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 49 additions & 0 deletions Firestore/Example/Tests/Integration/API/FIRValidationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,55 @@ - (void)testQueryCannotBeCreatedFromDocumentsMissingSortValues {
FSTAssertThrows([query queryEndingAtDocument:snapshot], reason);
}

- (void)testQueriesCannotBeSortedByAnUncommittedServerTimestamp {
__weak FIRCollectionReference *collection = [self collectionRef];
FIRFirestore *db = [self firestore];

[db disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
[self awaitExpectations];

XCTestExpectation *offlineCallbackDone =
[self expectationWithDescription:@"offline callback done"];
XCTestExpectation *onlineCallbackDone = [self expectationWithDescription:@"online callback done"];

[collection addSnapshotListener:^(FIRQuerySnapshot *snapshot, NSError *error) {
XCTAssertNil(error);

// Skip the initial empty snapshot.
if (snapshot.empty) return;

XCTAssertEqual(snapshot.count, 1);
FIRQueryDocumentSnapshot *docSnap = [snapshot documents][0];
rsgowman marked this conversation as resolved.
Show resolved Hide resolved

if ([snapshot metadata].pendingWrites) {
// Offline snapshot. Since the server timestamp is uncommitted, we
// shouldn't be able to query by it.
NSString *reason =
@"Invalid query. Your are trying to start or end a query using a document for which the "
@"field 'timestamp' is an uncommitted server timestamp. (Since the value of this field "
@"is unknown, you cannot start/end a query with it.)";
FSTAssertThrows([[[collection queryOrderedByField:@"timestamp"] queryEndingAtDocument:docSnap]
addSnapshotListener:^(FIRQuerySnapshot *, NSError *){
}],
reason);
[offlineCallbackDone fulfill];
} else {
// Online snapshot. Since the server timestamp is committed, we should be able to query by it.
[[[collection queryOrderedByField:@"timestamp"] queryEndingAtDocument:docSnap]
addSnapshotListener:^(FIRQuerySnapshot *, NSError *){
}];
[onlineCallbackDone fulfill];
}
}];

FIRDocumentReference *document = [collection documentWithAutoID];
[document setData:@{@"timestamp" : [FIRFieldValue fieldValueForServerTimestamp]}];
[self awaitExpectations];

[db enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
[self awaitExpectations];
}

- (void)testQueryBoundMustNotHaveMoreComponentsThanSortOrders {
FIRCollectionReference *testCollection = [self collectionRef];
FIRQuery *query = [testCollection queryOrderedByField:@"foo"];
Expand Down
15 changes: 13 additions & 2 deletions Firestore/Source/API/FIRQuery.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
#import "Firestore/Source/API/FIRFieldPath+Internal.h"
#import "Firestore/Source/API/FIRFieldValue+Internal.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/API/FIRListenerRegistration+Internal.h"
#import "Firestore/Source/API/FIRQuery+Internal.h"
Expand Down Expand Up @@ -550,7 +551,9 @@ - (void)validateOrderByField:(const FieldPath &)orderByField
* Note that the FSTBound will always include the key of the document and the position will be
* unambiguous.
*
* Will throw if the document does not contain all fields of the order by of the query.
* Will throw if the document does not contain all fields of the order by of
* the query or if any of the fields in the order by are an uncommitted server
* timestamp.
*/
- (FSTBound *)boundFromSnapshot:(FIRDocumentSnapshot *)snapshot isBefore:(BOOL)isBefore {
if (![snapshot exists]) {
Expand All @@ -572,7 +575,15 @@ - (FSTBound *)boundFromSnapshot:(FIRDocumentSnapshot *)snapshot isBefore:(BOOL)i
databaseID:self.firestore.databaseID]];
} else {
FSTFieldValue *value = [document fieldForPath:sortOrder.field];
if (value != nil) {

if ([value isKindOfClass:[FSTServerTimestampValue class]]) {
FSTThrowInvalidUsage(@"InvalidQueryException",
@"Invalid query. Your are trying to start or end a query using a "
rsgowman marked this conversation as resolved.
Show resolved Hide resolved
"document for which the field '%s' is an uncommitted server "
"timestamp. (Since the value of this field is unknown, you cannot "
"start/end a query with it.)",
sortOrder.field.CanonicalString().c_str());
} else if (value != nil) {
[components addObject:value];
} else {
FSTThrowInvalidUsage(@"InvalidQueryException",
Expand Down