Skip to content
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
2 changes: 1 addition & 1 deletion handwritten/firestore/dev/src/reference/query-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export class QueryUtil<
// call to `requestStream()` will backoff should the restart
// fail before delivering any results.
let newQuery: Query<AppModelType, DbModelType>;
if (!this._queryOptions.limit) {
if (this._queryOptions.limit === undefined) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check this._queryOptions.limit === undefined correctly allows 0 to be processed in the else block, but it changes the behavior for null values. Previously, null was treated as "no limit" (entering the if block), but now it will proceed to the else block. To maintain the existing behavior for null while fixing it for 0, consider using a nullish check (== null), which is the recommended way to check for both null and undefined in the Google TypeScript Style Guide.

Suggested change
if (this._queryOptions.limit === undefined) {
if (this._queryOptions.limit == null) {
References
  1. The Google TypeScript Style Guide recommends using == null to check for both null and undefined. (link)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limit is typed as number | undefined. So null shouldn't be a valid value.

newQuery = query;
} else {
const newLimit =
Expand Down
2 changes: 1 addition & 1 deletion handwritten/firestore/dev/src/reference/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ export class Query<
structuredQuery.startAt = this.toCursor(this._queryOptions.startAt);
structuredQuery.endAt = this.toCursor(this._queryOptions.endAt);

if (this._queryOptions.limit) {
if (this._queryOptions.limit !== undefined) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using !== undefined here will cause null values to be serialized as {value: null}, which may lead to errors during proto serialization if the field expects a numeric value. Using != null ensures that only non-nullish values (including 0) are serialized, preserving the original behavior for null while fixing the issue for 0. This aligns with the Google TypeScript Style Guide's recommendation for nullish checks.

Suggested change
if (this._queryOptions.limit !== undefined) {
if (this._queryOptions.limit != null) {
References
  1. The Google TypeScript Style Guide recommends using != null to check for both null and undefined. (link)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limit is typed as number | undefined. So null shouldn't be a valid value.

structuredQuery.limit = {value: this._queryOptions.limit};
}

Expand Down
14 changes: 14 additions & 0 deletions handwritten/firestore/dev/test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,20 @@ describe('limit() interface', () => {
query = query.limit(1).limit(2).limit(3);
await query.get();
});

it('handles limit(0) correctly', async () => {
const overrides: ApiOverride = {
runQuery: request => {
queryEquals(request, limit(0));
return emptyQueryStream();
},
};

firestore = await createInstance(overrides);
let query: Query = firestore.collection('collectionId');
query = query.limit(0);
await query.get();
});
});

describe('limitToLast() interface', () => {
Expand Down
Loading