不要怂,就是干,撸起袖子干!

Commit 454cf48e by Sushant Committed by GitHub

fix(model): throw for invalid include type (#10527)

1 parent 0b5aa71f
...@@ -324,48 +324,56 @@ class Model { ...@@ -324,48 +324,56 @@ class Model {
} }
static _conformInclude(include, self) { static _conformInclude(include, self) {
let model; if (include) {
let model;
if (include._pseudo) return include; if (include._pseudo) return include;
include = this._transformStringAssociation(include, self); include = this._transformStringAssociation(include, self);
if (include instanceof Association) { if (include instanceof Association) {
if (self && include.target.name === self.name) { if (self && include.target.name === self.name) {
model = include.source; model = include.source;
} else { } else {
model = include.target; model = include.target;
}
return { model, association: include, as: include.as };
} }
return { model, association: include, as: include.as }; if (include.prototype && include.prototype instanceof Model) {
} return { model: include };
if (include.prototype && include.prototype instanceof Model) { }
return { model: include };
}
if (_.isPlainObject(include)) {
if (include.association) {
include.association = this._transformStringAssociation(include.association, self); if (_.isPlainObject(include)) {
if (include.association) {
include.association = this._transformStringAssociation(include.association, self);
if (self && include.association.target.name === self.name) { if (self && include.association.target.name === self.name) {
model = include.association.source; model = include.association.source;
} else { } else {
model = include.association.target; model = include.association.target;
}
if (!include.model) include.model = model;
if (!include.as) include.as = include.association.as;
this._conformOptions(include, model);
return include;
} }
if (!include.model) { if (include.model) {
include.model = model; this._conformOptions(include, include.model);
return include;
} }
if (!include.as) {
include.as = include.association.as; if (include.all) {
this._conformOptions(include);
return include;
} }
} else {
model = include.model;
} }
this._conformOptions(include, model);
return include;
} }
throw new Error('Include unexpected. Element has to be either a Model, an Association or an object.'); throw new Error('Include unexpected. Element has to be either a Model, an Association or an object.');
} }
......
...@@ -67,7 +67,6 @@ ...@@ -67,7 +67,6 @@
"eslint": "^5.15.0", "eslint": "^5.15.0",
"eslint-plugin-jsdoc": "^4.1.1", "eslint-plugin-jsdoc": "^4.1.1",
"eslint-plugin-mocha": "^5.2.1", "eslint-plugin-mocha": "^5.2.1",
"hints": "^1.x",
"husky": "^1.3.1", "husky": "^1.3.1",
"js-combinatorics": "^0.5.4", "js-combinatorics": "^0.5.4",
"lcov-result-merger": "^3.0.0", "lcov-result-merger": "^3.0.0",
......
...@@ -8,7 +8,6 @@ const chai = require('chai'), ...@@ -8,7 +8,6 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => { describe(Support.getTestDialectTeaser('Model'), () => {
describe('all', () => { describe('all', () => {
const Referral = current.define('referal'); const Referral = current.define('referal');
Referral.belongsTo(Referral); Referral.belongsTo(Referral);
...@@ -252,7 +251,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -252,7 +251,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
describe('_conformInclude: string alias', () => { describe('_conformInclude', () => {
it('should expand association from string alias', function() { it('should expand association from string alias', function() {
const options = { const options = {
include: ['Owner'] include: ['Owner']
...@@ -282,6 +281,30 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -282,6 +281,30 @@ describe(Support.getTestDialectTeaser('Model'), () => {
as: 'Owner' as: 'Owner'
}); });
}); });
it('should throw an error if invalid model is passed', function() {
const options = {
include: [{
model: null
}]
};
expect(() => {
Sequelize.Model._conformOptions(options, this.Company);
}).to.throw('Include unexpected. Element has to be either a Model, an Association or an object.');
});
it('should throw an error if invalid association is passed', function() {
const options = {
include: [{
association: null
}]
};
expect(() => {
Sequelize.Model._conformOptions(options, this.Company);
}).to.throw('Include unexpected. Element has to be either a Model, an Association or an object.');
});
}); });
describe('_getIncludedAssociation', () => { describe('_getIncludedAssociation', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!