Skip to content

fix(firestore): ensure limit(0) is properly serialized in query requests#8076

Open
liu-husong wants to merge 2 commits intogoogleapis:mainfrom
liu-husong:main
Open

fix(firestore): ensure limit(0) is properly serialized in query requests#8076
liu-husong wants to merge 2 commits intogoogleapis:mainfrom
liu-husong:main

Conversation

@liu-husong
Copy link
Copy Markdown

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary). I’m not sure if this bugfix requires any doc updates — let me know if I’ve missed anything.

Fixes #7382 🦕

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
@liu-husong liu-husong requested a review from a team as a code owner April 20, 2026 09:47
@product-auto-label product-auto-label bot added the api: firestore Issues related to the Firestore API. label Apr 20, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Firestore query logic to correctly handle a limit of 0 by replacing falsy checks with explicit undefined checks, and adds a corresponding test case. Feedback suggests using nullish equality checks (== null and != null) instead of strict undefined checks to preserve existing behavior for null values and follow the Google TypeScript Style Guide.

// 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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: firestore Issues related to the Firestore API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Firestore client library's limit API not handling limit(0) correctly

1 participant