fix: normalize include with boolean or number
- On include = true or include = 1 the lib crashes with "TypeError: includes.forEach is not a function". - checking for boolean and number type and return empty array. - fix: include symbol and function in normalization - fix: review changes; adding debug stmt + early exit for tests
This commit is contained in:
parent
3965078f76
commit
aff2318c41
|
@ -5,6 +5,7 @@
|
|||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var debug = require('debug')('loopback:include');
|
||||
var g = require('strong-globalize')();
|
||||
var utils = require('./utils');
|
||||
var List = require('./list');
|
||||
|
@ -14,6 +15,8 @@ var defineCachedRelations = utils.defineCachedRelations;
|
|||
var uniq = utils.uniq;
|
||||
var idName = utils.idName;
|
||||
|
||||
var DISALLOWED_TYPES = ['boolean', 'number', 'symbol', 'function'];
|
||||
|
||||
/*!
|
||||
* Normalize the include to be an array
|
||||
* @param include
|
||||
|
@ -45,6 +48,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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
// fixed by DB optimization
|
||||
it('should support include scope on hasAndBelongsToMany', function(done) {
|
||||
|
|
Loading…
Reference in New Issue