From 4f9d2f3d0f9ab5950ee977970a0e5e590b4cbc2f Mon Sep 17 00:00:00 2001 From: Blake Szabo Date: Wed, 2 Oct 2024 09:38:12 +1000 Subject: [PATCH] fix(openapi-fetch): Prevent orphan ampersands in query from empty arrays --- .changeset/strong-boats-drive.md | 5 +++++ packages/openapi-fetch/src/index.js | 3 +++ .../openapi-fetch/test/common/params.test.ts | 17 +++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 .changeset/strong-boats-drive.md diff --git a/.changeset/strong-boats-drive.md b/.changeset/strong-boats-drive.md new file mode 100644 index 000000000..a5275e3d2 --- /dev/null +++ b/.changeset/strong-boats-drive.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": patch +--- + +Fix multiple empty arrays in query params appending extra ampersands diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js index 56c4826d5..17726464a 100644 --- a/packages/openapi-fetch/src/index.js +++ b/packages/openapi-fetch/src/index.js @@ -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", diff --git a/packages/openapi-fetch/test/common/params.test.ts b/packages/openapi-fetch/test/common/params.test.ts index 623101dfb..67a322bf6 100644 --- a/packages/openapi-fetch/test/common/params.test.ts +++ b/packages/openapi-fetch/test/common/params.test.ts @@ -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({}, 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({}, async (req) => {