Skip to content

Commit 1e66e52

Browse files
liu-husongthiyaguk09
authored andcommitted
fix(firestore): ensure limit(0) is properly serialized in query requests (googleapis#8076)
Previously, calling .limit(0) on a Firestore Query did not serialize the zero value to the wire protocol, causing queries to be sent without a limit constraint. This change explicitly includes limit(0) in the query proto. Fixes googleapis#7382
1 parent c7d8732 commit 1e66e52

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

handwritten/firestore/dev/src/reference/query-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ export class QueryUtil<
344344
// call to `requestStream()` will backoff should the restart
345345
// fail before delivering any results.
346346
let newQuery: Query<AppModelType, DbModelType>;
347-
if (!this._queryOptions.limit) {
347+
if (this._queryOptions.limit === undefined) {
348348
newQuery = query;
349349
} else {
350350
const newLimit =

handwritten/firestore/dev/src/reference/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ export class Query<
16081608
structuredQuery.startAt = this.toCursor(this._queryOptions.startAt);
16091609
structuredQuery.endAt = this.toCursor(this._queryOptions.endAt);
16101610

1611-
if (this._queryOptions.limit) {
1611+
if (this._queryOptions.limit !== undefined) {
16121612
structuredQuery.limit = {value: this._queryOptions.limit};
16131613
}
16141614

handwritten/firestore/dev/test/query.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,20 @@ describe('limit() interface', () => {
19571957
query = query.limit(1).limit(2).limit(3);
19581958
await query.get();
19591959
});
1960+
1961+
it('handles limit(0) correctly', async () => {
1962+
const overrides: ApiOverride = {
1963+
runQuery: request => {
1964+
queryEquals(request, limit(0));
1965+
return emptyQueryStream();
1966+
},
1967+
};
1968+
1969+
firestore = await createInstance(overrides);
1970+
let query: Query = firestore.collection('collectionId');
1971+
query = query.limit(0);
1972+
await query.get();
1973+
});
19601974
});
19611975

19621976
describe('limitToLast() interface', () => {

0 commit comments

Comments
 (0)