fix: preserve array query parameters in redirect logic#28734
fix: preserve array query parameters in redirect logic#28734TheBhowmik wants to merge 10 commits into
Conversation
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="apps/web/server/lib/[user]/getServerSideProps.ts">
<violation number="1" location="apps/web/server/lib/[user]/getServerSideProps.ts:118">
P2: Scalar empty-string query parameters are silently dropped by the new `if (value)` filter, changing redirect query semantics (e.g., `?foo=` is removed).</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
|
@cubic-dev-ai I have addressed this in my latest commit by explicitly checking for null and undefined. Please re-verify. |
@TheBhowmik I have started the AI code review. It will take a few minutes to complete. |
sahitya-chandra
left a comment
There was a problem hiding this comment.
Can you clean up the comments
|
"Done! I've removed the comments, cleaned up the unused imports/variables, and updated the branch with the latest changes from main. Ready for another look!" |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughReplaced 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/web/server/lib/[user]/getServerSideProps.ts (1)
111-121: Refactor duplicated query serialization and guard empty query suffix.The same
context.queryserialization logic is implemented twice. Extracting it into a local helper will keep both redirect paths consistent and avoid future drift. Also, build destination URLs without forcing?when the query string is empty.♻️ Proposed refactor
export const getServerSideProps: GetServerSideProps<UserPageProps> = async (context) => { + const buildQueryString = (query: typeof context.query) => { + const searchParams = new URLSearchParams(); + for (const [key, value] of Object.entries(query)) { + if (Array.isArray(value)) { + value.forEach((v) => searchParams.append(key, v)); + } else if (value !== undefined && value !== null) { + searchParams.append(key, value); + } + } + return searchParams.toString(); + }; + const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(context.req, context.params?.orgSlug); @@ if (isDynamicGroup) { const destinationUrl = encodeURI(`/${usernameList.join("+")}/dynamic`); - - const searchParams = new URLSearchParams(); - Object.entries(context.query).forEach(([key, value]) => { - if (Array.isArray(value)) { - value.forEach((v) => searchParams.append(key, v)); - } else if (value !== undefined && value !== null) { - searchParams.append(key, value); - } - }); - - const originalQueryString = searchParams.toString(); - const destinationWithQuery = `${destinationUrl}?${originalQueryString}`; + const originalQueryString = buildQueryString(context.query); + const destinationWithQuery = originalQueryString + ? `${destinationUrl}?${originalQueryString}` + : destinationUrl; @@ if (eventTypes.length === 1 && context.query.redirect !== "false") { const urlDestination = `/${user.profile.username}/${eventTypes[0].slug}`; - const searchParams = new URLSearchParams(); - - Object.entries(context.query).forEach(([key, value]) => { - if (Array.isArray(value)) { - value.forEach((v) => searchParams.append(key, v)); - } else if (value !== undefined && value !== null) { - searchParams.append(key, value); - } - }); - - const urlQuery = searchParams.toString(); + const urlQuery = buildQueryString(context.query); + const destination = urlQuery ? `${encodeURI(urlDestination)}?${urlQuery}` : encodeURI(urlDestination); return { redirect: { permanent: false, - destination: `${encodeURI(urlDestination)}?${urlQuery}`, + destination, }, }; }Also applies to: 172-188
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/web/server/lib/`[user]/getServerSideProps.ts around lines 111 - 121, Extract the duplicated serialization of context.query into a small local helper (e.g., serializeQuery or buildQueryString) that takes context.query and returns the encoded query string; replace the duplicate blocks that build originalQueryString with calls to that helper. When composing destinationWithQuery (or any redirect URL), append the query string only if the helper returns a non-empty string (i.e., use `${destinationUrl}?${qs}` only when qs is truthy) so you don't add a trailing '?' for empty queries. Ensure both places referenced in the diff (the block producing originalQueryString/destinationWithQuery and the other occurrence around lines 172-188) call the same helper to keep behavior consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@apps/web/server/lib/`[user]/getServerSideProps.ts:
- Around line 111-121: Extract the duplicated serialization of context.query
into a small local helper (e.g., serializeQuery or buildQueryString) that takes
context.query and returns the encoded query string; replace the duplicate blocks
that build originalQueryString with calls to that helper. When composing
destinationWithQuery (or any redirect URL), append the query string only if the
helper returns a non-empty string (i.e., use `${destinationUrl}?${qs}` only when
qs is truthy) so you don't add a trailing '?' for empty queries. Ensure both
places referenced in the diff (the block producing
originalQueryString/destinationWithQuery and the other occurrence around lines
172-188) call the same helper to keep behavior consistent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9b51f911-1b9b-412d-9437-e002108286ae
📒 Files selected for processing (1)
apps/web/server/lib/[user]/getServerSideProps.ts
|
Final update: Refactored the logic into a typed getQueryString helper. This addresses the array serialization issue and the trailing '?' suffix. The file is now fully compliant with Biome nursery linting rules (explicit types, no ternaries). Ready for review!" |
|
@sahitya-chandra sir ? |
|
This PR has been marked as stale due to inactivity. If you're still working on it or need any help, please let us know or update the PR to keep it active. |
What does this PR do?
This PR fixes the incorrect serialization of array query parameters within the getServerSideProps redirect logic.
Previously, the code used an unsafe type cast (as Record<string, string>) when initializing URLSearchParams. This caused query parameters with multiple values (e.g., ?test=a&test=b) to be merged into a single string separated by a comma (e.g., ?test=a,b) during server-side redirects.
Changes:
Removed the unsafe type casting that forced arrays into strings.
Implemented a robust loop that iterates through context.query entries.
Used URLSearchParams.append() to ensure that each value in an array is preserved as an individual key-value pair in the resulting query string.
Fixes #28687
Visual Demo (For contributors especially)
Image Demo :
Before Fix (Buggy)
.../15min?test=apple,banana
After Fix (Correct)
.../15min?test=apple&test=banana
Mandatory Tasks (DO NOT REMOVE)
How should this be tested?
Start the development server and navigate to a user profile page with repeated query parameters: http://localhost:3000/wow?test=apple&test=banana
Click on any event type card (e.g., "15 min meeting") to trigger the server-side redirect logic in getServerSideProps.ts.
Expected Result: The final redirected URL should preserve the ampersand separator: localhost:3000/wow/15min?test=apple&test=banana&...
Original Buggy Behavior: The URL would incorrectly squash the values into a single string: localhost:3000/wow/15min?test=apple,banana&...
Are there environment variables that should be set?
No. This is a logic fix for standard URL serialization.
What are the minimal test data to have?
A local user profile (e.g., /wow) and at least one active Event Type (e.g., "15 min meeting").
What is expected (happy path) to have (input and output)?
Input: Navigating to /[user]?test=a&test=b and clicking an event card.
Output: Redirecting to /[user]/[event]?test=a&test=b (preserving the & separator).
Any other important info that could help to test that PR
This fix replaces an unsafe type cast that was causing URLSearchParams to treat arrays as single comma-separated strings.