Skip to content

Commit c73b06c

Browse files
committed
fix(generator): dereference aws request bodies
1 parent 9f99004 commit c73b06c

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

packages/serverless-openapi/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export class ServerlessPlugin {
6060

6161
private async generate() {
6262
this.log.notice('Generate open api');
63-
const openApi = new Generator(this.log).generate(this.serverless);
63+
const openApi = await new Generator(this.log).generate(this.serverless);
6464
const customOpenApi = this.serverless.service.custom
6565
.openapi as CustomProperties;
6666

6767

68-
const api = await $RefParser.bundle(openApi as any, {
68+
const api = await $RefParser.bundle(JSON.parse(JSON.stringify(openApi as any)), {
6969
resolve: {
7070
file: {
7171
canRead: ['.yml', '.json'],
@@ -77,9 +77,10 @@ export class ServerlessPlugin {
7777
}
7878
}
7979
});
80-
8180
this.log.debug(`API name: ${openApi.info.title}, Version: ${openApi.info.version}`);
8281
this.saveToFile(api, customOpenApi.out);
82+
83+
8384
}
8485

8586
private saveToFile(openApi: any, out = 'openapi.json') {

packages/serverless-openapi/src/lib/generator.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import Aws, {
66
HttpRequestParametersValidation,
77
} from 'serverless/plugins/aws/provider/awsProvider';
88
import { Log } from './sls.types';
9+
import $RefParser from '@apidevtools/json-schema-ref-parser';
10+
import { readFile } from 'fs/promises';
11+
import * as path from 'path';
912

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

26-
public generate(serverless: Serverless): OpenAPIV3.Document {
29+
public async generate(serverless: Serverless): Promise<OpenAPIV3.Document> {
2730
const openApi: OpenAPIV3.Document = {
2831
openapi: '3.0.0',
2932
info: {
@@ -70,15 +73,17 @@ export class Generator {
7073
}
7174

7275
const httpEvent = event['http'] as HttpEvent;
73-
const path = '/' + httpEvent.path;
74-
if (!openApi.paths[path]) {
75-
openApi.paths[path] = {};
76+
const httpPath = '/' + httpEvent.path;
77+
if (!openApi.paths[httpPath]) {
78+
openApi.paths[httpPath] = {};
7679
}
7780

7881
const responses = this.handleResponses(
7982
httpEvent.responseSchemas,
8083
openApi
8184
);
85+
// Clean up, as not needed in serverless
86+
delete httpEvent.responseSchemas
8287

8388
const auth: OpenAPIV3.SecurityRequirementObject[] | undefined = [];
8489
if (httpEvent.authorizer) {
@@ -121,9 +126,23 @@ export class Generator {
121126
httpEvent.request.schemas,
122127
openApi
123128
);
129+
130+
httpEvent.request.schemas = await $RefParser.dereference(JSON.parse(JSON.stringify(httpEvent.request.schemas)), {
131+
resolve: {
132+
file: {
133+
canRead: ['.yml', '.json'],
134+
read: async (ref) => {
135+
const orgRef = (ref.url as string).replace(process.cwd(), "")
136+
const realPath = path.join(process.cwd(), customOpenApi.schemaFolder, orgRef )
137+
return await readFile(realPath)
138+
}
139+
}
140+
}
141+
}) as any;
142+
124143
}
125144

126-
openApi.paths[path][this.getMethod(httpEvent.method)] = operation;
145+
openApi.paths[httpPath][this.getMethod(httpEvent.method)] = operation;
127146
}
128147
}
129148

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

139158
return openApi;
140159
}
160+
141161
private handleParameters(
142162
httpEvent: HttpEvent
143163
): OpenAPIV3.ParameterObject[] | undefined {
@@ -245,6 +265,7 @@ export class Generator {
245265
};
246266
delete schemaJSON.schema['$schema'];
247267
openApi.components.schemas[schemaJSON.name] = schemaJSON.schema as any;
268+
248269
}
249270
}
250271

0 commit comments

Comments
 (0)