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:
spurreiter 2018-05-08 11:40:38 +02:00 committed by Miroslav Bajtoš
parent 3965078f76
commit aff2318c41
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
2 changed files with 43 additions and 0 deletions

View File

@ -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;
} }

View File

@ -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) {