@@ -6,6 +6,9 @@ import Aws, {
66 HttpRequestParametersValidation ,
77} from 'serverless/plugins/aws/provider/awsProvider' ;
88import { 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
1013interface HttpEvent extends Aws . Http {
1114 operationId : string ;
@@ -23,7 +26,7 @@ interface HttpEvent extends Aws.Http {
2326export 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