Skip to content
This repository was archived by the owner on Jul 23, 2021. It is now read-only.

Commit 78a81be

Browse files
lineadtchock
authored andcommitted
Allow for swagger document to be passed in custom.documentation (#76)
1 parent 0450b58 commit 78a81be

File tree

3 files changed

+823
-1
lines changed

3 files changed

+823
-1
lines changed

src/index.js

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const documentation = require('./documentation');
33
const models = require('./models');
4+
const swagger = require('./swagger');
45
const fs = require('fs');
56
const downloadDocumentation = require('./downloadDocumentation');
67

@@ -12,6 +13,7 @@ class ServerlessAWSDocumentation {
1213
this.fs = fs;
1314

1415
Object.assign(this, models);
16+
Object.assign(this, swagger);
1517
Object.assign(this, documentation());
1618
Object.assign(this, downloadDocumentation);
1719

@@ -52,9 +54,92 @@ class ServerlessAWSDocumentation {
5254

5355
beforeDeploy() {
5456
this.customVars = this.serverless.variables.service.custom;
55-
5657
if (!(this.customVars && this.customVars.documentation)) return;
5758

59+
if (this.customVars.documentation.swagger) {
60+
// Handle references to models
61+
this.replaceSwaggerDefinitions(this.customVars.documentation.definitions)
62+
//Map swagger into documentation models
63+
const swaggerDefs = this.customVars.documentation.definitions
64+
if (swaggerDefs) {
65+
const swaggerModels = Object.keys(swaggerDefs).map(definitionName => {
66+
return {
67+
name: definitionName,
68+
description: swaggerDefs[definitionName].description,
69+
contentType: 'application/json',
70+
schema: swaggerDefs[definitionName]
71+
}
72+
})
73+
this.customVars.documentation.models = swaggerModels
74+
} else {
75+
this.customVars.documentation.models = []
76+
}
77+
78+
//Find http events and map the swagger across
79+
this.serverless.service.getAllFunctions().forEach(functionName => {
80+
const func = this.serverless.service.getFunction(functionName)
81+
if (func.events) {
82+
func.events.forEach(event => {
83+
if (event.http) {
84+
// look up the path in the swagger
85+
const path = this.customVars.documentation.paths['/' + event.http.path]
86+
if (path) {
87+
const method = path[event.http.method]
88+
const methodDoc = {'requestHeaders': [], 'pathParams': [], 'queryParams': [],
89+
'requestModels': {}}
90+
if ( method.parameters ) {
91+
method.parameters.forEach(param => {
92+
if (param.in === 'header') {
93+
methodDoc['requestHeaders'].push({
94+
name: param.name,
95+
description: param.description,
96+
required: param.required
97+
})
98+
} else if (param.in === 'path') {
99+
methodDoc['pathParams'].push({
100+
name: param.name,
101+
description: param.description,
102+
required: param.required
103+
})
104+
} else if (param.in === 'query') {
105+
methodDoc['queryParams'].push({
106+
name: param.name,
107+
description: param.description,
108+
required: param.required
109+
})
110+
} else if (param.in === 'body') {
111+
methodDoc['requestModels']['application/json'] =
112+
this.extractModel(param, this.customVars.documentation.models);
113+
}
114+
})
115+
}
116+
117+
if ( method.responses ) {
118+
methodDoc['methodResponses'] = []
119+
Object.keys(method.responses).map(statusCode => {
120+
const response = method.responses[statusCode];
121+
const methodResponse = {
122+
statusCode: ""+statusCode,
123+
};
124+
125+
if ( response.schema ) {
126+
const responseModels = {};
127+
responseModels['application/json'] =
128+
this.extractModel(response, this.customVars.documentation.models);
129+
methodResponse['responseModels'] = responseModels;
130+
}
131+
methodDoc['methodResponses'].push(methodResponse);
132+
});
133+
}
134+
135+
event.http.documentation = methodDoc
136+
}
137+
}
138+
})
139+
}
140+
})
141+
}
142+
58143
this.cfTemplate = this.serverless.service.provider.compiledCloudFormationTemplate;
59144

60145
// The default rest API reference

0 commit comments

Comments
 (0)