serverless-openapi-documenter 0.0.71 → 0.0.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -1
- package/package.json +1 -1
- package/src/definitionGenerator.js +879 -730
- package/test/unit/openAPIGenerator.spec.js +241 -200
|
@@ -1,223 +1,264 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const fs = require(
|
|
4
|
-
const PostmanGenerator = require(
|
|
5
|
-
const sinon = require(
|
|
6
|
-
const expect = require(
|
|
7
|
-
|
|
8
|
-
const validOpenAPI = require(
|
|
9
|
-
|
|
10
|
-
const basicDocumentation = require(
|
|
11
|
-
const basicValidFunction = require(
|
|
12
|
-
|
|
13
|
-
const OpenAPIGenerator = require(
|
|
14
|
-
|
|
15
|
-
describe(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const PostmanGenerator = require("openapi-to-postmanv2");
|
|
5
|
+
const sinon = require("sinon");
|
|
6
|
+
const expect = require("chai").expect;
|
|
7
|
+
|
|
8
|
+
const validOpenAPI = require("../json/valid-openAPI.json");
|
|
9
|
+
|
|
10
|
+
const basicDocumentation = require("../models/BasicDocumentation.json");
|
|
11
|
+
const basicValidFunction = require("../models/BasicValidFunction.json");
|
|
12
|
+
|
|
13
|
+
const OpenAPIGenerator = require("../../src/openAPIGenerator");
|
|
14
|
+
|
|
15
|
+
describe("OpenAPIGenerator", () => {
|
|
16
|
+
let sls, logOutput;
|
|
17
|
+
beforeEach(function () {
|
|
18
|
+
sls = {
|
|
19
|
+
service: {
|
|
20
|
+
service: "test-service",
|
|
21
|
+
provider: {
|
|
22
|
+
stage: "test",
|
|
23
|
+
},
|
|
24
|
+
getAllFunctions: () => {},
|
|
25
|
+
getFunction: () => {},
|
|
26
|
+
},
|
|
27
|
+
version: "3.0.0",
|
|
28
|
+
variables: {
|
|
29
|
+
service: {
|
|
30
|
+
custom: {},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
configSchemaHandler: {
|
|
34
|
+
defineFunctionEventProperties: () => {},
|
|
35
|
+
defineFunctionProperties: () => {},
|
|
36
|
+
defineCustomProperties: () => {},
|
|
37
|
+
},
|
|
38
|
+
classes: {
|
|
39
|
+
Error: class ServerlessError {
|
|
40
|
+
constructor(err) {
|
|
41
|
+
return new Error(err);
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
processedInput: {
|
|
46
|
+
options: {
|
|
47
|
+
postmanCollection: "postman.json",
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
logOutput = {
|
|
53
|
+
log: {
|
|
54
|
+
notice: (str) => {},
|
|
55
|
+
error: (str) => {},
|
|
56
|
+
success: (str) => {},
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("generationAndValidation", () => {
|
|
62
|
+
it("should correctly generate a valid openAPI document", async function () {
|
|
63
|
+
const succSpy = sinon.spy(logOutput.log, "success");
|
|
64
|
+
const errSpy = sinon.spy(logOutput.log, "error");
|
|
65
|
+
|
|
66
|
+
Object.assign(sls.service, basicDocumentation);
|
|
67
|
+
const getAllFuncsStub = sinon
|
|
68
|
+
.stub(sls.service, "getAllFunctions")
|
|
69
|
+
.returns(["createUser"]);
|
|
70
|
+
|
|
71
|
+
const getFuncStub = sinon
|
|
72
|
+
.stub(sls.service, "getFunction")
|
|
73
|
+
.returns(basicValidFunction.createUser);
|
|
74
|
+
|
|
75
|
+
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
|
|
76
|
+
openAPIGenerator.processCliInput();
|
|
77
|
+
|
|
78
|
+
const validOpenAPIDocument = await openAPIGenerator
|
|
79
|
+
.generationAndValidation()
|
|
80
|
+
.catch((err) => {
|
|
81
|
+
expect(err).to.be.undefined;
|
|
81
82
|
});
|
|
82
83
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const errSpy = sinon.spy(logOutput.log, 'error')
|
|
84
|
+
expect(succSpy.called).to.be.true;
|
|
85
|
+
expect(errSpy.called).to.be.false;
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const getFuncStub = sinon.stub(sls.service, 'getFunction').returns(basicInvalidFunction.createUser)
|
|
93
|
-
|
|
94
|
-
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
|
|
95
|
-
openAPIGenerator.processCliInput()
|
|
96
|
-
|
|
97
|
-
const validOpenAPIDocument = await openAPIGenerator.generationAndValidation()
|
|
98
|
-
.catch(err => {
|
|
99
|
-
expect(err.message).to.be.equal('Error: createUser is missing a Response Model for statusCode 200')
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
expect(succSpy.called).to.be.false
|
|
103
|
-
expect(errSpy.called).to.be.true
|
|
87
|
+
succSpy.restore();
|
|
88
|
+
errSpy.restore();
|
|
89
|
+
getAllFuncsStub.reset();
|
|
90
|
+
getFuncStub.reset();
|
|
91
|
+
});
|
|
104
92
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
93
|
+
it("should throw an error when trying to generate an invalid openAPI document", async function () {
|
|
94
|
+
const succSpy = sinon.spy(logOutput.log, "success");
|
|
95
|
+
const errSpy = sinon.spy(logOutput.log, "error");
|
|
96
|
+
|
|
97
|
+
Object.assign(sls.service, basicDocumentation);
|
|
98
|
+
const getAllFuncsStub = sinon
|
|
99
|
+
.stub(sls.service, "getAllFunctions")
|
|
100
|
+
.returns(["createUser"]);
|
|
101
|
+
const basicInvalidFunction = JSON.parse(
|
|
102
|
+
JSON.stringify(basicValidFunction)
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
delete basicInvalidFunction.createUser.events[0].http.documentation
|
|
106
|
+
.methodResponses[0].responseModels;
|
|
107
|
+
const getFuncStub = sinon
|
|
108
|
+
.stub(sls.service, "getFunction")
|
|
109
|
+
.returns(basicInvalidFunction.createUser);
|
|
110
|
+
|
|
111
|
+
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
|
|
112
|
+
openAPIGenerator.processCliInput();
|
|
113
|
+
|
|
114
|
+
const validOpenAPIDocument = await openAPIGenerator
|
|
115
|
+
.generationAndValidation()
|
|
116
|
+
.catch((err) => {
|
|
117
|
+
expect(err.message).to.be.equal(
|
|
118
|
+
"Error: createUser is missing a Response Model for statusCode 200"
|
|
119
|
+
);
|
|
109
120
|
});
|
|
110
121
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const errSpy = sinon.spy(logOutput.log, 'error')
|
|
114
|
-
|
|
115
|
-
Object.assign(sls.service, basicDocumentation)
|
|
116
|
-
const getAllFuncsStub = sinon.stub(sls.service, 'getAllFunctions').returns(['createUser'])
|
|
117
|
-
const basicInvalidFunction = JSON.parse(JSON.stringify(basicValidFunction))
|
|
118
|
-
|
|
119
|
-
const getFuncStub = sinon.stub(sls.service, 'getFunction').returns(basicInvalidFunction.createUser)
|
|
122
|
+
expect(succSpy.called).to.be.false;
|
|
123
|
+
expect(errSpy.called).to.be.true;
|
|
120
124
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
expect(err).to.be.undefined
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
expect(succSpy.called).to.be.true
|
|
130
|
-
expect(errSpy.called).to.be.false
|
|
131
|
-
expect(validOpenAPIDocument).to.have.property('openapi')
|
|
125
|
+
succSpy.restore();
|
|
126
|
+
errSpy.restore();
|
|
127
|
+
getAllFuncsStub.reset();
|
|
128
|
+
getFuncStub.reset();
|
|
129
|
+
});
|
|
132
130
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
131
|
+
it("should correctly validate a valid openAPI document", async function () {
|
|
132
|
+
const succSpy = sinon.spy(logOutput.log, "success");
|
|
133
|
+
const errSpy = sinon.spy(logOutput.log, "error");
|
|
134
|
+
|
|
135
|
+
Object.assign(sls.service, basicDocumentation);
|
|
136
|
+
const getAllFuncsStub = sinon
|
|
137
|
+
.stub(sls.service, "getAllFunctions")
|
|
138
|
+
.returns(["createUser"]);
|
|
139
|
+
const basicInvalidFunction = JSON.parse(
|
|
140
|
+
JSON.stringify(basicValidFunction)
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const getFuncStub = sinon
|
|
144
|
+
.stub(sls.service, "getFunction")
|
|
145
|
+
.returns(basicInvalidFunction.createUser);
|
|
146
|
+
|
|
147
|
+
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
|
|
148
|
+
openAPIGenerator.processCliInput();
|
|
149
|
+
|
|
150
|
+
const validOpenAPIDocument = await openAPIGenerator
|
|
151
|
+
.generationAndValidation()
|
|
152
|
+
.catch((err) => {
|
|
153
|
+
expect(err).to.be.undefined;
|
|
137
154
|
});
|
|
138
155
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
Object.assign(sls.service, basicDocumentation)
|
|
144
|
-
const getAllFuncsStub = sinon.stub(sls.service, 'getAllFunctions').returns(['createUser'])
|
|
145
|
-
const basicInvalidFunction = JSON.parse(JSON.stringify(basicValidFunction))
|
|
156
|
+
expect(succSpy.called).to.be.true;
|
|
157
|
+
expect(errSpy.called).to.be.false;
|
|
158
|
+
expect(validOpenAPIDocument).to.have.property("openapi");
|
|
146
159
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
160
|
+
succSpy.restore();
|
|
161
|
+
errSpy.restore();
|
|
162
|
+
getAllFuncsStub.reset();
|
|
163
|
+
getFuncStub.reset();
|
|
164
|
+
});
|
|
152
165
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
166
|
+
it("should throw an error when trying to validate an invalid openAPI document", async function () {
|
|
167
|
+
const succSpy = sinon.spy(logOutput.log, "success");
|
|
168
|
+
const errSpy = sinon.spy(logOutput.log, "error");
|
|
169
|
+
|
|
170
|
+
Object.assign(sls.service, basicDocumentation);
|
|
171
|
+
const getAllFuncsStub = sinon
|
|
172
|
+
.stub(sls.service, "getAllFunctions")
|
|
173
|
+
.returns(["createUser"]);
|
|
174
|
+
const basicInvalidFunction = JSON.parse(
|
|
175
|
+
JSON.stringify(basicValidFunction)
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
delete basicInvalidFunction.createUser.events[0].http.documentation
|
|
179
|
+
.pathParams;
|
|
180
|
+
const getFuncStub = sinon
|
|
181
|
+
.stub(sls.service, "getFunction")
|
|
182
|
+
.returns(basicInvalidFunction.createUser);
|
|
183
|
+
|
|
184
|
+
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
|
|
185
|
+
openAPIGenerator.processCliInput();
|
|
186
|
+
|
|
187
|
+
const validOpenAPIDocument = await openAPIGenerator
|
|
188
|
+
.generationAndValidation()
|
|
189
|
+
.catch((err) => {
|
|
190
|
+
expect(err.message).to.be.equal(
|
|
191
|
+
"AssertionError: Templated parameter name not found"
|
|
192
|
+
);
|
|
193
|
+
});
|
|
157
194
|
|
|
158
|
-
|
|
159
|
-
|
|
195
|
+
expect(succSpy.called).to.be.false;
|
|
196
|
+
expect(errSpy.called).to.be.true;
|
|
160
197
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
198
|
+
succSpy.restore();
|
|
199
|
+
errSpy.restore();
|
|
200
|
+
getAllFuncsStub.reset();
|
|
201
|
+
getFuncStub.reset();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("createPostman", () => {
|
|
206
|
+
it("should generate a postman collection when a valid openAPI file is generated", function () {
|
|
207
|
+
const fsStub = sinon.stub(fs, "writeFileSync").returns(true);
|
|
208
|
+
const succSpy = sinon.spy(logOutput.log, "success");
|
|
209
|
+
const errSpy = sinon.spy(logOutput.log, "error");
|
|
210
|
+
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
|
|
211
|
+
openAPIGenerator.processCliInput();
|
|
212
|
+
|
|
213
|
+
openAPIGenerator.createPostman(validOpenAPI);
|
|
214
|
+
|
|
215
|
+
expect(fsStub.called).to.be.true;
|
|
216
|
+
expect(succSpy.calledTwice).to.be.true;
|
|
217
|
+
expect(errSpy.called).to.be.false;
|
|
218
|
+
fsStub.restore();
|
|
219
|
+
succSpy.restore();
|
|
220
|
+
errSpy.restore();
|
|
166
221
|
});
|
|
167
222
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const errStub = sinon.stub(logOutput.log, 'error').returns('')
|
|
188
|
-
const succSpy = sinon.spy(logOutput.log, 'success')
|
|
189
|
-
const fsStub = sinon.stub(fs, 'writeFileSync').throws(new Error())
|
|
190
|
-
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
|
|
191
|
-
openAPIGenerator.processCliInput()
|
|
192
|
-
|
|
193
|
-
expect(() => {openAPIGenerator.createPostman(validOpenAPI)}).to.throw()
|
|
194
|
-
|
|
195
|
-
expect(fsStub.called).to.be.true
|
|
196
|
-
expect(errStub.called).to.be.true
|
|
197
|
-
expect(succSpy.calledOnce).to.be.true
|
|
198
|
-
expect(succSpy.calledTwice).to.be.false
|
|
199
|
-
fsStub.restore()
|
|
200
|
-
succSpy.restore()
|
|
201
|
-
errStub.restore()
|
|
202
|
-
});
|
|
223
|
+
it("should throw an error when writing a file fails", function () {
|
|
224
|
+
const errStub = sinon.stub(logOutput.log, "error").returns("");
|
|
225
|
+
const succSpy = sinon.spy(logOutput.log, "success");
|
|
226
|
+
const fsStub = sinon.stub(fs, "writeFileSync").throws(new Error());
|
|
227
|
+
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
|
|
228
|
+
openAPIGenerator.processCliInput();
|
|
229
|
+
|
|
230
|
+
expect(() => {
|
|
231
|
+
openAPIGenerator.createPostman(validOpenAPI);
|
|
232
|
+
}).to.throw();
|
|
233
|
+
|
|
234
|
+
expect(fsStub.called).to.be.true;
|
|
235
|
+
expect(errStub.called).to.be.true;
|
|
236
|
+
expect(succSpy.calledOnce).to.be.true;
|
|
237
|
+
expect(succSpy.calledTwice).to.be.false;
|
|
238
|
+
fsStub.restore();
|
|
239
|
+
succSpy.restore();
|
|
240
|
+
errStub.restore();
|
|
241
|
+
});
|
|
203
242
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
243
|
+
it("should throw an error converting an OpenAPI fails", function () {
|
|
244
|
+
const errStub = sinon.spy(logOutput.log, "error");
|
|
245
|
+
const succSpy = sinon.spy(logOutput.log, "success");
|
|
246
|
+
const pgStub = sinon.stub(PostmanGenerator, "convert");
|
|
247
|
+
pgStub.yields(new Error());
|
|
209
248
|
|
|
210
|
-
|
|
211
|
-
|
|
249
|
+
const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
|
|
250
|
+
openAPIGenerator.processCliInput();
|
|
212
251
|
|
|
213
|
-
|
|
252
|
+
expect(() => {
|
|
253
|
+
openAPIGenerator.createPostman(validOpenAPI);
|
|
254
|
+
}).to.throw();
|
|
214
255
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
256
|
+
expect(errStub.called).to.be.true;
|
|
257
|
+
expect(succSpy.calledOnce).to.be.false;
|
|
258
|
+
expect(succSpy.calledTwice).to.be.false;
|
|
218
259
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
});
|
|
260
|
+
succSpy.restore();
|
|
261
|
+
errStub.restore();
|
|
222
262
|
});
|
|
263
|
+
});
|
|
223
264
|
});
|