Merge pull request #236 from strongloop/feature/remove-model

feat: rebuild swagger after a model was deleted
This commit is contained in:
Miroslav Bajtoš 2018-04-17 10:22:58 +02:00 committed by GitHub
commit 7f90b03eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 10 deletions

View File

@ -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) {

View File

@ -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"
},

View File

@ -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 });