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

Commit 1c68e0d

Browse files
dspasojevictchock
authored andcommitted
Use provider REST API ID (#78)
* Honour provider API Gateway Rest API ID. * Remove console debugging. * Add unit tests for shared API Gateway suppport. * Simplify tests for shared API Gateway.
1 parent 076db05 commit 1c68e0d

File tree

4 files changed

+182
-20
lines changed

4 files changed

+182
-20
lines changed

src/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,21 @@ class ServerlessAWSDocumentation {
3232

3333
this.cfTemplate = this.serverless.service.provider.compiledCloudFormationTemplate;
3434

35+
// The default rest API reference
36+
let restApiId = {
37+
Ref: 'ApiGatewayRestApi',
38+
};
39+
40+
// Use the provider API gateway if one has been provided.
41+
if (this.serverless.service.provider.apiGateway && this.serverless.service.provider.apiGateway.restApiId) {
42+
restApiId = this.serverless.service.provider.apiGateway.restApiId
43+
}
44+
3545
if (this.customVars.documentation.models) {
46+
const cfModelCreator = this.createCfModel(restApiId);
47+
3648
// Add model resources
37-
const models = this.customVars.documentation.models.map(this.createCfModel)
49+
const models = this.customVars.documentation.models.map(cfModelCreator)
3850
.reduce((modelObj, model) => {
3951
modelObj[`${model.Properties.Name}Model`] = model;
4052
return modelObj;
@@ -51,9 +63,7 @@ class ServerlessAWSDocumentation {
5163
// Add models
5264
this.cfTemplate.Outputs.AwsDocApiId = {
5365
Description: 'API ID',
54-
Value: {
55-
Ref: 'ApiGatewayRestApi',
56-
},
66+
Value: restApiId,
5767
};
5868
}
5969

src/index.spec.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,100 @@ describe('ServerlessAWSDocumentation', function () {
570570
});
571571
});
572572

573+
it('should use the provider rest api id', function () {
574+
this.serverlessMock.variables.service.custom.documentation.models = [{
575+
name: 'CreateResponseJson',
576+
contentType: "application/json",
577+
schema: {
578+
type: 'object'
579+
}
580+
}];
581+
this.serverlessMock.service._functionNames = ['test'];
582+
this.serverlessMock.service._functions = {
583+
test: {
584+
events: [{
585+
http: {
586+
path: 'some/path',
587+
method: 'post',
588+
cors: true,
589+
private: true,
590+
documentation: {
591+
methodResponses: [
592+
{
593+
statusCode: 200,
594+
responseModels: {
595+
'application/json': 'CreateResponseJson',
596+
},
597+
responseHeaders: [{
598+
name: 'x-header',
599+
description: 'THE header',
600+
}],
601+
},
602+
],
603+
}
604+
},
605+
}],
606+
},
607+
};
608+
this.serverlessMock.service.provider.apiGateway = {
609+
restApiId: {
610+
'Fn::ImportValue': 'PublicApiGatewayRestApi'
611+
}
612+
};
613+
614+
const resources = this.serverlessMock.service.provider.compiledCloudFormationTemplate.Resources;
615+
resources.somepath_post = {
616+
some: 'configuration',
617+
Properties: {},
618+
};
619+
620+
this.plugin.beforeDeploy();
621+
622+
expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
623+
Resources: {
624+
ExistingResource: {
625+
with: 'configuration',
626+
},
627+
somepath_post: {
628+
some: 'configuration',
629+
DependsOn: ['CreateResponseJsonModel'],
630+
Properties: {
631+
MethodResponses: [{
632+
StatusCode: '200',
633+
ResponseModels: {
634+
'application/json': 'CreateResponseJson',
635+
},
636+
ResponseParameters: {
637+
'method.response.header.x-header': true,
638+
},
639+
}],
640+
},
641+
},
642+
CreateResponseJsonModel: {
643+
Type: 'AWS::ApiGateway::Model',
644+
Properties: {
645+
RestApiId: {
646+
'Fn::ImportValue': 'PublicApiGatewayRestApi'
647+
},
648+
ContentType: 'application/json',
649+
Name: 'CreateResponseJson',
650+
Schema: {
651+
type: 'object'
652+
}
653+
}
654+
},
655+
},
656+
Outputs: {
657+
AwsDocApiId: {
658+
Description: 'API ID',
659+
Value: {
660+
'Fn::ImportValue': 'PublicApiGatewayRestApi',
661+
},
662+
}
663+
},
664+
});
665+
});
666+
573667
it('should only add response methods with existing MethodResponses to ApiGateway methods', function () {
574668
this.serverlessMock.variables.service.custom.documentation.models = [];
575669
this.serverlessMock.service._functionNames = ['test'];

src/models.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
function replaceModelRefs(cfModel) {
3+
function replaceModelRefs(restApiId, cfModel) {
44
if (!cfModel.Properties || !cfModel.Properties.Schema || Object.keys(cfModel.Properties.Schema).length == 0) {
55
return cfModel;
66
}
@@ -15,7 +15,7 @@ function replaceModelRefs(cfModel) {
1515
'/',
1616
[
1717
'https://apigateway.amazonaws.com/restapis',
18-
{ Ref: 'ApiGatewayRestApi' },
18+
restApiId,
1919
'models',
2020
match[1]
2121
]
@@ -40,18 +40,20 @@ function replaceModelRefs(cfModel) {
4040
}
4141

4242
module.exports = {
43-
createCfModel: function createCfModel(model) {
44-
return replaceModelRefs({
45-
Type: 'AWS::ApiGateway::Model',
46-
Properties: {
47-
RestApiId: {
48-
Ref: 'ApiGatewayRestApi',
49-
},
50-
ContentType: model.contentType,
51-
Name: model.name,
52-
Schema: model.schema || {},
53-
},
54-
});
43+
createCfModel: function createCfModel(restApiId) {
44+
return function(model) {
45+
return replaceModelRefs(restApiId,
46+
{
47+
Type: 'AWS::ApiGateway::Model',
48+
Properties: {
49+
RestApiId: restApiId,
50+
ContentType: model.contentType,
51+
Name: model.name,
52+
Schema: model.schema || {},
53+
},
54+
}
55+
);
56+
}
5557
},
5658

5759
addModelDependencies: function addModelDependencies(models, resource) {

src/models.spec.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ describe('ServerlessAWSDocumentation', function() {
1616
}
1717
};
1818

19-
let modelOutput = objectUnderTest.createCfModel(modelInput);
19+
let modelOutput = objectUnderTest.createCfModel({
20+
Ref: 'ApiGatewayRestApi',
21+
})(modelInput);
2022
expect(modelOutput).toEqual({
2123
Type: 'AWS::ApiGateway::Model',
2224
Properties: {
@@ -52,6 +54,58 @@ describe('ServerlessAWSDocumentation', function() {
5254
});
5355
});
5456

57+
it('should use provided rest api setting', () => {
58+
let modelInput = {
59+
contentType: 'application/json',
60+
name: 'TestModel',
61+
schema: {
62+
type: 'object',
63+
properties: {
64+
prop: {
65+
'$ref': '{{model: OtherModelName}}'
66+
}
67+
}
68+
}
69+
};
70+
71+
let modelOutput = objectUnderTest.createCfModel({
72+
'Fn::ImportValue': 'PublicApiGatewayRestApi',
73+
})(modelInput);
74+
expect(modelOutput).toEqual({
75+
Type: 'AWS::ApiGateway::Model',
76+
Properties: {
77+
RestApiId: {
78+
'Fn::ImportValue': 'PublicApiGatewayRestApi',
79+
},
80+
ContentType: 'application/json',
81+
Name: 'TestModel',
82+
Schema: {
83+
type: 'object',
84+
properties: {
85+
prop: {
86+
'$ref': {
87+
'Fn::Join': [
88+
'/',
89+
[
90+
'https://apigateway.amazonaws.com/restapis',
91+
{
92+
'Fn::ImportValue': 'PublicApiGatewayRestApi'
93+
},
94+
'models',
95+
'OtherModelName'
96+
]
97+
]
98+
}
99+
}
100+
}
101+
}
102+
},
103+
DependsOn: [
104+
'OtherModelNameModel'
105+
]
106+
});
107+
});
108+
55109
it('should not mess with non-ref model definitions', () => {
56110
let modelInput = {
57111
contentType: 'application/json',
@@ -66,7 +120,9 @@ describe('ServerlessAWSDocumentation', function() {
66120
}
67121
};
68122

69-
let modelOutput = objectUnderTest.createCfModel(modelInput);
123+
let modelOutput = objectUnderTest.createCfModel({
124+
Ref: 'ApiGatewayRestApi',
125+
})(modelInput);
70126
expect(modelOutput).toEqual({
71127
Type: 'AWS::ApiGateway::Model',
72128
Properties: {

0 commit comments

Comments
 (0)