fix(users): default malformed search limits#345
Conversation
Greptile SummaryFixes a bug in
Confidence Score: 5/5The change is safe to merge; it narrows a well-scoped input-parsing bug with no risk of regression. The fix is minimal and self-contained: two lines in route.ts replace a fragile falsy-OR guard with an explicit Number.isFinite check, and the new tests cover every affected branch. No other call sites or data paths are touched. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["GET /api/users/search?q=...&limit=..."] --> B{Auth check}
B -- "not authenticated" --> C["401 Unauthorized"]
B -- "authenticated" --> D["parseInt(limitParam || '10', 10)"]
D --> E{Number.isFinite?}
E -- "false (NaN e.g. 'abc', 'Infinity')" --> F["limit = 10 (default)"]
E -- "true (valid integer)" --> G["Math.min(Math.max(1, parsed), 20)"]
G --> H["limit ∈ [1, 20]"]
F --> I{Empty query?}
H --> I
I -- "yes" --> J["200 { users: [] }"]
I -- "no" --> K["supabase.from('profiles').select(...).ilike(...).limit(limit)"]
K --> L{error?}
L -- "yes" --> M["500 Failed to search users"]
L -- "no" --> N["200 { users: [...] }"]
Reviews (2): Last reviewed commit: "test(users): cover search limit edges" | Re-trigger Greptile |
| const parsedLimit = parseInt(searchParams.get("limit") || "10", 10); | ||
| const limit = Number.isFinite(parsedLimit) | ||
| ? Math.min(Math.max(1, parsedLimit), 20) | ||
| : 10; |
There was a problem hiding this comment.
Silent behavior change for
limit=0
The old code used limitParam || 10, which treated 0 as falsy and silently defaulted to 10. The new code uses Number.isFinite(0) === true, so limit=0 now flows through the clamp and becomes 1. This is arguably the more correct outcome, but it's an undocumented behavior change and there's no test asserting it. Worth adding a test case for limit=0 → expects mockLimit called with 1 so the intent is explicit and won't regress silently.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Added a follow-up test commit for the Greptile edge-case note. The test suite now covers malformed limit values plus 0, negative, and Infinity cases; route logic is unchanged from the original fix. |
Fixes #344.
This keeps
/api/users/searchfrom passingNaNintosupabase.limit(...)when callers send malformedlimitvalues. Valid numeric limits still clamp to 1..20, and malformed values now fall back to the default limit of 10.I also added regression coverage for
limit=abc.Verification: local dependency install is unavailable in this workspace, so I validated the targeted source/test changes and will follow the PR checks.