Skip to content

Commit 6f9599e

Browse files
committed
fix: Avoid index signature error for paths with empty params
1 parent 65ff4b1 commit 6f9599e

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function transformPathsObject(pathsObject: PathsObject, ctx: Glob
2525
const pathParams = new Map([...extractPathParams(pathItemObject), ...OPERATIONS.flatMap((op) => Array.from(extractPathParams(pathItemObject[op as keyof PathItemObject])))]);
2626

2727
// build dynamic string template literal index
28-
if (ctx.pathParamsAsTypes && pathParams) {
28+
if (ctx.pathParamsAsTypes && pathParams.size) {
2929
for (const p of pathParams.values()) {
3030
const paramType = transformParameterObject(p, { path: `#/paths/${url}/parameters/${p.name}`, ctx });
3131
path = path.replace(`{${p.name}}`, `\${${paramType}}`);

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

+72
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,78 @@ describe("Paths Object", () => {
145145
};
146146
};
147147
};
148+
}`);
149+
});
150+
test("empty params", () => {
151+
const schema: PathsObject = {
152+
"/api/v1/user/me": {
153+
parameters: [{ name: "page", in: "query", schema: { type: "number" }, description: "Page number." }],
154+
get: {
155+
parameters: [],
156+
responses: {
157+
200: {
158+
description: "OK",
159+
headers: {
160+
Link: {
161+
$ref: 'components["headers"]["link"]',
162+
},
163+
},
164+
content: {
165+
"application/json": {
166+
schema: {
167+
type: "object",
168+
properties: {
169+
id: { type: "string" },
170+
email: { type: "string" },
171+
name: { type: "string" },
172+
},
173+
required: ["id", "email"],
174+
},
175+
},
176+
},
177+
},
178+
404: {
179+
$ref: 'components["responses"]["NotFound"]',
180+
},
181+
},
182+
},
183+
},
184+
};
185+
186+
const generated = transformPathsObject(schema, { ...options, pathParamsAsTypes: true });
187+
expect(generated).toBe(`{
188+
"/api/v1/user/me": {
189+
get: {
190+
parameters: {
191+
query?: {
192+
/** @description Page number. */
193+
page?: number;
194+
};
195+
};
196+
responses: {
197+
/** @description OK */
198+
200: {
199+
headers: {
200+
Link: components["headers"]["link"];
201+
};
202+
content: {
203+
"application/json": {
204+
id: string;
205+
email: string;
206+
name?: string;
207+
};
208+
};
209+
};
210+
404: components["responses"]["NotFound"];
211+
};
212+
};
213+
parameters: {
214+
query?: {
215+
/** @description Page number. */
216+
page?: number;
217+
};
218+
};
219+
};
148220
}`);
149221
});
150222
});

0 commit comments

Comments
 (0)