Skip to content

Commit

Permalink
fix(openapi-fetch): Prevent orphan ampersands in query from empty arr…
Browse files Browse the repository at this point in the history
…ays (#1936)
  • Loading branch information
BlakeSzabo authored Oct 25, 2024
1 parent 2a0b0df commit d14aa65
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-boats-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix multiple empty arrays in query params appending extra ampersands
3 changes: 3 additions & 0 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ export function createQuerySerializer(options) {
continue;
}
if (Array.isArray(value)) {
if (value.length === 0) {
continue;
}
search.push(
serializeArrayParam(name, value, {
style: "form",
Expand Down
17 changes: 17 additions & 0 deletions packages/openapi-fetch/test/common/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,23 @@ describe("params", () => {
expect(actualURL.search).toBe("");
});

test("array params (empty, multiple)", async () => {
let actualURL = new URL("https://fakeurl.example");
const client = createObservedClient<paths>({}, async (req) => {
actualURL = new URL(req.url);
return Response.json({});
});

await client.GET("/query-params", {
params: {
query: { array: [], second_array: [], third_array: [] },
},
});

expect(actualURL.pathname).toBe("/query-params");
expect(actualURL.search).toBe("");
});

test("empty/null params", async () => {
let actualURL = new URL("https://fakeurl.example");
const client = createObservedClient<paths>({}, async (req) => {
Expand Down

0 comments on commit d14aa65

Please sign in to comment.