Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] initial-relationship-setup must handle null #4910

Merged
merged 2 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions addon/-private/system/relationships/state/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,15 @@ export default class ManyRelationship extends Relationship {
}

setInitialInternalModels(internalModels) {
let args = [0, this.canonicalState.length].concat(internalModels);
this.canonicalState.splice.apply(this.canonicalState, args);
internalModels.forEach(internalModel => {
this.canonicalMembers.add(internalModel);
this.members.add(internalModel);
this.setupInverseRelationship(internalModel);
});
if (internalModels && internalModels.length) {
Copy link
Member

Choose a reason for hiding this comment

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

Why would it be true but not have a length?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was just being overly cautious

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have only ever hit undefined or an array with items in practice

Copy link
Member

Choose a reason for hiding this comment

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

If it's not undefined but has no length, l think we are in a bad state. We should assert against that in debug maybe, but we shouldn't guard, that would just hide a bug

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped the length check, I suspect asserting length here would be asserting at the wrong layer.

let args = [0, this.canonicalState.length].concat(internalModels);
this.canonicalState.splice.apply(this.canonicalState, args);
internalModels.forEach(internalModel => {
this.canonicalMembers.add(internalModel);
this.members.add(internalModel);
this.setupInverseRelationship(internalModel);
});
}
}

fetchLink() {
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/model/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,48 @@ test('hasMany does not notify when it is initially reified', function(assert) {
});
});

test('hasMany can be initially reified with null', function(assert) {
assert.expect(1);

const Tag = DS.Model.extend({
name: DS.attr('string'),
people: DS.hasMany('person', { async: false })
});

const Person = DS.Model.extend({
name: DS.attr('string'),
tag: DS.belongsTo('tag', { async: false })
});

let env = setupStore({ tag: Tag, person: Person });
let { store } = env;

env.adapter.shouldBackgroundReloadRecord = () => false;

run(() => {
store.push({
data: {
type: 'tag',
id: 1,
attributes: {
name: 'whatever'
},
relationships: {
people: {
data: null
}
}
}
});
});

return run(() => {
let tag = store.peekRecord('tag', 1);

assert.equal(tag.get('people.length'), 0, 'relationship is correct');
});
});

test('hasMany tolerates reflexive self-relationships', function(assert) {
assert.expect(1);

Expand Down