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

Encode slashes in generatePath dynamic params #12017

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,4 @@
- yuleicul
- zeromask1337
- zheng-chuang
- levvysokiy
5 changes: 5 additions & 0 deletions packages/react-router/__tests__/generatePath-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ describe("generatePath", () => {
"/courses/baz"
);
});
it("handles slashes in dynamic params", () => {
expect(generatePath("/courses/:id", { id: "foo/bar" })).toBe(
"/courses/foo%2Fbar"
);
});
});

describe("with extraneous params", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-router/lib/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,9 @@ export function generatePath<Path extends string>(
const [, key, optional] = keyMatch;
let param = params[key as PathParam<Path>];
invariant(optional === "?" || param != null, `Missing ":${key}" param`);
if (typeof param === "string") {
param = param.replace(/\//g, "%2F");
}
return stringify(param);
}

Expand Down