@@ -6,6 +6,9 @@ import Aws, {
6
6
HttpRequestParametersValidation ,
7
7
} from 'serverless/plugins/aws/provider/awsProvider' ;
8
8
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' ;
9
12
10
13
interface HttpEvent extends Aws . Http {
11
14
operationId : string ;
@@ -23,7 +26,7 @@ interface HttpEvent extends Aws.Http {
23
26
export class Generator {
24
27
constructor ( private log : Log ) { }
25
28
26
- public generate ( serverless : Serverless ) : OpenAPIV3 . Document {
29
+ public async generate ( serverless : Serverless ) : Promise < OpenAPIV3 . Document > {
27
30
const openApi : OpenAPIV3 . Document = {
28
31
openapi : '3.0.0' ,
29
32
info : {
@@ -70,15 +73,17 @@ export class Generator {
70
73
}
71
74
72
75
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 ] = { } ;
76
79
}
77
80
78
81
const responses = this . handleResponses (
79
82
httpEvent . responseSchemas ,
80
83
openApi
81
84
) ;
85
+ // Clean up, as not needed in serverless
86
+ delete httpEvent . responseSchemas
82
87
83
88
const auth : OpenAPIV3 . SecurityRequirementObject [ ] | undefined = [ ] ;
84
89
if ( httpEvent . authorizer ) {
@@ -121,9 +126,23 @@ export class Generator {
121
126
httpEvent . request . schemas ,
122
127
openApi
123
128
) ;
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
+
124
143
}
125
144
126
- openApi . paths [ path ] [ this . getMethod ( httpEvent . method ) ] = operation ;
145
+ openApi . paths [ httpPath ] [ this . getMethod ( httpEvent . method ) ] = operation ;
127
146
}
128
147
}
129
148
@@ -138,6 +157,7 @@ export class Generator {
138
157
139
158
return openApi ;
140
159
}
160
+
141
161
private handleParameters (
142
162
httpEvent : HttpEvent
143
163
) : OpenAPIV3 . ParameterObject [ ] | undefined {
@@ -245,6 +265,7 @@ export class Generator {
245
265
} ;
246
266
delete schemaJSON . schema [ '$schema' ] ;
247
267
openApi . components . schemas [ schemaJSON . name ] = schemaJSON . schema as any ;
268
+
248
269
}
249
270
}
250
271
0 commit comments