Merge pull request #141 from pktippa/master

Returning updated swaggerObject

Close #141
Fix #140
Fix https://github.com/strongloop/loopback/issues/1820
This commit is contained in:
Miroslav Bajtoš 2016-01-05 10:45:31 +01:00
commit 9e8a19f726
2 changed files with 37 additions and 0 deletions

View File

@ -95,6 +95,12 @@ function routes(loopbackApplication, options) {
function mountSwagger(loopbackApplication, swaggerApp, opts) {
var swaggerObject = createSwaggerObject(loopbackApplication, 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);
});
var resourcePath = opts && opts.resourcePath || 'swagger.json';
if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath;

View File

@ -220,6 +220,37 @@ describe('explorer', function() {
});
});
it('updates swagger object when a new model is added', function(done) {
var app = loopback();
configureRestApiAndExplorer(app, '/explorer');
// Ensure the swagger object was built
request(app)
.get('/explorer/swagger.json')
.expect(200)
.end(function(err) {
if (err) return done(err);
// Create a new model
var Model = loopback.PersistedModel.extend('Customer');
Model.attachTo(loopback.memory());
app.model(Model);
// 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.contain('Customer');
var paths = Object.keys(res.body.paths);
expect(paths).to.have.contain('/Customers');
done();
});
});
});
function givenLoopBackAppWithExplorer(explorerBase) {
return function(done) {
var app = this.app = loopback();