fix(firestore): ensure limit(0) is properly serialized in query requests#8076
fix(firestore): ensure limit(0) is properly serialized in query requests#8076liu-husong wants to merge 2 commits intogoogleapis:mainfrom
Conversation
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
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
| if (this._queryOptions.limit === undefined) { | |
| if (this._queryOptions.limit == null) { |
References
- The Google TypeScript Style Guide recommends using
== nullto check for bothnullandundefined. (link)
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
| if (this._queryOptions.limit !== undefined) { | |
| if (this._queryOptions.limit != null) { |
References
- The Google TypeScript Style Guide recommends using
!= nullto check for bothnullandundefined. (link)
There was a problem hiding this comment.
limit is typed as number | undefined. So null shouldn't be a valid value.
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:
Fixes #7382 🦕