From 62a46a0344616c899f13dbe3b89eacb4b122d1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 16 Apr 2018 14:41:32 +0200 Subject: [PATCH] feat: rebuild swagger after a model was deleted --- index.js | 18 +++++++++--------- package.json | 2 +- test/explorer.test.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 49e4002..312593e 100644 --- a/index.js +++ b/index.js @@ -119,22 +119,18 @@ function mountSwagger(loopbackApplication, swaggerApp, opts) { // listening to modelRemoted event for updating the swaggerObject // with the newly created model to appear in the Swagger UI. - loopbackApplication.on('modelRemoted', function() { - swaggerObject = createSwaggerObject(loopbackApplication, opts); - }); + loopbackApplication.on('modelRemoted', rebuildSwaggerObject); + + loopbackApplication.on('modelDeleted', rebuildSwaggerObject); // listening to started event for updating the swaggerObject // when a call to app.models.[modelName].nestRemoting([modelName]) // to appear that method in the Swagger UI. - loopbackApplication.on('remoteMethodAdded', function() { - swaggerObject = createSwaggerObject(loopbackApplication, opts); - }); + loopbackApplication.on('remoteMethodAdded', rebuildSwaggerObject); // listening to remoteMethodDisabled event for updating the swaggerObject // when a remote method is disabled to hide that method in the Swagger UI. - loopbackApplication.on('remoteMethodDisabled', function() { - swaggerObject = createSwaggerObject(loopbackApplication, opts); - }); + loopbackApplication.on('remoteMethodDisabled', rebuildSwaggerObject); var resourcePath = (opts && opts.resourcePath) || 'swagger.json'; if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath; @@ -145,6 +141,10 @@ function mountSwagger(loopbackApplication, swaggerApp, opts) { swaggerApp.get(resourcePath, function sendSwaggerObject(req, res) { res.status(200).send(swaggerObject); }); + + function rebuildSwaggerObject() { + swaggerObject = createSwaggerObject(loopbackApplication, opts); + } } function setupCors(swaggerApp, remotes) { diff --git a/package.json b/package.json index 2386144..0591832 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "chai": "^3.2.0", "eslint": "^2.8.0", "eslint-config-loopback": "^2.0.0", - "loopback": "^3.0.0", + "loopback": "^3.19.0", "mocha": "^2.2.5", "supertest": "^1.0.1" }, diff --git a/test/explorer.test.js b/test/explorer.test.js index a3e0db4..ac9bc66 100644 --- a/test/explorer.test.js +++ b/test/explorer.test.js @@ -325,6 +325,41 @@ describe('explorer', function() { }); }); + it('updates swagger object when a model is removed', function(done) { + var app = loopback(); + app.set('remoting', { cors: false }); + configureRestApiAndExplorer(app, '/explorer'); + + var Model = loopback.PersistedModel.extend('Customer'); + Model.attachTo(loopback.memory()); + app.model(Model); + + // Ensure the swagger object was built + request(app) + .get('/explorer/swagger.json') + .expect(200) + .end(function(err) { + if (err) return done(err); + + app.deleteModelByName('Customer'); + + // Request swagger.json again + request(app) + .get('/explorer/swagger.json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + var modelNames = Object.keys(res.body.definitions); + expect(modelNames).to.not.contain('Customer'); + var paths = Object.keys(res.body.paths); + expect(paths).to.not.contain('/Customers'); + + done(); + }); + }); + }); + it('updates swagger object when a remote method is disabled', function(done) { var app = loopback(); app.set('remoting', { cors: false });