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: Remove DocumentReference special case in cursor #1396

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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 @@ -1151,13 +1151,7 @@ public Query startAt(@Nonnull DocumentSnapshot snapshot) {
*/
@Nonnull
public Query startAt(Object... fieldValues) {
// TODO(b/296435819): Remove this warning message.
warningOnSingleDocumentReference(fieldValues);
milaGGL marked this conversation as resolved.
Show resolved Hide resolved

ImmutableList<FieldOrder> fieldOrders =
milaGGL marked this conversation as resolved.
Show resolved Hide resolved
fieldValues.length == 1 && fieldValues[0] instanceof DocumentReference
? createImplicitOrderBy()
: options.getFieldOrders();
ImmutableList<FieldOrder> fieldOrders = options.getFieldOrders();
Cursor cursor = createCursor(fieldOrders, fieldValues, true);

Builder newOptions = options.toBuilder();
Expand Down Expand Up @@ -1246,13 +1240,7 @@ public Query startAfter(@Nonnull DocumentSnapshot snapshot) {
* @return The created Query.
*/
public Query startAfter(Object... fieldValues) {
// TODO(b/296435819): Remove this warning message.
warningOnSingleDocumentReference(fieldValues);

ImmutableList<FieldOrder> fieldOrders =
fieldValues.length == 1 && fieldValues[0] instanceof DocumentReference
? createImplicitOrderBy()
: options.getFieldOrders();
ImmutableList<FieldOrder> fieldOrders = options.getFieldOrders();
Cursor cursor = createCursor(fieldOrders, fieldValues, false);

Builder newOptions = options.toBuilder();
Expand Down Expand Up @@ -1290,13 +1278,7 @@ public Query endBefore(@Nonnull DocumentSnapshot snapshot) {
*/
@Nonnull
public Query endBefore(Object... fieldValues) {
// TODO(b/296435819): Remove this warning message.
warningOnSingleDocumentReference(fieldValues);

ImmutableList<FieldOrder> fieldOrders =
fieldValues.length == 1 && fieldValues[0] instanceof DocumentReference
? createImplicitOrderBy()
: options.getFieldOrders();
ImmutableList<FieldOrder> fieldOrders = options.getFieldOrders();
Cursor cursor = createCursor(fieldOrders, fieldValues, true);

Builder newOptions = options.toBuilder();
Expand All @@ -1314,13 +1296,7 @@ public Query endBefore(Object... fieldValues) {
*/
@Nonnull
public Query endAt(Object... fieldValues) {
// TODO(b/296435819): Remove this warning message.
warningOnSingleDocumentReference(fieldValues);

ImmutableList<FieldOrder> fieldOrders =
fieldValues.length == 1 && fieldValues[0] instanceof DocumentReference
? createImplicitOrderBy()
: options.getFieldOrders();
ImmutableList<FieldOrder> fieldOrders = options.getFieldOrders();
Cursor cursor = createCursor(fieldOrders, fieldValues, false);

Builder newOptions = options.toBuilder();
Expand All @@ -1329,16 +1305,6 @@ public Query endAt(Object... fieldValues) {
return new Query(rpcContext, newOptions.build());
}

private void warningOnSingleDocumentReference(Object... fieldValues) {
if (options.getFieldOrders().isEmpty()
&& fieldValues.length == 1
&& fieldValues[0] instanceof DocumentReference) {
LOGGER.warning(
"Warning: Passing DocumentReference into a cursor without orderBy clause is not an intended "
+ "behavior. Please use DocumentSnapshot or add an explicit orderBy on document key field.");
}
}

/**
* Creates and returns a new Query that ends at the provided document (inclusive). The end
* position is relative to the order of the query. The document must contain all of the fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,23 +599,6 @@ public void withDocumentIdAndDocumentSnapshotCursor() {
assertEquals(queryRequest, runQuery.getValue());
}

@Test
public void withDocumentReferenceCursor() {
doAnswer(queryResponse())
.when(firestoreMock)
.streamRequest(runQuery.capture(), streamObserverCapture.capture(), any());

DocumentReference documentCursor = firestoreMock.document(DOCUMENT_PATH);
Value documentValue = reference(DOCUMENT_NAME);

query.startAt(documentCursor).get();

RunQueryRequest queryRequest =
query(order("__name__", StructuredQuery.Direction.ASCENDING), startAt(documentValue, true));

assertEquals(queryRequest, runQuery.getValue());
}

@Test
public void withDocumentIdAndDocumentReferenceCursor() {
doAnswer(queryResponse())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,9 @@ public void startAfterAddsAnImplicitOrderByForDocumentReferences() throws Except
doc1.set(map("foo", 1)).get();
doc2.set(map("foo", 1)).get();

QuerySnapshot querySnapshot = randomColl.startAfter(doc1).get().get();
DocumentSnapshot docSnap = doc1.get().get();

QuerySnapshot querySnapshot = randomColl.startAfter(docSnap).get().get();
assertEquals(1, querySnapshot.size());
Iterator<QueryDocumentSnapshot> documents = querySnapshot.iterator();
assertEquals(doc2, documents.next().getReference());
Expand Down
Loading