Merge pull request #236 from strongloop/feature/remove-model
feat: rebuild swagger after a model was deleted
This commit is contained in:
commit
7f90b03eff
18
index.js
18
index.js
|
@ -119,22 +119,18 @@ function mountSwagger(loopbackApplication, swaggerApp, opts) {
|
||||||
|
|
||||||
// listening to modelRemoted event for updating the swaggerObject
|
// listening to modelRemoted event for updating the swaggerObject
|
||||||
// with the newly created model to appear in the Swagger UI.
|
// with the newly created model to appear in the Swagger UI.
|
||||||
loopbackApplication.on('modelRemoted', function() {
|
loopbackApplication.on('modelRemoted', rebuildSwaggerObject);
|
||||||
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
||||||
});
|
loopbackApplication.on('modelDeleted', rebuildSwaggerObject);
|
||||||
|
|
||||||
// listening to started event for updating the swaggerObject
|
// listening to started event for updating the swaggerObject
|
||||||
// when a call to app.models.[modelName].nestRemoting([modelName])
|
// when a call to app.models.[modelName].nestRemoting([modelName])
|
||||||
// to appear that method in the Swagger UI.
|
// to appear that method in the Swagger UI.
|
||||||
loopbackApplication.on('remoteMethodAdded', function() {
|
loopbackApplication.on('remoteMethodAdded', rebuildSwaggerObject);
|
||||||
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
||||||
});
|
|
||||||
|
|
||||||
// listening to remoteMethodDisabled event for updating the swaggerObject
|
// listening to remoteMethodDisabled event for updating the swaggerObject
|
||||||
// when a remote method is disabled to hide that method in the Swagger UI.
|
// when a remote method is disabled to hide that method in the Swagger UI.
|
||||||
loopbackApplication.on('remoteMethodDisabled', function() {
|
loopbackApplication.on('remoteMethodDisabled', rebuildSwaggerObject);
|
||||||
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
||||||
});
|
|
||||||
|
|
||||||
var resourcePath = (opts && opts.resourcePath) || 'swagger.json';
|
var resourcePath = (opts && opts.resourcePath) || 'swagger.json';
|
||||||
if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath;
|
if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath;
|
||||||
|
@ -145,6 +141,10 @@ function mountSwagger(loopbackApplication, swaggerApp, opts) {
|
||||||
swaggerApp.get(resourcePath, function sendSwaggerObject(req, res) {
|
swaggerApp.get(resourcePath, function sendSwaggerObject(req, res) {
|
||||||
res.status(200).send(swaggerObject);
|
res.status(200).send(swaggerObject);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function rebuildSwaggerObject() {
|
||||||
|
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupCors(swaggerApp, remotes) {
|
function setupCors(swaggerApp, remotes) {
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
"chai": "^3.2.0",
|
"chai": "^3.2.0",
|
||||||
"eslint": "^2.8.0",
|
"eslint": "^2.8.0",
|
||||||
"eslint-config-loopback": "^2.0.0",
|
"eslint-config-loopback": "^2.0.0",
|
||||||
"loopback": "^3.0.0",
|
"loopback": "^3.19.0",
|
||||||
"mocha": "^2.2.5",
|
"mocha": "^2.2.5",
|
||||||
"supertest": "^1.0.1"
|
"supertest": "^1.0.1"
|
||||||
},
|
},
|
||||||
|
|
|
@ -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) {
|
it('updates swagger object when a remote method is disabled', function(done) {
|
||||||
var app = loopback();
|
var app = loopback();
|
||||||
app.set('remoting', { cors: false });
|
app.set('remoting', { cors: false });
|
||||||
|
|
Loading…
Reference in New Issue