diff --git a/lib/include.js b/lib/include.js index 7da80451..05cea2a8 100644 --- a/lib/include.js +++ b/lib/include.js @@ -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; } diff --git a/test/include.test.js b/test/include.test.js index 616d0a6d..8875a829 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -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) {