Skip to content

Commit

Permalink
Fix: assign unknown properties in init after initialization is finish…
Browse files Browse the repository at this point in the history
…ed to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>
  • Loading branch information
2 people authored and snewcomer committed Feb 3, 2022
1 parent 21b43fc commit 6e4b19a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ module('Store.createRecord() coverage', function (hooks) {
store = owner.lookup('service:store');
});

test("createRecord doesn't crash when setter is involved", async function (assert) {
class User extends Model {
@attr() email;

get name() {
return this.email ? this.email.substring(0, this.email.indexOf('@')) : '';
}

set name(value) {
this.email = `${value.toLowerCase()}@ember.js`;
}
}
this.owner.register(`model:user`, User);
const store = this.owner.lookup('service:store');

const user = store.createRecord('user', { name: 'Robert' });
assert.strictEqual(user.email, '[email protected]');
});

test('unloading a newly created a record with a sync belongsTo relationship', async function (assert) {
let chris = store.push({
data: {
Expand Down
8 changes: 6 additions & 2 deletions packages/model/addon/-private/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ function computeOnce(target, key, desc) {
@uses EmberData.DeprecatedEvented
*/
class Model extends EmberObject {
init(...args) {
super.init(...args);
init(options = {}) {
const createProps = options._createProps;
delete options._createProps;
super.init(options);

if (DEBUG) {
if (!this._internalModel) {
Expand All @@ -133,6 +135,7 @@ class Model extends EmberObject {
if (CUSTOM_MODEL_CLASS) {
this.___recordState = new RecordState(this);
}
this.setProperties(createProps);
}

/**
Expand Down Expand Up @@ -2076,6 +2079,7 @@ class Model extends EmberObject {
// the values initialized during create to `setUnknownProperty`
Model.prototype._internalModel = null;
Model.prototype.store = null;
Model.prototype._createProps = null;

if (HAS_DEBUG_PACKAGE) {
/**
Expand Down
3 changes: 2 additions & 1 deletion packages/store/addon/-private/system/ds-model-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ class Store extends CoreStore {
let createOptions: any = {
store: this,
_internalModel: internalModel,
// TODO deprecate allowing unknown args setting
_createProps: createRecordArgs,
container: null,
};
assign(createOptions, createRecordArgs);

// ensure that `getOwner(this)` works inside a model instance
setOwner(createOptions, getOwner(this));
Expand Down

0 comments on commit 6e4b19a

Please sign in to comment.