Skip to content

Commit fadd03a

Browse files
committed
test: add pathSerializer tests for custom serialization
1 parent ff4b40f commit fadd03a

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

packages/openapi-fetch/test/common/params.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,106 @@ describe("params", () => {
191191
// expect post_id to be encoded properly
192192
expect(actualPathname).toBe("/path-params/%F0%9F%A5%B4");
193193
});
194+
195+
describe("pathSerializer", () => {
196+
test("global", async () => {
197+
let actualPathname = "";
198+
const client = createObservedClient<paths>(
199+
{
200+
pathSerializer: (pathname, pathParams) => {
201+
// Custom serializer that wraps path values in brackets
202+
let result = pathname;
203+
for (const [key, value] of Object.entries(pathParams)) {
204+
result = result.replace(`{${key}}`, `[${value}]`);
205+
}
206+
return result;
207+
},
208+
},
209+
async (req) => {
210+
actualPathname = new URL(req.url).pathname;
211+
return Response.json({});
212+
},
213+
);
214+
215+
await client.GET("/resources/{id}", {
216+
params: {
217+
path: { id: 123 },
218+
},
219+
});
220+
221+
expect(actualPathname).toBe("/resources/[123]");
222+
});
223+
224+
test("per-request", async () => {
225+
let actualPathname = "";
226+
const client = createObservedClient<paths>(
227+
{
228+
pathSerializer: (pathname, pathParams) => {
229+
// Default global serializer (should be overridden)
230+
let result = pathname;
231+
for (const [key, value] of Object.entries(pathParams)) {
232+
result = result.replace(`{${key}}`, `global-${value}`);
233+
}
234+
return result;
235+
},
236+
},
237+
async (req) => {
238+
actualPathname = new URL(req.url).pathname;
239+
return Response.json({});
240+
},
241+
);
242+
243+
await client.GET("/resources/{id}", {
244+
params: {
245+
path: { id: 456 },
246+
},
247+
pathSerializer: (pathname, pathParams) => {
248+
// Per-request serializer should override global
249+
let result = pathname;
250+
for (const [key, value] of Object.entries(pathParams)) {
251+
result = result.replace(`{${key}}`, `request-${value}`);
252+
}
253+
return result;
254+
},
255+
});
256+
257+
expect(actualPathname).toBe("/resources/request-456");
258+
});
259+
260+
test("complex path params with custom serializer", async () => {
261+
let actualPathname = "";
262+
const client = createObservedClient<paths>(
263+
{
264+
pathSerializer: (pathname, pathParams) => {
265+
// Custom serializer that handles different value types
266+
let result = pathname;
267+
for (const [key, value] of Object.entries(pathParams)) {
268+
if (typeof value === "string") {
269+
result = result.replace(`{${key}}`, `custom:${value}`);
270+
} else {
271+
result = result.replace(`{${key}}`, `other:${value}`);
272+
}
273+
}
274+
return result;
275+
},
276+
},
277+
async (req) => {
278+
actualPathname = new URL(req.url).pathname;
279+
return Response.json({});
280+
},
281+
);
282+
283+
await client.GET("/path-params/{string}", {
284+
params: {
285+
path: {
286+
string: "test-value",
287+
},
288+
},
289+
});
290+
291+
expect(actualPathname).toBe("/path-params/custom:test-value");
292+
});
293+
});
194294
});
195295

196296
describe("header", () => {

0 commit comments

Comments
 (0)