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

Commit ddd4811

Browse files
shevchenkostchock
authored andcommitted
fix: fixed problem with missing response headers (#12)
adds response headers to method responses as response parameters
1 parent 5137dfb commit ddd4811

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-aws-documentation",
3-
"version": "0.5.4",
3+
"version": "0.5.5",
44
"description": "Serverless 1.0 plugin to add documentation and models to the serverless generated API Gateway",
55
"main": "src/index.js",
66
"scripts": {

src/index.spec.js

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

308+
it('should only add response methods with response headers to ApiGateway methods', function () {
309+
this.serverlessMock.variables.service.custom.documentation.models = [];
310+
this.serverlessMock.service._functionNames = ['test', 'blub'];
311+
this.serverlessMock.service._functions = {
312+
test: {
313+
events: [{
314+
http: {
315+
path: 'some/path',
316+
method: 'post',
317+
cors: true,
318+
private: true,
319+
documentation: {
320+
methodResponses: [
321+
{
322+
statusCode: 200,
323+
responseModels: {
324+
'application/json': 'CreateResponse',
325+
},
326+
responseHeaders: [{
327+
name: 'x-header',
328+
description: 'THE header',
329+
}, {
330+
name: 'x-other-header',
331+
description: 'THE other header',
332+
}],
333+
},
334+
{
335+
statusCode: 400,
336+
responseModels: {
337+
'application/json': 'ErrorResponse'
338+
},
339+
responseHeaders: [{
340+
name: 'x-header',
341+
description: 'THE header',
342+
}],
343+
},
344+
{
345+
statusCode: 404,
346+
responseModels: {
347+
'application/json': 'ErrorResponse'
348+
},
349+
responseHeaders: [{
350+
name: 'x-header',
351+
description: 'THE header',
352+
}],
353+
},
354+
],
355+
}
356+
},
357+
}],
358+
},
359+
blub: {
360+
events: [{
361+
http: {
362+
path: 'some/other/path',
363+
method: 'get',
364+
cors: true,
365+
private: true,
366+
documentation: {
367+
methodResponses: [
368+
{
369+
statusCode: 204,
370+
responseModels: {
371+
'application/json': 'CrazyResponse',
372+
},
373+
responseHeaders: [{
374+
name: 'x-header',
375+
description: 'THE header',
376+
}, {
377+
name: 'x-other-header',
378+
description: 'THE other header',
379+
}],
380+
},
381+
],
382+
},
383+
},
384+
}],
385+
},
386+
};
387+
388+
const resources = this.serverlessMock.service.provider.compiledCloudFormationTemplate.Resources;
389+
resources.someotherpath_get = {
390+
some: 'other_configuration',
391+
Properties: {},
392+
};
393+
resources.somepath_post = {
394+
some: 'configuration',
395+
Properties: {},
396+
};
397+
398+
this.plugin.beforeDeploy();
399+
400+
expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
401+
Resources: {
402+
ExistingResource: {
403+
with: 'configuration',
404+
},
405+
somepath_post: {
406+
some: 'configuration',
407+
DependsOn: ['CreateResponseModel', 'ErrorResponseModel'],
408+
Properties: {
409+
MethodResponses: [{
410+
StatusCode: 200,
411+
ResponseModels: {
412+
'application/json': 'CreateResponse',
413+
},
414+
ResponseParameters: {
415+
'method.response.header.x-header': true,
416+
'method.response.header.x-other-header': true,
417+
},
418+
},
419+
{
420+
StatusCode: 400,
421+
ResponseModels: {
422+
'application/json': 'ErrorResponse'
423+
},
424+
ResponseParameters: {
425+
'method.response.header.x-header': true,
426+
},
427+
},
428+
{
429+
StatusCode: 404,
430+
ResponseModels: {
431+
'application/json': 'ErrorResponse'
432+
},
433+
ResponseParameters: {
434+
'method.response.header.x-header': true,
435+
},
436+
}],
437+
},
438+
},
439+
someotherpath_get: {
440+
some: 'other_configuration',
441+
DependsOn: ['CrazyResponseModel'],
442+
Properties: {
443+
MethodResponses: [{
444+
StatusCode: 204,
445+
ResponseModels: {
446+
'application/json': 'CrazyResponse',
447+
},
448+
ResponseParameters: {
449+
'method.response.header.x-header': true,
450+
'method.response.header.x-other-header': true,
451+
},
452+
}],
453+
}
454+
},
455+
},
456+
Outputs: {
457+
AwsDocApiId: {
458+
Description: 'API ID',
459+
Value: {
460+
Ref: 'ApiGatewayRestApi',
461+
},
462+
}
463+
},
464+
});
465+
});
466+
308467
it('should only add request models to ApiGateway methods', function () {
309468
this.serverlessMock.variables.service.custom.documentation.models = [];
310469
this.serverlessMock.service._functionNames = ['test', 'blub'];

src/models.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ module.exports = {
3131
ResponseModels: response.responseModels,
3232
};
3333

34+
if(response.responseHeaders){
35+
const methodResponseHeaders = {};
36+
response.responseHeaders.forEach(header => {
37+
methodResponseHeaders[`method.response.header.${header.name}`] = true
38+
});
39+
_response.ResponseParameters = methodResponseHeaders;
40+
}
41+
3442
this.addModelDependencies(_response.ResponseModels, resource);
3543
resource.Properties.MethodResponses.push(_response);
3644
});

0 commit comments

Comments
 (0)