Skip to content

Commit a45ce4f

Browse files
committed
Properly escape :params in generatePath
Fixes #11940
1 parent 2ab9bad commit a45ce4f

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Properly escape interpolated :param values in generatePath()

packages/react-router/__tests__/generatePath-test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ describe("generatePath", () => {
133133
});
134134
});
135135

136+
describe("with a param that contains a /", () => {
137+
it("properly encodes the slash", () => {
138+
expect(generatePath("/courses/:id/grades", { id: "a/b" })).toBe(
139+
"/courses/a%2Fb/grades"
140+
);
141+
});
142+
});
143+
136144
it("throws only on on missing named parameters, but not missing splat params", () => {
137145
expect(() => generatePath(":foo")).toThrow();
138146
expect(() => generatePath("/:foo")).toThrow();

packages/react-router/lib/router/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ export function generatePath<Path extends string>(
11081108
const [, key, optional] = keyMatch;
11091109
let param = params[key as PathParam<Path>];
11101110
invariant(optional === "?" || param != null, `Missing ":${key}" param`);
1111-
return stringify(param);
1111+
return encodeURIComponent(stringify(param));
11121112
}
11131113

11141114
// Remove any optional markers from optional static segments

0 commit comments

Comments
 (0)