From f5a9a48bf545d7fc286b7714af6dc136a11fa8c3 Mon Sep 17 00:00:00 2001 From: Lukas Horak Date: Thu, 1 Jun 2023 09:00:43 +0200 Subject: [PATCH] fix: Avoid index signature error for paths with empty params --- .changeset/selfish-ears-hope.md | 5 ++ .../src/transform/paths-object.ts | 2 +- .../test/paths-object.test.ts | 72 +++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 .changeset/selfish-ears-hope.md diff --git a/.changeset/selfish-ears-hope.md b/.changeset/selfish-ears-hope.md new file mode 100644 index 000000000..f8670f56f --- /dev/null +++ b/.changeset/selfish-ears-hope.md @@ -0,0 +1,5 @@ +--- +"openapi-typescript": patch +--- + +Avoid index signature TS error for paths with empty params diff --git a/packages/openapi-typescript/src/transform/paths-object.ts b/packages/openapi-typescript/src/transform/paths-object.ts index f569cf4cb..4dac4d3a8 100644 --- a/packages/openapi-typescript/src/transform/paths-object.ts +++ b/packages/openapi-typescript/src/transform/paths-object.ts @@ -25,7 +25,7 @@ export default function transformPathsObject(pathsObject: PathsObject, ctx: Glob const pathParams = new Map([...extractPathParams(pathItemObject), ...OPERATIONS.flatMap((op) => Array.from(extractPathParams(pathItemObject[op as keyof PathItemObject])))]); // build dynamic string template literal index - if (ctx.pathParamsAsTypes && pathParams) { + if (ctx.pathParamsAsTypes && pathParams.size) { for (const p of pathParams.values()) { const paramType = transformParameterObject(p, { path: `#/paths/${url}/parameters/${p.name}`, ctx }); path = path.replace(`{${p.name}}`, `\${${paramType}}`); diff --git a/packages/openapi-typescript/test/paths-object.test.ts b/packages/openapi-typescript/test/paths-object.test.ts index cb8bee415..8a79598b9 100644 --- a/packages/openapi-typescript/test/paths-object.test.ts +++ b/packages/openapi-typescript/test/paths-object.test.ts @@ -145,6 +145,78 @@ describe("Paths Object", () => { }; }; }; +}`); + }); + test("empty params", () => { + const schema: PathsObject = { + "/api/v1/user/me": { + parameters: [{ name: "page", in: "query", schema: { type: "number" }, description: "Page number." }], + get: { + parameters: [], + responses: { + 200: { + description: "OK", + headers: { + Link: { + $ref: 'components["headers"]["link"]', + }, + }, + content: { + "application/json": { + schema: { + type: "object", + properties: { + id: { type: "string" }, + email: { type: "string" }, + name: { type: "string" }, + }, + required: ["id", "email"], + }, + }, + }, + }, + 404: { + $ref: 'components["responses"]["NotFound"]', + }, + }, + }, + }, + }; + + const generated = transformPathsObject(schema, { ...options, pathParamsAsTypes: true }); + expect(generated).toBe(`{ + "/api/v1/user/me": { + get: { + parameters: { + query?: { + /** @description Page number. */ + page?: number; + }; + }; + responses: { + /** @description OK */ + 200: { + headers: { + Link: components["headers"]["link"]; + }; + content: { + "application/json": { + id: string; + email: string; + name?: string; + }; + }; + }; + 404: components["responses"]["NotFound"]; + }; + }; + parameters: { + query?: { + /** @description Page number. */ + page?: number; + }; + }; + }; }`); }); });