Skip to content

Commit

Permalink
fix(associations): correctly parse include options (#10580)
Browse files Browse the repository at this point in the history
  • Loading branch information
u9r52sld authored and sushantdhiman committed Mar 25, 2019
1 parent 83f696a commit ef18710
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
26 changes: 11 additions & 15 deletions lib/model.js
Expand Up @@ -90,7 +90,7 @@ class Model {
}

if (!options.includeValidated) {
this.constructor._conformOptions(options, this.constructor);
this.constructor._conformIncludes(options, this.constructor);
if (options.include) {
this.constructor._expandIncludeAll(options);
this.constructor._validateIncludedElements(options);
Expand Down Expand Up @@ -306,14 +306,9 @@ class Model {
}
}

static _conformOptions(options, self) {
if (self) {
self._expandAttributes(options);
}
static _conformIncludes(options, self) {
if (!options.include) return;

if (!options.include) {
return;
}
// if include is not an array, wrap in an array
if (!Array.isArray(options.include)) {
options.include = [options.include];
Expand Down Expand Up @@ -371,17 +366,17 @@ class Model {
if (!include.model) include.model = model;
if (!include.as) include.as = include.association.as;

this._conformOptions(include, model);
this._conformIncludes(include, model);
return include;
}

if (include.model) {
this._conformOptions(include, include.model);
this._conformIncludes(include, include.model);
return include;
}

if (include.all) {
this._conformOptions(include);
this._conformIncludes(include);
return include;
}
}
Expand Down Expand Up @@ -789,7 +784,7 @@ class Model {

static _baseMerge(...args) {
_.assignWith(...args);
this._conformOptions(args[0]);
this._conformIncludes(args[0], this);
this._uniqIncludes(args[0]);
return args[0];
}
Expand Down Expand Up @@ -1700,7 +1695,8 @@ class Model {
return this.runHooks('beforeFind', options);
}
}).then(() => {
this._conformOptions(options, this);
this._conformIncludes(options, this);
this._expandAttributes(options);
this._expandIncludeAll(options);

if (options.hooks) {
Expand Down Expand Up @@ -1936,7 +1932,7 @@ class Model {
options = Utils.cloneDeep(options);

this._injectScope(options);
this._conformOptions(options, this);
this._conformIncludes(options, this);

if (options.include) {
this._expandIncludeAll(options);
Expand Down Expand Up @@ -2149,7 +2145,7 @@ class Model {
}, options || {});

if (!options.includeValidated) {
this._conformOptions(options, this);
this._conformIncludes(options, this);
if (options.include) {
this._expandIncludeAll(options);
this._validateIncludedElements(options);
Expand Down
40 changes: 40 additions & 0 deletions test/integration/model/scope/associations.test.js
Expand Up @@ -418,6 +418,46 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(user.dataValues).not.to.have.property('secret');
});
});

it('should not throw error', function() {
const Clientfile = this.sequelize.define('clientfile');
const Mission = this.sequelize.define('mission', { secret: Sequelize.STRING });
const Building = this.sequelize.define('building');
const MissionAssociation = Clientfile.hasOne(Mission);
const BuildingAssociation = Clientfile.hasOne(Building);

return this.sequelize.sync({ force: true })
.then(() => {
return Clientfile.findAll({
include: [
{
association: 'mission',
where: {
secret: 'foo'
}
},
{
association: 'building'
}
]
});
})
.then(() => {
return Clientfile.findAll({
include: [
{
association: MissionAssociation,
where: {
secret: 'foo'
}
},
{
association: BuildingAssociation
}
]
});
});
});
});
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/unit/model/include.test.js
Expand Up @@ -256,7 +256,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
const options = {
include: ['Owner']
};
Sequelize.Model._conformOptions(options, this.Company);
Sequelize.Model._conformIncludes(options, this.Company);

expect(options.include[0]).to.deep.equal({
model: this.User,
Expand All @@ -272,7 +272,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
attributes: ['id']
}]
};
Sequelize.Model._conformOptions(options, this.Company);
Sequelize.Model._conformIncludes(options, this.Company);

expect(options.include[0]).to.deep.equal({
model: this.User,
Expand All @@ -290,7 +290,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
};

expect(() => {
Sequelize.Model._conformOptions(options, this.Company);
Sequelize.Model._conformIncludes(options, this.Company);
}).to.throw('Include unexpected. Element has to be either a Model, an Association or an object.');
});

Expand All @@ -302,7 +302,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
};

expect(() => {
Sequelize.Model._conformOptions(options, this.Company);
Sequelize.Model._conformIncludes(options, this.Company);
}).to.throw('Include unexpected. Element has to be either a Model, an Association or an object.');
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/sql/generateJoin.test.js
Expand Up @@ -18,7 +18,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {

const name = `${path}, ${util.inspect(options, { depth: 10 })}`;

Sequelize.Model._conformOptions(options);
Sequelize.Model._conformIncludes(options);
options = Sequelize.Model._validateIncludedElements(options);

const include = _.at(options, path)[0];
Expand Down

0 comments on commit ef18710

Please sign in to comment.