From 754a6b53e4affb75016a4a1cefab13c41fa83faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augustin=20=C5=A0ulc?= Date: Tue, 14 Feb 2023 11:42:34 +0100 Subject: [PATCH] OpenAPI generator better handles malformed input --- .vscode/launch.json | 4 ++-- packages/generator/src/index.ts | 6 +++--- .../src/openapi/parsers/openApi3Parser.ts | 14 ++++++++++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index dee256e..55202ba 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "Attach to Chrome", "port": 9222, "request": "attach", - "type": "pwa-chrome", + "type": "chrome", "webRoot": "${workspaceFolder}" }, { @@ -23,7 +23,7 @@ } }, { - "type": "pwa-node", + "type": "node", "request": "launch", "name": "Debug Current Test File", "autoAttachChildProcesses": true, diff --git a/packages/generator/src/index.ts b/packages/generator/src/index.ts index 02a9895..234dbff 100644 --- a/packages/generator/src/index.ts +++ b/packages/generator/src/index.ts @@ -24,7 +24,7 @@ program config: options.config, debug: options.debug, }; - console.log("Processing ", params.config); + console.log("Processing", params.config); if (options.decorators) { params.decorators = { output: options.decoratorsOutput }; @@ -53,7 +53,7 @@ program output: options.output, debug: options.debug, }; - console.log("Processing ", params.config); + console.log("Processing", params.config); const GeneratorType = await import("./views"); const generator = new GeneratorType.default(params); @@ -74,7 +74,7 @@ program config: options.config, debug: options.debug, }; - console.log("Processing ", params.config); + console.log("Processing", params.config); const GeneratorType = await import("./openapi"); const generator = new GeneratorType.default(params); diff --git a/packages/generator/src/openapi/parsers/openApi3Parser.ts b/packages/generator/src/openapi/parsers/openApi3Parser.ts index 8773815..890e935 100644 --- a/packages/generator/src/openapi/parsers/openApi3Parser.ts +++ b/packages/generator/src/openapi/parsers/openApi3Parser.ts @@ -37,6 +37,8 @@ export default class OpenApi3Parser implements ApiModel { } parseSchemaObject(name: string, definition: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject): TypeReference { + console.debug("Parsing object", name); + if (isV3ReferenceObject(definition)) { return this.parseReferenceObject(definition); } else if (definition.enum) { @@ -223,6 +225,8 @@ export default class OpenApi3Parser implements ApiModel { } private parseEndpoint({ path, method, action }: { path: string; method: string; action: OpenAPIV3.OperationObject }) { + console.debug("Parsing endpoint", method, path); + path = this.config?.endpointUrlPrefix ? path.replace(this.config.endpointUrlPrefix, "") : path; const name = action.operationId ?? camelCase(method + "-" + path.replace(/\{(\D*?)\}/, "By-$1")); // the dash makes sure first path word starts with upper case @@ -257,10 +261,12 @@ export default class OpenApi3Parser implements ApiModel { } endpoint.responses = {}; - Object.entries(action.responses).forEach(([code, response]) => { - const responseBodyType = this.getResponseBodyType(`${name}Response${code}`, response); - (endpoint.responses as Record)[code] = responseBodyType; - }); + if (action.responses as unknown) { + Object.entries(action.responses).forEach(([code, response]) => { + const responseBodyType = this.getResponseBodyType(`${name}Response${code}`, response); + (endpoint.responses as Record)[code] = responseBodyType; + }); + } return endpoint; }