Skip to content

Commit 5085c5a

Browse files
committed
fix(serverless-openapi): parse request body correctly and add to openapi file
1 parent 54e54b2 commit 5085c5a

File tree

1 file changed

+81
-29
lines changed

1 file changed

+81
-29
lines changed

packages/serverless-openapi/src/index.ts

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,27 @@ type CommandsDefinition = Record<
2323
}
2424
>;
2525

26+
interface Schema {
27+
schema?: JSONSchema7;
28+
name: string;
29+
description?: string;
30+
}
31+
2632
interface HttpEvent {
2733
path: string;
2834
method: HttpMethod;
2935
authorizer?: any;
3036
cors?: any;
3137
operationId: string;
3238
integration?: string | undefined;
33-
responseSchemas: {
34-
[key: string]: {
35-
'application/json': {
36-
schema?: JSONSchema7;
37-
name: string;
38-
description?: string;
39-
};
39+
request: {
40+
schemas: {
41+
'application/json': Schema;
4042
};
4143
};
44+
responseSchemas: {
45+
[key: string]: { 'application/json': Schema };
46+
};
4247
}
4348

4449
export class ServerlessPlugin {
@@ -150,31 +155,24 @@ export class ServerlessPlugin {
150155
openApi.paths[path] = {};
151156
}
152157

153-
const responseSchemas = httpEvent.responseSchemas;
154-
155-
const responses: OpenAPIV3.ResponsesObject = {};
156-
157-
for (const code of Object.keys(responseSchemas)) {
158-
const schema = responseSchemas[code]['application/json'];
159-
openApi.components.schemas[schema.name] = schema.schema as any;
160-
responses[code] = {
161-
description: schema.description,
162-
};
163-
if (schema.schema) {
164-
responses[code]['content'] = {
165-
'application/json': {
166-
schema: {
167-
$ref: '#/components/schemas/' + schema.name,
168-
},
169-
},
170-
};
171-
}
172-
}
158+
const responses = this.handleResponses(
159+
httpEvent.responseSchemas,
160+
openApi
161+
);
173162

174-
openApi.paths[path][this.getMethod(httpEvent.method)] = {
163+
const operation: OpenAPIV3.OperationObject = {
175164
operationId: httpEvent.operationId,
176165
responses: responses,
177-
} as OpenAPIV3.OperationObject;
166+
};
167+
168+
if (httpEvent.request && httpEvent.request.schemas) {
169+
operation.requestBody = this.handleRequestBody(
170+
httpEvent.request.schemas,
171+
openApi
172+
);
173+
}
174+
175+
openApi.paths[path][this.getMethod(httpEvent.method)] = operation;
178176
}
179177
}
180178

@@ -190,6 +188,60 @@ export class ServerlessPlugin {
190188

191189
writeFileSync(out, output);
192190
}
191+
192+
private handleRequestBody(
193+
requestSchemas: { 'application/json': Schema },
194+
openApi: OpenAPIV3.Document
195+
) {
196+
const request: OpenAPIV3.RequestBodyObject = {
197+
content: {},
198+
required: true,
199+
};
200+
201+
const schemaJSON = requestSchemas['application/json'];
202+
request.description = schemaJSON.description;
203+
204+
if (schemaJSON.schema) {
205+
request['content'] = {
206+
'application/json': {
207+
schema: {
208+
$ref: '#/components/schemas/' + schemaJSON.name,
209+
},
210+
},
211+
};
212+
delete schemaJSON.schema['$schema'];
213+
openApi.components.schemas[schemaJSON.name] = schemaJSON.schema as any;
214+
}
215+
return request;
216+
}
217+
218+
private handleResponses(
219+
responseSchemas: { [key: string]: { 'application/json': Schema } },
220+
openApi: OpenAPIV3.Document
221+
) {
222+
const responses: OpenAPIV3.ResponsesObject = {};
223+
224+
for (const code of Object.keys(responseSchemas)) {
225+
const schemaJSON = responseSchemas[code]['application/json'];
226+
responses[code] = {
227+
description: schemaJSON.description,
228+
};
229+
if (schemaJSON.schema) {
230+
responses[code]['content'] = {
231+
'application/json': {
232+
schema: {
233+
$ref: '#/components/schemas/' + schemaJSON.name,
234+
},
235+
},
236+
};
237+
delete schemaJSON.schema['$schema'];
238+
openApi.components.schemas[schemaJSON.name] = schemaJSON.schema as any;
239+
}
240+
}
241+
242+
return responses;
243+
}
244+
193245
getMethod(method: HttpMethod): OpenAPIV3.HttpMethods {
194246
switch (method) {
195247
case 'get':

0 commit comments

Comments
 (0)