Skip to content

Commit d14aa65

Browse files
authored
fix(openapi-fetch): Prevent orphan ampersands in query from empty arrays (#1936)
1 parent 2a0b0df commit d14aa65

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

.changeset/strong-boats-drive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Fix multiple empty arrays in query params appending extra ampersands

packages/openapi-fetch/src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ export function createQuerySerializer(options) {
436436
continue;
437437
}
438438
if (Array.isArray(value)) {
439+
if (value.length === 0) {
440+
continue;
441+
}
439442
search.push(
440443
serializeArrayParam(name, value, {
441444
style: "form",

packages/openapi-fetch/test/common/params.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,23 @@ describe("params", () => {
264264
expect(actualURL.search).toBe("");
265265
});
266266

267+
test("array params (empty, multiple)", async () => {
268+
let actualURL = new URL("https://fakeurl.example");
269+
const client = createObservedClient<paths>({}, async (req) => {
270+
actualURL = new URL(req.url);
271+
return Response.json({});
272+
});
273+
274+
await client.GET("/query-params", {
275+
params: {
276+
query: { array: [], second_array: [], third_array: [] },
277+
},
278+
});
279+
280+
expect(actualURL.pathname).toBe("/query-params");
281+
expect(actualURL.search).toBe("");
282+
});
283+
267284
test("empty/null params", async () => {
268285
let actualURL = new URL("https://fakeurl.example");
269286
const client = createObservedClient<paths>({}, async (req) => {

0 commit comments

Comments
 (0)