Skip to content

Fix behavior for empty arrays and objects in default querySerializer #1404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-sheep-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix behavior for empty arrays and objects in default `querySerializer`
8 changes: 7 additions & 1 deletion packages/openapi-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export function defaultQuerySerializer<T = unknown>(q: T): string {
if (q && typeof q === "object") {
for (const [k, v] of Object.entries(q)) {
const value = defaultQueryParamSerializer([k], v);
if (value !== undefined) {
if (value) {
search.push(value);
}
}
Expand All @@ -328,6 +328,9 @@ export function defaultQueryParamSerializer<T = unknown>(
return `${deepObjectPath(key)}=${String(value)}`;
}
if (Array.isArray(value)) {
if (!value.length) {
return undefined;
}
const nextValue: string[] = [];
for (const item of value) {
const next = defaultQueryParamSerializer(key, item);
Expand All @@ -338,6 +341,9 @@ export function defaultQueryParamSerializer<T = unknown>(
return nextValue.join(`&`);
}
if (typeof value === "object") {
if (!Object.keys(value).length) {
return undefined;
}
const nextValue: string[] = [];
for (const [k, v] of Object.entries(value)) {
if (v !== undefined && v !== null) {
Expand Down
12 changes: 12 additions & 0 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ describe("client", () => {
);
});

it("array params (empty)", async () => {
const client = createClient<paths>();
mockFetchOnce({ status: 200, body: "{}" });
await client.GET("/query-params", {
params: {
query: { array: [] },
},
});

expect(fetchMocker.mock.calls[0][0]).toBe("/query-params");
});

it("object params", async () => {
const client = createClient<paths>();
mockFetchOnce({ status: 200, body: "{}" });
Expand Down
12 changes: 12 additions & 0 deletions packages/openapi-fetch/test/v7-beta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ describe("client", () => {
);
});

it("array params (empty)", async () => {
const client = createClient<paths>();
mockFetchOnce({ status: 200, body: "{}" });
await client.GET("/query-params", {
params: {
query: { array: [] },
},
});

expect(fetchMocker.mock.calls[0][0]).toBe("/query-params");
});

it("object params", async () => {
const client = createClient<paths>();
mockFetchOnce({ status: 200, body: "{}" });
Expand Down