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

Commit

Permalink
fix: solve problem with potentially existing method responses and new…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
tchock committed Feb 12, 2017
1 parent 58a14fc commit 712549d
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 19 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "serverless-aws-documentation",
"version": "0.5.6",
"version": "0.5.7",
"description": "Serverless 1.0 plugin to add documentation and models to the serverless generated API Gateway",
"main": "src/index.js",
"scripts": {
"codecov": "cat coverage/*/lcov.info | codecov",
"test": "istanbul cover -x \"src/index.spec.js\" jasmine ./src/index.spec.js"
"test": "istanbul cover -x \"src/index.spec.js\" jasmine ./src/index.spec.js",
"test:nocoverage": "jasmine ./src/index.spec.js"
},
"repository": "9cookies/serverless-aws-documentation",
"author": "Simon Jentsch <[email protected]> (https://twitter.com/tchockie)",
Expand Down
166 changes: 165 additions & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ describe('ServerlessAWSDocumentation', function () {
});
});

it('should only add response methods whith existence MethodResponses to ApiGateway methods', function () {
it('should only add response methods with existing MethodResponses to ApiGateway methods', function () {
this.serverlessMock.variables.service.custom.documentation.models = [];
this.serverlessMock.service._functionNames = ['test'];
this.serverlessMock.service._functions = {
Expand Down Expand Up @@ -343,15 +343,179 @@ describe('ServerlessAWSDocumentation', function () {
Properties: {
MethodResponses: [{
StatusCode: 200,
id: 9001,
},
{
StatusCode: 404,
id: 9002,
}],
},
};

this.plugin.beforeDeploy();

expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
Resources: {
ExistingResource: {
with: 'configuration',
},
somepath_post: {
some: 'configuration',
DependsOn: ['CreateResponseModel', 'ErrorResponseModel'],
Properties: {
MethodResponses: [{
StatusCode: 200,
id: 9001,
ResponseModels: {
'application/json': 'CreateResponse',
},
},
{
StatusCode: 404,
id: 9002,
ResponseModels: {
'application/json': 'ErrorResponse'
},
}],
},
},
},
Outputs: {
AwsDocApiId: {
Description: 'API ID',
Value: {
Ref: 'ApiGatewayRestApi',
},
}
},
});
});

it('should only add response methods with existing and new MethodResponses to ApiGateway methods', function () {
this.serverlessMock.variables.service.custom.documentation.models = [];
this.serverlessMock.service._functionNames = ['test'];
this.serverlessMock.service._functions = {
test: {
events: [{
http: {
path: 'some/path',
method: 'post',
cors: true,
private: true,
documentation: {
methodResponses: [
{
statusCode: 200,
should: 'not be included',
responseModels: {
'application/json': 'CreateResponse',
},
},
{
statusCode: 404,
should: 'not be included',
responseModels: {
'application/json': 'ErrorResponse'
},
},
],
}
},
}],
},
};

const resources = this.serverlessMock.service.provider.compiledCloudFormationTemplate.Resources;
resources.somepath_post = {
some: 'configuration',
Properties: {
MethodResponses: [{
StatusCode: 200,
id: 9001,
},],
},
};

this.plugin.beforeDeploy();

expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
Resources: {
ExistingResource: {
with: 'configuration',
},
somepath_post: {
some: 'configuration',
DependsOn: ['CreateResponseModel', 'ErrorResponseModel'],
Properties: {
MethodResponses: [{
StatusCode: 200,
id: 9001,
ResponseModels: {
'application/json': 'CreateResponse',
},
},
{
StatusCode: 404,
ResponseModels: {
'application/json': 'ErrorResponse'
},
}],
},
},
},
Outputs: {
AwsDocApiId: {
Description: 'API ID',
Value: {
Ref: 'ApiGatewayRestApi',
},
}
},
});
});

it('should only add response methods with existing empty MethodResponses to ApiGateway methods', function () {
this.serverlessMock.variables.service.custom.documentation.models = [];
this.serverlessMock.service._functionNames = ['test'];
this.serverlessMock.service._functions = {
test: {
events: [{
http: {
path: 'some/path',
method: 'post',
cors: true,
private: true,
documentation: {
methodResponses: [
{
statusCode: 200,
responseModels: {
'application/json': 'CreateResponse',
},
},
{
statusCode: 404,
responseModels: {
'application/json': 'ErrorResponse'
},
},
],
}
},
}],
},
};

const resources = this.serverlessMock.service.provider.compiledCloudFormationTemplate.Resources;
resources.somepath_post = {
some: 'configuration',
Properties: {
MethodResponses: [],
},
};

this.plugin.beforeDeploy();

expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
Resources: {
ExistingResource: {
Expand Down
29 changes: 13 additions & 16 deletions src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,17 @@ module.exports = {

addMethodResponses: function addMethodResponses(resource, documentation) {
if (documentation.methodResponses) {
if (resource.Properties.MethodResponses) {
resource.Properties.MethodResponses.forEach(originalResponse => {
documentation.methodResponses.forEach(response => {
if (originalResponse.StatusCode === response.statusCode) {
originalResponse.ResponseModels = response.responseModels;
this.addModelDependencies(originalResponse.ResponseModels, resource);
}
});
});
} else {
if (!resource.Properties.MethodResponses) {
resource.Properties.MethodResponses = [];
}

documentation.methodResponses.forEach(response => {
let _response = resource.Properties.MethodResponses
.find(originalResponse => originalResponse.StatusCode === response.statusCode);

documentation.methodResponses.forEach(response => {
const _response = {
if (!_response) {
_response = {
StatusCode: response.statusCode,
ResponseModels: response.responseModels
};

if (response.responseHeaders) {
Expand All @@ -49,10 +44,12 @@ module.exports = {
_response.ResponseParameters = methodResponseHeaders;
}

this.addModelDependencies(_response.ResponseModels, resource);
resource.Properties.MethodResponses.push(_response);
});
}
}

_response.ResponseModels = response.responseModels;
this.addModelDependencies(_response.ResponseModels, resource);
});
}
},

Expand Down

0 comments on commit 712549d

Please sign in to comment.