Merge pull request #1718 from strongloop/fix/include-crash-2x
fix: normalize include with boolean or number
This commit is contained in:
commit
18da6bce7c
|
@ -5,6 +5,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
var debug = require('debug')('loopback:include');
|
||||||
var g = require('strong-globalize')();
|
var g = require('strong-globalize')();
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var List = require('./list');
|
var List = require('./list');
|
||||||
|
@ -14,6 +15,8 @@ var defineCachedRelations = utils.defineCachedRelations;
|
||||||
var uniq = utils.uniq;
|
var uniq = utils.uniq;
|
||||||
var idName = utils.idName;
|
var idName = utils.idName;
|
||||||
|
|
||||||
|
var DISALLOWED_TYPES = ['boolean', 'number', 'symbol', 'function'];
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Normalize the include to be an array
|
* Normalize the include to be an array
|
||||||
* @param include
|
* @param include
|
||||||
|
@ -45,6 +48,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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -420,6 +420,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) {
|
||||||
|
var include = function() {};
|
||||||
|
User.find({include: 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) {
|
||||||
|
|
Loading…
Reference in New Issue