Skip to content

Commit

Permalink
fix(generator): dereference aws request bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightapes committed Mar 20, 2023
1 parent 9f99004 commit c73b06c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
7 changes: 4 additions & 3 deletions packages/serverless-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export class ServerlessPlugin {

private async generate() {
this.log.notice('Generate open api');
const openApi = new Generator(this.log).generate(this.serverless);
const openApi = await new Generator(this.log).generate(this.serverless);
const customOpenApi = this.serverless.service.custom
.openapi as CustomProperties;


const api = await $RefParser.bundle(openApi as any, {
const api = await $RefParser.bundle(JSON.parse(JSON.stringify(openApi as any)), {
resolve: {
file: {
canRead: ['.yml', '.json'],
Expand All @@ -77,9 +77,10 @@ export class ServerlessPlugin {
}
}
});

this.log.debug(`API name: ${openApi.info.title}, Version: ${openApi.info.version}`);
this.saveToFile(api, customOpenApi.out);


}

private saveToFile(openApi: any, out = 'openapi.json') {
Expand Down
31 changes: 26 additions & 5 deletions packages/serverless-openapi/src/lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import Aws, {
HttpRequestParametersValidation,
} from 'serverless/plugins/aws/provider/awsProvider';
import { Log } from './sls.types';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { readFile } from 'fs/promises';
import * as path from 'path';

interface HttpEvent extends Aws.Http {
operationId: string;
Expand All @@ -23,7 +26,7 @@ interface HttpEvent extends Aws.Http {
export class Generator {
constructor(private log: Log) {}

public generate(serverless: Serverless): OpenAPIV3.Document {
public async generate(serverless: Serverless): Promise<OpenAPIV3.Document> {
const openApi: OpenAPIV3.Document = {
openapi: '3.0.0',
info: {
Expand Down Expand Up @@ -70,15 +73,17 @@ export class Generator {
}

const httpEvent = event['http'] as HttpEvent;
const path = '/' + httpEvent.path;
if (!openApi.paths[path]) {
openApi.paths[path] = {};
const httpPath = '/' + httpEvent.path;
if (!openApi.paths[httpPath]) {
openApi.paths[httpPath] = {};
}

const responses = this.handleResponses(
httpEvent.responseSchemas,
openApi
);
// Clean up, as not needed in serverless
delete httpEvent.responseSchemas

const auth: OpenAPIV3.SecurityRequirementObject[] | undefined = [];
if (httpEvent.authorizer) {
Expand Down Expand Up @@ -121,9 +126,23 @@ export class Generator {
httpEvent.request.schemas,
openApi
);

httpEvent.request.schemas = await $RefParser.dereference(JSON.parse(JSON.stringify(httpEvent.request.schemas)), {
resolve: {
file: {
canRead: ['.yml', '.json'],
read: async (ref) => {
const orgRef = (ref.url as string).replace(process.cwd(), "")
const realPath = path.join(process.cwd(), customOpenApi.schemaFolder, orgRef )
return await readFile(realPath)
}
}
}
}) as any;

}

openApi.paths[path][this.getMethod(httpEvent.method)] = operation;
openApi.paths[httpPath][this.getMethod(httpEvent.method)] = operation;
}
}

Expand All @@ -138,6 +157,7 @@ export class Generator {

return openApi;
}

private handleParameters(
httpEvent: HttpEvent
): OpenAPIV3.ParameterObject[] | undefined {
Expand Down Expand Up @@ -245,6 +265,7 @@ export class Generator {
};
delete schemaJSON.schema['$schema'];
openApi.components.schemas[schemaJSON.name] = schemaJSON.schema as any;

}
}

Expand Down

0 comments on commit c73b06c

Please sign in to comment.