Skip to content

Commit

Permalink
Merge pull request #1717 from strongloop/fix/include-crash-3x
Browse files Browse the repository at this point in the history
fix: normalize include with boolean or number
  • Loading branch information
bajtos committed Apr 12, 2019
2 parents 127b45b + d67aeb1 commit 22f8bab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const uniq = utils.uniq;
const idName = utils.idName;
const debug = require('debug')('loopback:include');

const DISALLOWED_TYPES = ['boolean', 'number', 'symbol', 'function'];

/*!
* Normalize the include to be an array
* @param include
Expand Down Expand Up @@ -47,6 +49,9 @@ function normalizeInclude(include) {
newInclude = newInclude.concat(subIncludes);
}
return newInclude;
} else if (DISALLOWED_TYPES.includes(typeof include)) {
debug('Ignoring invalid "include" value of type %s:', typeof include, include);
return [];
} else {
return include;
}
Expand Down
37 changes: 37 additions & 0 deletions test/include.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// fixed by DB optimization
it('should support include scope on hasAndBelongsToMany', function(done) {
Expand Down

0 comments on commit 22f8bab

Please sign in to comment.