diff --git a/src/documentation.js b/src/documentation.js index 4862832..bae8081 100644 --- a/src/documentation.js +++ b/src/documentation.js @@ -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, @@ -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, @@ -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); }); diff --git a/src/index.spec.js b/src/index.spec.js index 0118ad3..85745ca 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -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',