Skip to content

Clone defaultScope so it doesn't get overwritten in Model#init - #6971

Merged
sushantdhiman merged 1 commit into
sequelize:masterfrom
TendInc:fix_global_default_attributes
Dec 22, 2016
Merged

sushantdhiman merged 1 commit into
sequelize:masterfrom
TendInc:fix_global_default_attributes

Conversation

@broofa

@broofa broofa commented Dec 10, 2016

Copy link
Copy Markdown
Contributor

Pull Request check-list

(I have not been able to test against master due to npm install failing with `gyp: Call to 'pg_config --libdir' returned exit status 127 while in binding.gyp. However this is reproduceable with latest version in npm and the code seems largely unchanged, so I'm assuming this is still an issue...?)

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does your issue contain a link to existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Have you added an entry under Future in the changelog?

NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.

Description of change

https://gist.github.com/broofa/cee97d28e3923ad62438561438f7e12a is a standalone test case showing how sequelized.options.define.defaultScope.options.attributes is overwritten when defining a model. It logs the state of of this property before, between, and after doing two define() calls. The output I see (Sequelized v3.27.0) is this:

kieffer@Roberts-MacBook-Pro$ node defaultScope_test.js
attributes before defining ModelA: { exclude: [ 'createdAt', 'updatedAt', 'deletedAt' ] }
attributes after defining ModelA: [ 'modelAUuid', 'fooA', 'barA' ]
attributes after defining ModelB: [ 'modelAUuid', 'fooA', 'barA' ]

Cloning defaultScope in Model.init() as per this PR fixes this, however I'm not 100% sure this is the correct solution. (If nothing else, doing a deep-copy would probably be more resistent to similar bugs emerging in the future).

@mention-bot

Copy link
Copy Markdown

@broofa, thanks for your PR! By analyzing the history of the files in this pull request, we identified @felixfbecker, @BridgeAR and @janmeier to be potential reviewers.

@broofa

broofa commented Dec 10, 2016

Copy link
Copy Markdown
Contributor Author

Verified this is still an issue in v4.0.0-2

Comment thread lib/model.js Outdated
this._findAutoIncrementField();

this._scope = this.options.defaultScope;
this._scope = Object.assign({}, this.options.defaultScope);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a deep clone?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, probably. What's the preferred way of doing that? JSON.parse(JSON.stringify(...))?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.cloneDeep

@felixfbecker

Copy link
Copy Markdown
Contributor

There are two test failures

@felixfbecker felixfbecker added the type: bug DEPRECATED: replace with the "bug" issue type label Dec 10, 2016
@broofa

broofa commented Dec 11, 2016 •

Copy link
Copy Markdown
Contributor Author

Hmm. Integration check failing with "before each" hook for "should uppercase the first letter in alias getter, but not in eager loading"... but I see the same test failure on the master branch, and it seems unlikely to be related to this PR. :-/

In other news, I stumbled across this comment of yours, @felixfbecker, on Utils.merge, which I'm not sure what to make of. Is cloning globalOptions like this going to run afoul of whatever that issue is?

@felixfbecker

Copy link
Copy Markdown
Contributor

That's not my comment, and it should not be relevant for defaultScope

@broofa

broofa commented Dec 11, 2016

Copy link
Copy Markdown
Contributor Author

@felixfbecker, sorry about that. I tracked that comment down to this commit, attributed to @greenkeeperio-bot and @janmeier.

So... what's up with that failing integration check? On this project's home page the Travis badge says "build failing", and links here... which calls out this PR. Is Travis testing against all outstanding PRs or something? My apologies if that's a noob question.

... but, as I said above, I see the same error on master (w/out my PR).

@codecov-io

codecov-io commented Dec 11, 2016 •

Copy link
Copy Markdown

Current coverage is 94.67% (diff: 100%)

Merging #6971 into master will decrease coverage by <.01%

Powered by Codecov. Last update 5594889...add6b3e

Comment thread lib/model.js Outdated
this.sequelize = options.sequelize;

const globalOptions = this.sequelize.options;
const globalOptions = _.cloneDeep(this.sequelize.options);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears to me that globalOptions is not used besides in L688. if that is the case, I think we could just modify L689 to read

options = Utils.merge({}, globalOptions.define, options)

@felixfbecker

Copy link
Copy Markdown
Contributor

Restarted the jobs and builds are now fixed

@broofa

broofa commented Dec 11, 2016

Copy link
Copy Markdown
Contributor Author

Utils.merge copies-by-reference when the target object is undefined, which turns out to not solve the original problem. But _.cloneDeep(globalOptions.define) does the trick.

Comment thread lib/model.js
* @return {Model}
*/
static init(attributes, options) { // testhint options:none
options = options || {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options parameter must stay optional, please keep this line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, this is functionally the same since options = {}, fails the options.sequelize check at L681 (precommit, below), but I'll revert it.

Comment thread lib/model.js
if (globalOptions.define) {
options = Utils.merge(globalOptions.define, options);
}
options = Utils.merge(_.cloneDeep(globalOptions.define), options);

@felixfbecker felixfbecker Dec 12, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens now if globalOptions.define is undefined?

@broofa broofa Dec 12, 2016 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.cloneDeep(undefined) -> undefined, and Utils.merge() ignores undefined values... so functionally unchanged.

> var Utils = require('./lib/utils.js')
undefined
> var _ = require('lodash')
Expression assignment to _ now disabled.
undefined
> Utils.merge(_.cloneDeep())
{}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integration test failing again for unrelated reasons?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug DEPRECATED: replace with the "bug" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants