fix(utils.url): delete existing key before appending array values in withQuery by LeSingh1 · Pull Request #594 · unjs/ofetch · GitHub
Skip to content

fix(utils.url): delete existing key before appending array values in withQuery#594

Open
LeSingh1 wants to merge 1 commit into
unjs:mainfrom
LeSingh1:fix/withquery-array-replace-existing
Open

fix(utils.url): delete existing key before appending array values in withQuery#594
LeSingh1 wants to merge 1 commit into
unjs:mainfrom
LeSingh1:fix/withquery-array-replace-existing

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Jun 4, 2026

Copy link
Copy Markdown

When withQuery is called with a URL that already has a query string, scalar values are replaced correctly via searchParams.set(). Array values, though, were only appended without first removing the old entries for that key, so the original value leaked into the final URL alongside the new ones.

Example before this fix:

withQuery('https://example.org/path?tag=old&other=1', { tag: ['a', 'b'] })
// produced: ?tag=old&other=1&tag=a&tag=b  (tag=old should not be there)

After:

withQuery('https://example.org/path?tag=old&other=1', { tag: ['a', 'b'] })
// produces: ?other=1&tag=a&tag=b

The fix adds searchParams.delete(key) before the append loop, matching the replace semantics that searchParams.set() already provides for scalar values. A new test covers the case by fetching through the real h3 server and asserting the raw path+search echoed back.

All 29 tests pass.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed improper handling of undefined values in URL query parameters to ensure they are properly removed
  • Tests

    • Added test coverage verifying that query parameters are correctly replaced rather than accumulated when provided

…withQuery

When withQuery merges a query object into a URL that already has an
existing query string, scalar values are correctly replaced via
searchParams.set(). Array values, however, were appended without first
deleting the existing key for that name, so the old value would persist
alongside the new ones.

For example:
  withQuery('https://example.org/path?tag=old', { tag: ['a', 'b'] })
  produced:  ...?tag=old&tag=a&tag=b   (wrong)
  now gives: ...?tag=a&tag=b           (correct)

Add searchParams.delete(key) before the append loop to match the
replace semantics that searchParams.set() already provides for
scalar values.
@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
test/index.test.ts (1)

347-359: 💤 Low value

Consider asserting that non-replaced parameters are preserved.

The test verifies that tag=old is replaced by tag=a and tag=b, but doesn't explicitly check that other=1 (which isn't in the query object) remains in the result. While the PR description mentions this ("result: ?other=1&tag=a&tag=b"), adding an assertion like expect(result).toContain("other=1") would more thoroughly verify the replace-not-merge semantics.

📋 Optional assertion to verify parameter preservation
     expect(result).toContain("tag=a");
     expect(result).toContain("tag=b");
     expect(result).not.toContain("tag=old");
+    expect(result).toContain("other=1");
   });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/index.test.ts` around lines 347 - 359, The test "replaces existing array
query params instead of accumulating" checks that tag=old is removed when
passing query: { tag: ["a","b"] } but doesn't assert that unrelated existing
params are preserved; update the test case (the it block titled "replaces
existing array query params instead of accumulating") to add an expectation that
the original other=1 remains (use the same result from await
$fetch(getURL("url/check?tag=old&other=1"), { query: { tag: ["a","b"] } }) and
add expect(result).toContain("other=1") so the test verifies replace-not-merge
semantics while preserving unrelated query params).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@test/index.test.ts`:
- Around line 347-359: The test "replaces existing array query params instead of
accumulating" checks that tag=old is removed when passing query: { tag:
["a","b"] } but doesn't assert that unrelated existing params are preserved;
update the test case (the it block titled "replaces existing array query params
instead of accumulating") to add an expectation that the original other=1
remains (use the same result from await
$fetch(getURL("url/check?tag=old&other=1"), { query: { tag: ["a","b"] } }) and
add expect(result).toContain("other=1") so the test verifies replace-not-merge
semantics while preserving unrelated query params).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 57232bfe-5210-4dce-b7dc-f8bcd759ab44

📥 Commits

Reviewing files that changed from the base of the PR and between dfbe3ca and d7d408a.

📒 Files selected for processing (2)
  • src/utils.url.ts
  • test/index.test.ts

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.

1 participant