Merge pull request #222 from strongloop/refresh-on-remote-method-added

Refresh swagger on remoteMethodAdded
This commit is contained in:
Raymond Feng 2017-09-15 11:01:07 -07:00 committed by GitHub
commit 0b02e9f00e
2 changed files with 42 additions and 0 deletions

View File

@ -116,6 +116,13 @@ function mountSwagger(loopbackApplication, swaggerApp, opts) {
swaggerObject = createSwaggerObject(loopbackApplication, opts); swaggerObject = createSwaggerObject(loopbackApplication, opts);
}); });
// 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);
});
// 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', function() {

View File

@ -359,6 +359,41 @@ describe('explorer', function() {
}); });
}); });
it('updates swagger object when a remote method is added', function(done) {
var app = loopback();
app.set('remoting', { cors: false });
configureRestApiAndExplorer(app, '/explorer');
// Ensure the swagger object was built
request(app)
.get('/explorer/swagger.json')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
// Check the method that will be disabled
var paths = Object.keys(res.body.paths);
expect(paths).to.contain('/products/findOne');
var Product = app.models.Product;
Product.findOne2 = function(cb) { cb(null, 1); };
Product.remoteMethod('findOne2', {});
// Request swagger.json again
request(app)
.get('/explorer/swagger.json')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
var paths = Object.keys(res.body.paths);
expect(paths).to.contain('/products/findOne2');
done();
});
});
});
function givenLoopBackAppWithExplorer(explorerBase) { function givenLoopBackAppWithExplorer(explorerBase) {
return function(done) { return function(done) {
var app = this.app = loopback(); var app = this.app = loopback();