diff --git a/src/types/OpenAPI3.ts b/src/types/OpenAPI3.ts index 98a693ce1..7d597afcf 100644 --- a/src/types/OpenAPI3.ts +++ b/src/types/OpenAPI3.ts @@ -9,13 +9,27 @@ export interface OpenAPI3Schemas { } export interface OpenAPI3Paths { - [path: string]: { - [method: string]: OpenAPI3Operation | Parameter[]; - }; + [path: string]: OpenAPI3PathItemObject; } +export type OpenAPI3PathItemObject = { + $ref?: string; + summary?: string; + description?: string; + get?: OpenAPI3Operation; + put?: OpenAPI3Operation; + post?: OpenAPI3Operation; + delete?: OpenAPI3Operation; + options?: OpenAPI3Operation; + head?: OpenAPI3Operation; + patch?: OpenAPI3Operation; + trace?: OpenAPI3Operation; + parameters?: Parameter[]; +}; + export interface OpenAPI3Operation { operationId?: string; + summary?: string; description?: string; parameters?: Parameter[]; requestBody?: OpenAPI3RequestBody; diff --git a/src/v3.ts b/src/v3.ts index 3f8e83209..3228cffd2 100644 --- a/src/v3.ts +++ b/src/v3.ts @@ -261,29 +261,40 @@ export default function generateTypesV3( function transformPaths(paths: OpenAPI3Paths): string { let output = ""; - Object.entries(paths).forEach(([path, methods]) => { + Object.entries(paths).forEach(([path, pathItem]) => { output += `"${path}": {\n`; - Object.entries(methods).forEach(([method, operation]) => { + Object.entries(pathItem).forEach(([field, operation]) => { // skip the parameters "method" for shared parameters - we'll handle it later - if (method !== "parameters") { + const isMethod = [ + "get", + "put", + "post", + "delete", + "options", + "head", + "patch", + "trace", + ].includes(field); + + if (isMethod) { operation = operation as OpenAPI3Operation; if (operation.operationId) { - output += `"${method}": operations["${operation.operationId}"];\n`; + output += `"${field}": operations["${operation.operationId}"];\n`; operations[operation.operationId] = operation; } else { if (operation.description) output += comment(operation.description); - output += `"${method}": ${transformOperation( + output += `"${field}": ${transformOperation( operation as OpenAPI3Operation )}`; } } }); - if (methods.parameters) { + if (pathItem.parameters) { // Handle shared parameters - output += transformParameters(methods.parameters as Parameter[]); + output += transformParameters(pathItem.parameters as Parameter[]); } output += `}\n`; }); diff --git a/tests/v3/index.test.ts b/tests/v3/index.test.ts index ce4d68ffb..437293314 100644 --- a/tests/v3/index.test.ts +++ b/tests/v3/index.test.ts @@ -988,4 +988,39 @@ describe("OpenAPI3 features", () => { `) ); }); + + it("paths include 'summary' and 'description'", () => { + const schema: OpenAPI3 = { + openapi: "3.0.1", + paths: { + "/": { + summary: "root summary", + description: "root description", + get: { + summary: "get summary", + description: "get description", + responses: { }, + }, + }, + } + }; + + expect(swaggerToTS(schema)).toBe( + format(` + export interface paths { + '/': { + /** + * get description + */ + get: { + responses: { } + } + }; + } + + export interface operations {} + + export interface components {}`) + ); + }); });