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

Commit 712549d

Browse files
authored
fix: solve problem with potentially existing method responses and new ones (#18)
fixes problem that caused method responses to be empty when there already was an empty method response (regression in 0.5.6). fixes #17
1 parent 58a14fc commit 712549d

File tree

3 files changed

+181
-19
lines changed

3 files changed

+181
-19
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "serverless-aws-documentation",
3-
"version": "0.5.6",
3+
"version": "0.5.7",
44
"description": "Serverless 1.0 plugin to add documentation and models to the serverless generated API Gateway",
55
"main": "src/index.js",
66
"scripts": {
77
"codecov": "cat coverage/*/lcov.info | codecov",
8-
"test": "istanbul cover -x \"src/index.spec.js\" jasmine ./src/index.spec.js"
8+
"test": "istanbul cover -x \"src/index.spec.js\" jasmine ./src/index.spec.js",
9+
"test:nocoverage": "jasmine ./src/index.spec.js"
910
},
1011
"repository": "9cookies/serverless-aws-documentation",
1112
"author": "Simon Jentsch <[email protected]> (https://twitter.com/tchockie)",

src/index.spec.js

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ describe('ServerlessAWSDocumentation', function () {
305305
});
306306
});
307307

308-
it('should only add response methods whith existence MethodResponses to ApiGateway methods', function () {
308+
it('should only add response methods with existing MethodResponses to ApiGateway methods', function () {
309309
this.serverlessMock.variables.service.custom.documentation.models = [];
310310
this.serverlessMock.service._functionNames = ['test'];
311311
this.serverlessMock.service._functions = {
@@ -343,15 +343,179 @@ describe('ServerlessAWSDocumentation', function () {
343343
Properties: {
344344
MethodResponses: [{
345345
StatusCode: 200,
346+
id: 9001,
346347
},
347348
{
348349
StatusCode: 404,
350+
id: 9002,
349351
}],
350352
},
351353
};
352354

353355
this.plugin.beforeDeploy();
354356

357+
expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
358+
Resources: {
359+
ExistingResource: {
360+
with: 'configuration',
361+
},
362+
somepath_post: {
363+
some: 'configuration',
364+
DependsOn: ['CreateResponseModel', 'ErrorResponseModel'],
365+
Properties: {
366+
MethodResponses: [{
367+
StatusCode: 200,
368+
id: 9001,
369+
ResponseModels: {
370+
'application/json': 'CreateResponse',
371+
},
372+
},
373+
{
374+
StatusCode: 404,
375+
id: 9002,
376+
ResponseModels: {
377+
'application/json': 'ErrorResponse'
378+
},
379+
}],
380+
},
381+
},
382+
},
383+
Outputs: {
384+
AwsDocApiId: {
385+
Description: 'API ID',
386+
Value: {
387+
Ref: 'ApiGatewayRestApi',
388+
},
389+
}
390+
},
391+
});
392+
});
393+
394+
it('should only add response methods with existing and new MethodResponses to ApiGateway methods', function () {
395+
this.serverlessMock.variables.service.custom.documentation.models = [];
396+
this.serverlessMock.service._functionNames = ['test'];
397+
this.serverlessMock.service._functions = {
398+
test: {
399+
events: [{
400+
http: {
401+
path: 'some/path',
402+
method: 'post',
403+
cors: true,
404+
private: true,
405+
documentation: {
406+
methodResponses: [
407+
{
408+
statusCode: 200,
409+
should: 'not be included',
410+
responseModels: {
411+
'application/json': 'CreateResponse',
412+
},
413+
},
414+
{
415+
statusCode: 404,
416+
should: 'not be included',
417+
responseModels: {
418+
'application/json': 'ErrorResponse'
419+
},
420+
},
421+
],
422+
}
423+
},
424+
}],
425+
},
426+
};
427+
428+
const resources = this.serverlessMock.service.provider.compiledCloudFormationTemplate.Resources;
429+
resources.somepath_post = {
430+
some: 'configuration',
431+
Properties: {
432+
MethodResponses: [{
433+
StatusCode: 200,
434+
id: 9001,
435+
},],
436+
},
437+
};
438+
439+
this.plugin.beforeDeploy();
440+
441+
expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
442+
Resources: {
443+
ExistingResource: {
444+
with: 'configuration',
445+
},
446+
somepath_post: {
447+
some: 'configuration',
448+
DependsOn: ['CreateResponseModel', 'ErrorResponseModel'],
449+
Properties: {
450+
MethodResponses: [{
451+
StatusCode: 200,
452+
id: 9001,
453+
ResponseModels: {
454+
'application/json': 'CreateResponse',
455+
},
456+
},
457+
{
458+
StatusCode: 404,
459+
ResponseModels: {
460+
'application/json': 'ErrorResponse'
461+
},
462+
}],
463+
},
464+
},
465+
},
466+
Outputs: {
467+
AwsDocApiId: {
468+
Description: 'API ID',
469+
Value: {
470+
Ref: 'ApiGatewayRestApi',
471+
},
472+
}
473+
},
474+
});
475+
});
476+
477+
it('should only add response methods with existing empty MethodResponses to ApiGateway methods', function () {
478+
this.serverlessMock.variables.service.custom.documentation.models = [];
479+
this.serverlessMock.service._functionNames = ['test'];
480+
this.serverlessMock.service._functions = {
481+
test: {
482+
events: [{
483+
http: {
484+
path: 'some/path',
485+
method: 'post',
486+
cors: true,
487+
private: true,
488+
documentation: {
489+
methodResponses: [
490+
{
491+
statusCode: 200,
492+
responseModels: {
493+
'application/json': 'CreateResponse',
494+
},
495+
},
496+
{
497+
statusCode: 404,
498+
responseModels: {
499+
'application/json': 'ErrorResponse'
500+
},
501+
},
502+
],
503+
}
504+
},
505+
}],
506+
},
507+
};
508+
509+
const resources = this.serverlessMock.service.provider.compiledCloudFormationTemplate.Resources;
510+
resources.somepath_post = {
511+
some: 'configuration',
512+
Properties: {
513+
MethodResponses: [],
514+
},
515+
};
516+
517+
this.plugin.beforeDeploy();
518+
355519
expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
356520
Resources: {
357521
ExistingResource: {

src/models.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,17 @@ module.exports = {
2323

2424
addMethodResponses: function addMethodResponses(resource, documentation) {
2525
if (documentation.methodResponses) {
26-
if (resource.Properties.MethodResponses) {
27-
resource.Properties.MethodResponses.forEach(originalResponse => {
28-
documentation.methodResponses.forEach(response => {
29-
if (originalResponse.StatusCode === response.statusCode) {
30-
originalResponse.ResponseModels = response.responseModels;
31-
this.addModelDependencies(originalResponse.ResponseModels, resource);
32-
}
33-
});
34-
});
35-
} else {
26+
if (!resource.Properties.MethodResponses) {
3627
resource.Properties.MethodResponses = [];
28+
}
29+
30+
documentation.methodResponses.forEach(response => {
31+
let _response = resource.Properties.MethodResponses
32+
.find(originalResponse => originalResponse.StatusCode === response.statusCode);
3733

38-
documentation.methodResponses.forEach(response => {
39-
const _response = {
34+
if (!_response) {
35+
_response = {
4036
StatusCode: response.statusCode,
41-
ResponseModels: response.responseModels
4237
};
4338

4439
if (response.responseHeaders) {
@@ -49,10 +44,12 @@ module.exports = {
4944
_response.ResponseParameters = methodResponseHeaders;
5045
}
5146

52-
this.addModelDependencies(_response.ResponseModels, resource);
5347
resource.Properties.MethodResponses.push(_response);
54-
});
55-
}
48+
}
49+
50+
_response.ResponseModels = response.responseModels;
51+
this.addModelDependencies(_response.ResponseModels, resource);
52+
});
5653
}
5754
},
5855

0 commit comments

Comments
 (0)