Skip to content

fix(users): default malformed search limits#345

Open
Jorel97 wants to merge 3 commits into
profullstack:masterfrom
Jorel97:codex/fix-users-search-limit-344
Open

fix(users): default malformed search limits#345
Jorel97 wants to merge 3 commits into
profullstack:masterfrom
Jorel97:codex/fix-users-search-limit-344

Conversation

@Jorel97
Copy link
Copy Markdown
Contributor

@Jorel97 Jorel97 commented May 30, 2026

Fixes #344.

This keeps /api/users/search from passing NaN into supabase.limit(...) when callers send malformed limit values. 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.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 30, 2026

Greptile Summary

Fixes a bug in /api/users/search where malformed limit query parameters (e.g. limit=abc) produced NaN from parseInt and passed it directly to supabase.limit(...). The guard is replaced with a Number.isFinite check that falls back to 10 for non-finite values, while valid integers are clamped to the [1, 20] range.

  • route.ts: Replaces the limitParam || 10 falsy-OR pattern with Number.isFinite(parsedLimit) ? clamp(...) : 10, correctly handling NaN and also changing the limit=0 behaviour from silently defaulting to 10 to correctly clamping to 1.
  • route.test.ts: Adds a test for the reported limit=abc case plus an it.each block covering 0 → 1, -1 → 1, and Infinity → 10, addressing the edge-case gaps noted in the previous review round.

Confidence Score: 5/5

The 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

Filename Overview
src/app/api/users/search/route.ts Replaces the falsy-OR guard with a Number.isFinite check so NaN from parseInt never reaches supabase.limit(); also correctly changes limit=0 to clamp to 1.
src/app/api/users/search/route.test.ts Adds a dedicated test for limit=abc (the reported bug) plus an it.each block covering limit=0, limit=-1, and limit=Infinity, closing the gaps flagged in the previous review thread.

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: [...] }"]
Loading

Reviews (2): Last reviewed commit: "test(users): cover search limit edges" | Re-trigger Greptile

Comment on lines +14 to +17
const parsedLimit = parseInt(searchParams.get("limit") || "10", 10);
const limit = Number.isFinite(parsedLimit)
? Math.min(Math.max(1, parsedLimit), 20)
: 10;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 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!

Comment thread src/app/api/users/search/route.test.ts
@Jorel97
Copy link
Copy Markdown
Contributor Author

Jorel97 commented May 30, 2026

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Guard user search limit against malformed values

1 participant