Skip to content
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

Apply encodeURIComponent on path parameter value #1696

Merged
merged 4 commits into from
Jun 24, 2024
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/wet-days-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix: encode primitive path parameters
2 changes: 1 addition & 1 deletion packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export function defaultPathSerializer(pathname, pathParams) {
nextURL = nextURL.replace(match, `;${serializePrimitiveParam(name, value)}`);
continue;
}
nextURL = nextURL.replace(match, style === "label" ? `.${value}` : value);
nextURL = nextURL.replace(match, style === "label" ? `.${encodeURIComponent(value)}` : encodeURIComponent(value));
}
return nextURL;
}
Expand Down
41 changes: 38 additions & 3 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,42 @@ describe("client", () => {
);
});

it("escapes reserved characters in path segment", async () => {
const client = createClient<paths>({ baseUrl });
const { getRequestUrl } = useMockRequestHandler({
baseUrl,
method: "get",
path: "/blogposts/*",
});

await client.GET("/blogposts/{post_id}", {
params: { path: { post_id: ";/?:@&=+$,# " } },
});

// expect post_id to be encoded properly
const url = getRequestUrl();
expect(url.pathname).toBe("/blogposts/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23%20");
});

it("does not escape allowed characters in path segment", async () => {
const client = createClient<paths>({ baseUrl });
const { getRequestUrl } = useMockRequestHandler({
baseUrl,
method: "get",
path: "/blogposts/*",
});

const postId = "aAzZ09-_.!~*'()";

await client.GET("/blogposts/{post_id}", {
params: { path: { post_id: postId } },
});

// expect post_id to stay unchanged
const url = getRequestUrl();
expect(url.pathname).toBe(`/blogposts/${postId}`);
});

it("allows UTF-8 characters", async () => {
const client = createClient<paths>({ baseUrl });
const { getRequestUrl } = useMockRequestHandler({
Expand All @@ -215,13 +251,12 @@ describe("client", () => {
});

await client.GET("/blogposts/{post_id}", {
params: { path: { post_id: "post?id = 🥴" } },
params: { path: { post_id: "🥴" } },
});

// expect post_id to be encoded properly
const url = getRequestUrl();
expect(url.searchParams.get("id ")).toBe(" 🥴");
expect(url.pathname + url.search).toBe("/blogposts/post?id%20=%20%F0%9F%A5%B4");
expect(url.pathname).toBe("/blogposts/%F0%9F%A5%B4");
});
});

Expand Down