Skip to content

Commit 93204e4

Browse files
authored
Fix behavior for empty arrays and objects in default querySerializer (#1404)
1 parent 4e116ad commit 93204e4

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Diff for: .changeset/witty-sheep-mix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Fix behavior for empty arrays and objects in default `querySerializer`

Diff for: packages/openapi-fetch/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export function defaultQuerySerializer<T = unknown>(q: T): string {
305305
if (q && typeof q === "object") {
306306
for (const [k, v] of Object.entries(q)) {
307307
const value = defaultQueryParamSerializer([k], v);
308-
if (value !== undefined) {
308+
if (value) {
309309
search.push(value);
310310
}
311311
}
@@ -328,6 +328,9 @@ export function defaultQueryParamSerializer<T = unknown>(
328328
return `${deepObjectPath(key)}=${String(value)}`;
329329
}
330330
if (Array.isArray(value)) {
331+
if (!value.length) {
332+
return undefined;
333+
}
331334
const nextValue: string[] = [];
332335
for (const item of value) {
333336
const next = defaultQueryParamSerializer(key, item);
@@ -338,6 +341,9 @@ export function defaultQueryParamSerializer<T = unknown>(
338341
return nextValue.join(`&`);
339342
}
340343
if (typeof value === "object") {
344+
if (!Object.keys(value).length) {
345+
return undefined;
346+
}
341347
const nextValue: string[] = [];
342348
for (const [k, v] of Object.entries(value)) {
343349
if (v !== undefined && v !== null) {

Diff for: packages/openapi-fetch/test/index.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ describe("client", () => {
185185
);
186186
});
187187

188+
it("array params (empty)", async () => {
189+
const client = createClient<paths>();
190+
mockFetchOnce({ status: 200, body: "{}" });
191+
await client.GET("/query-params", {
192+
params: {
193+
query: { array: [] },
194+
},
195+
});
196+
197+
expect(fetchMocker.mock.calls[0][0]).toBe("/query-params");
198+
});
199+
188200
it("object params", async () => {
189201
const client = createClient<paths>();
190202
mockFetchOnce({ status: 200, body: "{}" });

Diff for: packages/openapi-fetch/test/v7-beta.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ describe("client", () => {
194194
);
195195
});
196196

197+
it("array params (empty)", async () => {
198+
const client = createClient<paths>();
199+
mockFetchOnce({ status: 200, body: "{}" });
200+
await client.GET("/query-params", {
201+
params: {
202+
query: { array: [] },
203+
},
204+
});
205+
206+
expect(fetchMocker.mock.calls[0][0]).toBe("/query-params");
207+
});
208+
197209
it("object params", async () => {
198210
const client = createClient<paths>();
199211
mockFetchOnce({ status: 200, body: "{}" });

0 commit comments

Comments
 (0)