Skip to content

Commit

Permalink
Fix path params as types (#1982)
Browse files Browse the repository at this point in the history
* fix(openapi-typescript): Make pathParamsAsTypes work with integer/boolean types

* Update changeset
  • Loading branch information
prewk authored Nov 8, 2024
1 parent 343cadf commit 8d00218
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-queens-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Make pathParamsAsTypes work with integer/boolean types
15 changes: 11 additions & 4 deletions packages/openapi-typescript/src/transform/paths-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
17 changes: 13 additions & 4 deletions packages/openapi-typescript/test/transform/paths-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,19 @@ describe("transformPathsObject", () => {
"options > pathParamsAsTypes: true",
{
given: {
"/api/v1/user/me": {
"/api/v1/user/{user_id}": {
parameters: [
{
name: "page",
in: "query",
schema: { type: "number" },
description: "Page number.",
},
{
name: "user_id",
in: "path",
schema: { format: "int64", type: "integer" },
},
],
get: {
parameters: [],
Expand Down Expand Up @@ -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: {
Expand All @@ -332,7 +339,9 @@ describe("transformPathsObject", () => {
page?: number;
};
header?: never;
path?: never;
path: {
user_id: number;
};
cookie?: never;
};
requestBody?: never;
Expand Down

0 comments on commit 8d00218

Please sign in to comment.