diff --git a/.changeset/thirty-queens-try.md b/.changeset/thirty-queens-try.md new file mode 100644 index 000000000..ff27609a8 --- /dev/null +++ b/.changeset/thirty-queens-try.md @@ -0,0 +1,5 @@ +--- +"openapi-typescript": patch +--- + +Make pathParamsAsTypes work with integer/boolean types diff --git a/packages/openapi-typescript/src/transform/paths-object.ts b/packages/openapi-typescript/src/transform/paths-object.ts index 6c6ed6d16..0ac966509 100644 --- a/packages/openapi-typescript/src/transform/paths-object.ts +++ b/packages/openapi-typescript/src/transform/paths-object.ts @@ -52,10 +52,17 @@ export default function transformPathsObject(pathsObject: PathsObject, ctx: Glob for (const match of matches) { const paramName = match.slice(1, -1); const param = pathParams[paramName]; - if (!param) { - rawPath = rawPath.replace(match, "${string}"); - } else { - rawPath = rawPath.replace(match, `$\{${(param.schema as any)?.type ?? "string"}}`); + switch (param?.schema?.type) { + case "number": + case "integer": + rawPath = rawPath.replace(match, "${number}"); + break; + case "boolean": + rawPath = rawPath.replace(match, "${boolean}"); + break; + default: + rawPath = rawPath.replace(match, "${string}"); + break; } } // note: creating a string template literal’s AST manually is hard! diff --git a/packages/openapi-typescript/test/transform/paths-object.test.ts b/packages/openapi-typescript/test/transform/paths-object.test.ts index 6319ce026..d7be4f5c8 100644 --- a/packages/openapi-typescript/test/transform/paths-object.test.ts +++ b/packages/openapi-typescript/test/transform/paths-object.test.ts @@ -274,7 +274,7 @@ describe("transformPathsObject", () => { "options > pathParamsAsTypes: true", { given: { - "/api/v1/user/me": { + "/api/v1/user/{user_id}": { parameters: [ { name: "page", @@ -282,6 +282,11 @@ describe("transformPathsObject", () => { schema: { type: "number" }, description: "Page number.", }, + { + name: "user_id", + in: "path", + schema: { format: "int64", type: "integer" }, + }, ], get: { parameters: [], @@ -315,14 +320,16 @@ describe("transformPathsObject", () => { }, }, want: `{ - "/api/v1/user/me": { + [path: \`/api/v1/user/\${number}\`]: { parameters: { query?: { /** @description Page number. */ page?: number; }; header?: never; - path?: never; + path: { + user_id: number; + }; cookie?: never; }; get: { @@ -332,7 +339,9 @@ describe("transformPathsObject", () => { page?: number; }; header?: never; - path?: never; + path: { + user_id: number; + }; cookie?: never; }; requestBody?: never;