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

Skip all Documentation Parts overwriting in case of shared API Gateway usage. #121

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ module.exports = function() {
}
});
},

_isSharedApi: function _isSharedApi(){
return this.serverless.service.provider.apiGateway ? !!this.serverless.service.provider.apiGateway.restApiId : false;
},

_updateDocumentation: function _updateDocumentation() {
const serverlessServicePropertyKey = 'serverless-service';
const aws = this.serverless.providers.aws;
return aws.request('APIGateway', 'getDocumentationVersion', {
restApiId: this.restApiId,
Expand All @@ -116,7 +121,13 @@ module.exports = function() {
limit: 9999,
})
)
.then(results => results.items.map(
.then(results => results.items.filter(part => {
if (this._isSharedApi()){
return JSON.parse(part.properties)[serverlessServicePropertyKey] === this.serverless.service.service;
}
return true;
})
.map(
part => aws.request('APIGateway', 'deleteDocumentationPart', {
documentationPartId: part.id,
restApiId: this.restApiId,
Expand All @@ -125,6 +136,9 @@ module.exports = function() {
.then(promises => Promise.all(promises))
.then(() => this.documentationParts.reduce((promise, part) => {
return promise.then(() => {
if (this._isSharedApi()) {
part.properties[serverlessServicePropertyKey] = this.serverless.service.service;
}
part.properties = JSON.stringify(part.properties);
return aws.request('APIGateway', 'createDocumentationPart', part);
});
Expand Down
42 changes: 42 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3313,6 +3313,48 @@ describe('ServerlessAWSDocumentation', function () {
});
});

it('should not delete another documentation parts in case of shared API usage', function (done) {
this.serverlessMock.service.provider.apiGateway = {
restApiId: 'superid',
};

this.serverlessMock.providers.aws.request.and.callFake((api, method) => {
switch (method) {
case 'describeStacks':
return Promise.resolve({
Stacks: [{
Outputs: [{
OutputKey: 'ApiId',
OutputValue: 'superid',
}],
}],
});
case 'getDocumentationParts':
return Promise.resolve({
items: [{
id: '123',
properties: JSON.stringify({ 'serverless-service': 'service1' })
}, {
id: '456',
properties: JSON.stringify({ 'serverless-service': 'service2' })
}],
});
case 'getDocumentationVersion':
return Promise.reject(new Error('Invalid Documentation version specified'));
case 'deleteDocumentationPart':
return Promise.reject();
default:
return Promise.resolve();
}
});
this.serverlessMock.providers.aws.naming.getStackName.and.returnValue('superstack');

this.plugin.afterDeploy().then(() => {
expect(this.serverlessMock.providers.aws.request).not.toHaveBeenCalledWith('APIGateway', 'deleteDocumentationPart', jasmine.any(Object));
done();
});
});

it('should generate different documentation versions for different documentation content', function() {
this.serverlessMock.variables.service.custom.documentation.api = {
description: 'this is an api',
Expand Down