Skip to content

Commit 8d00218

Browse files
authored
Fix path params as types (#1982)
* fix(openapi-typescript): Make pathParamsAsTypes work with integer/boolean types * Update changeset
1 parent 343cadf commit 8d00218

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

.changeset/thirty-queens-try.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Make pathParamsAsTypes work with integer/boolean types

packages/openapi-typescript/src/transform/paths-object.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ export default function transformPathsObject(pathsObject: PathsObject, ctx: Glob
5252
for (const match of matches) {
5353
const paramName = match.slice(1, -1);
5454
const param = pathParams[paramName];
55-
if (!param) {
56-
rawPath = rawPath.replace(match, "${string}");
57-
} else {
58-
rawPath = rawPath.replace(match, `$\{${(param.schema as any)?.type ?? "string"}}`);
55+
switch (param?.schema?.type) {
56+
case "number":
57+
case "integer":
58+
rawPath = rawPath.replace(match, "${number}");
59+
break;
60+
case "boolean":
61+
rawPath = rawPath.replace(match, "${boolean}");
62+
break;
63+
default:
64+
rawPath = rawPath.replace(match, "${string}");
65+
break;
5966
}
6067
}
6168
// note: creating a string template literal’s AST manually is hard!

packages/openapi-typescript/test/transform/paths-object.test.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,19 @@ describe("transformPathsObject", () => {
274274
"options > pathParamsAsTypes: true",
275275
{
276276
given: {
277-
"/api/v1/user/me": {
277+
"/api/v1/user/{user_id}": {
278278
parameters: [
279279
{
280280
name: "page",
281281
in: "query",
282282
schema: { type: "number" },
283283
description: "Page number.",
284284
},
285+
{
286+
name: "user_id",
287+
in: "path",
288+
schema: { format: "int64", type: "integer" },
289+
},
285290
],
286291
get: {
287292
parameters: [],
@@ -315,14 +320,16 @@ describe("transformPathsObject", () => {
315320
},
316321
},
317322
want: `{
318-
"/api/v1/user/me": {
323+
[path: \`/api/v1/user/\${number}\`]: {
319324
parameters: {
320325
query?: {
321326
/** @description Page number. */
322327
page?: number;
323328
};
324329
header?: never;
325-
path?: never;
330+
path: {
331+
user_id: number;
332+
};
326333
cookie?: never;
327334
};
328335
get: {
@@ -332,7 +339,9 @@ describe("transformPathsObject", () => {
332339
page?: number;
333340
};
334341
header?: never;
335-
path?: never;
342+
path: {
343+
user_id: number;
344+
};
336345
cookie?: never;
337346
};
338347
requestBody?: never;

0 commit comments

Comments
 (0)