Skip to content

Commit

Permalink
OpenAPI generator better handles malformed input
Browse files Browse the repository at this point in the history
  • Loading branch information
gius committed Feb 14, 2023
1 parent 5957740 commit 754a6b5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Attach to Chrome",
"port": 9222,
"request": "attach",
"type": "pwa-chrome",
"type": "chrome",
"webRoot": "${workspaceFolder}"
},
{
Expand All @@ -23,7 +23,7 @@
}
},
{
"type": "pwa-node",
"type": "node",
"request": "launch",
"name": "Debug Current Test File",
"autoAttachChildProcesses": true,
Expand Down
6 changes: 3 additions & 3 deletions packages/generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions packages/generator/src/openapi/parsers/openApi3Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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<string, unknown>)[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<string, unknown>)[code] = responseBodyType;
});
}

return endpoint;
}
Expand Down

0 comments on commit 754a6b5

Please sign in to comment.