Merge pull request #1581 from spurreiter/include-crashes

fix: normalize include with boolean or number
This commit is contained in:
Miroslav Bajtoš 2019-04-12 17:05:02 +02:00 committed by GitHub
commit fe83f3cfd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -16,6 +16,8 @@ const uniq = utils.uniq;
const idName = utils.idName; const idName = utils.idName;
const debug = require('debug')('loopback:include'); const debug = require('debug')('loopback:include');
const DISALLOWED_TYPES = ['boolean', 'number', 'symbol', 'function'];
/*! /*!
* Normalize the include to be an array * Normalize the include to be an array
* @param include * @param include
@ -47,6 +49,9 @@ function normalizeInclude(include) {
newInclude = newInclude.concat(subIncludes); newInclude = newInclude.concat(subIncludes);
} }
return newInclude; return newInclude;
} else if (DISALLOWED_TYPES.includes(typeof include)) {
debug('Ignoring invalid "include" value of type %s:', typeof include, include);
return [];
} else { } else {
return include; return include;
} }

View File

@ -1254,6 +1254,43 @@ describe('include', function() {
}); });
}); });
it('should not throw on fetch User if include is boolean equals true', function(done) {
User.find({include: true}, function(err, users) {
if (err) return done(err);
should.exist(users);
users.should.not.be.empty();
done();
});
});
it('should not throw on fetch User if include is number', function(done) {
User.find({include: 1}, function(err, users) {
if (err) return done(err);
should.exist(users);
users.should.not.be.empty();
done();
});
});
it('should not throw on fetch User if include is symbol', function(done) {
User.find({include: Symbol('include')}, function(err, users) {
if (err) return done(err);
should.exist(users);
users.should.not.be.empty();
done();
});
});
it('should not throw on fetch User if include is function', function(done) {
const include = () => {};
User.find({include}, function(err, users) {
if (err) return done(err);
should.exist(users);
users.should.not.be.empty();
done();
});
});
// Not implemented correctly, see: loopback-datasource-juggler/issues/166 // Not implemented correctly, see: loopback-datasource-juggler/issues/166
// fixed by DB optimization // fixed by DB optimization
it('should support include scope on hasAndBelongsToMany', function(done) { it('should support include scope on hasAndBelongsToMany', function(done) {