Merge pull request #1718 from strongloop/fix/include-crash-2x

fix: normalize include with boolean or number
This commit is contained in:
Miroslav Bajtoš 2019-04-15 12:49:03 +02:00 committed by GitHub
commit 18da6bce7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View File

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

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
// fixed by DB optimization
it('should support include scope on hasAndBelongsToMany', function(done) {