Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[E4E-0]: Account for Swagger 2 differences in file generation #10

Merged
merged 2 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [1.1.1](https://github.com/nrccua/apollo-rest-utils/compare/1.1.0...1.1.1) (2021-12-03)


### Changes

* [E4E-0]: Account for Swagger 2 differences in file generation ([5027a5e](https://github.com/nrccua/apollo-rest-utils/commit/5027a5e59a2e9a1be84a3dbde1f7009a727717f4))
* Merge pull request #8 from nrccua/E4E-30 ([35f0cdf](https://github.com/nrccua/apollo-rest-utils/commit/35f0cdfe80bce8bf193fbd13a6b28e70607e7a98)), closes [#8](https://github.com/nrccua/apollo-rest-utils/issues/8)
* [E4E-30]: 1.1.0 ([e1ba57b](https://github.com/nrccua/apollo-rest-utils/commit/e1ba57baef0c994981cc46ad558bc2576f6f0008))
* Merge pull request #7 from nrccua/E4E-30 ([43d8873](https://github.com/nrccua/apollo-rest-utils/commit/43d887310cf42351d04f4ef0224e934ebb2ca2b2)), closes [#7](https://github.com/nrccua/apollo-rest-utils/issues/7)
* Merge pull request #6 from nrccua/E4E-30 ([967c124](https://github.com/nrccua/apollo-rest-utils/commit/967c1246d0532ce397193cadc223ea84403b4d08)), closes [#6](https://github.com/nrccua/apollo-rest-utils/issues/6)
* Merge pull request #5 from nrccua/E4E-30 ([3ebd56f](https://github.com/nrccua/apollo-rest-utils/commit/3ebd56f0e219427a79f42412dcccc116c77e6544)), closes [#5](https://github.com/nrccua/apollo-rest-utils/issues/5)
* Merge pull request #4 from nrccua/E4E-30 ([7a655df](https://github.com/nrccua/apollo-rest-utils/commit/7a655df9628fbd3d7eb3e28ae6d0291551ae9f2c)), closes [#4](https://github.com/nrccua/apollo-rest-utils/issues/4)
* Merge pull request #3 from nrccua/E4E-30 ([2225f29](https://github.com/nrccua/apollo-rest-utils/commit/2225f293b361190bca7042a803359fbfc0627a56)), closes [#3](https://github.com/nrccua/apollo-rest-utils/issues/3)
* Merge pull request #2 from nrccua/E4E-30 ([96c0a20](https://github.com/nrccua/apollo-rest-utils/commit/96c0a20c048c270dd5b64ed92a041aeeac5495a7)), closes [#2](https://github.com/nrccua/apollo-rest-utils/issues/2)
* Merge pull request #1 from nrccua/E4E-30 ([29cd838](https://github.com/nrccua/apollo-rest-utils/commit/29cd8384f0c155c56a2db9f74a1a62739a56b54c)), closes [#1](https://github.com/nrccua/apollo-rest-utils/issues/1)

## [1.1.0](https://github.com/nrccua/apollo-rest-utils/compare/1.0.14...1.1.0) (2021-11-29)


Expand Down
44 changes: 29 additions & 15 deletions lib/generateRoutes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import fs from 'fs';
import path from 'path';

import SwaggerParser from '@apidevtools/swagger-parser';
import _ from 'lodash';
import _, { first } from 'lodash';
import { OpenAPI, OpenAPIV3 } from 'openapi-types';
import openapiTS from 'openapi-typescript';
import openapiTS, { ReferenceObject, ResponseObject, SchemaObject } from 'openapi-typescript';
import prettier from 'prettier';

import { RestEndpointSchema } from '../types';
Expand All @@ -24,7 +24,9 @@ export function addArgsToPath(endpointPath: string, parameters: OpenAPIV3.Parame
: endpointPath.substr(1).replace(/{/g, '{args.') + (queryParams.length > 0 ? `?${queryParams.join('&')}` : '');
}

export function getResponseSchema(properties?: { [name: string]: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject }): RestEndpointSchema | undefined {
export function getResponseSchema(properties?: {
[name: string]: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | ReferenceObject | SchemaObject;
}): RestEndpointSchema | undefined {
if (!properties) {
return undefined;
}
Expand Down Expand Up @@ -68,7 +70,7 @@ export function generateTypescript(api: OpenAPI.Document, typeImportLocation: st
let generatedTSEndpoints =
'/**\n* This file was auto-generated by the generateRoutes endpoint generator.\n* Do not make direct changes to the file.\n' +
`* To update this file run \`npx apollo-rest-utils <swagger_definition> <directory_of_this_file>\`\n*/\n\n`;
generatedTSEndpoints += `import {operations} from '${typeImportLocation}'\n\nimport { IRestEndpoint} from 'apollo-rest-utils';\n\n\n`;
generatedTSEndpoints += `import { paths } from '${typeImportLocation}'\n\nimport { IRestEndpoint} from 'apollo-rest-utils';\n\n\n`;
// Create an object representing routes by method, e.g., { 'get': {}, ... }
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
const routes = Object.entries(OpenAPIV3.HttpMethods).reduce(
Expand All @@ -87,30 +89,42 @@ export function generateTypescript(api: OpenAPI.Document, typeImportLocation: st
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const endpointObject = pathObject[method];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
let responseBody = `operations['${endpointObject.operationId as string}']['responses']`;
let responseBody = `paths['${endpointPath}']['${method}']['responses']`;
let responseSchema;
let isArray = false;
const okResponse = _.first(Object.keys(endpointObject.responses).sort());
if (okResponse && parseInt(okResponse, 10) < 300) {
responseBody += `['${okResponse}']`;
const responseObject = endpointObject.responses[okResponse] as OpenAPIV3.ResponseObject;
const responseObject = endpointObject.responses[okResponse] as OpenAPIV3.ResponseObject | ResponseObject;
let schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | ReferenceObject | SchemaObject | undefined;
if (responseObject?.content) {
responseBody += `['content']`;
if (responseObject?.content?.['application/json']) {
responseBody += `['application/json']`;
const schema = responseObject.content['application/json'].schema as OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
if ('properties' in schema) {
responseSchema = getResponseSchema(schema.properties);
} else if ('items' in schema && 'properties' in schema.items) {
isArray = true;
responseSchema = getResponseSchema(schema.items.properties);
}
schema = responseObject.content['application/json'].schema as OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
}
} else if ('schema' in responseObject) {
responseBody += `['schema']`;
schema = responseObject.schema;
}
if (schema && 'properties' in schema) {
responseSchema = getResponseSchema(schema.properties);
} else if (schema && 'items' in schema && schema.items && 'properties' in schema.items) {
isArray = true;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
responseSchema = getResponseSchema(schema.items.properties);
}
}
const requestBodyObject = endpointObject.requestBody as OpenAPIV3.RequestBodyObject;
const requestBody = Object.keys(requestBodyObject?.content ?? {}).includes('application/json')
? `operations['${endpointObject.operationId as string}']['requestBody']['content']['application/json']`
const contentKeys = Object.keys(requestBodyObject?.content ?? {});
const bodyParam = first(endpointObject.parameters?.filter(p => 'name' in p && p.name.toLowerCase() === 'body'));
// eslint-disable-next-line no-nested-ternary
const requestBody = contentKeys.includes('application/json')
? // OpenAPI 3 way
`paths['${endpointPath}']['${method}']['requestBody']['content']['application/json']`
: // Swagger 2 way
bodyParam
? `paths['${endpointPath}']['${method}']['parameters']['body']['body']`
: undefined;
const headers = endpointObject.parameters?.filter(p => 'in' in p && p.in === 'header').map(p => p as OpenAPIV3.ParameterObject) ?? [];
routes[method as Uppercase<OpenAPIV3.HttpMethods>][
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@
"update:version:major": "standard-version --release-as major",
"update:version:minor": "standard-version --release-as minor"
},
"version": "1.1.0"
"version": "1.1.1"
}
Loading