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] inverseFor should respect inverse: null #5261

Merged
merged 1 commit into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion addon/-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function findPossibleInverses(type, inverseType, name, relationshipsSoFar) {
let relationships = relationshipMap.get(type.modelName).filter(relationship => {
let optionsForRelationship = inverseType.metaForProperty(relationship.name).options;

if (!optionsForRelationship.inverse) {
if (!optionsForRelationship.inverse && optionsForRelationship.inverse !== null) {
return true;
}

Expand Down
34 changes: 34 additions & 0 deletions tests/integration/relationships/inverse-relationships-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { module, test } from 'qunit';

import DS from 'ember-data';

let {
Model,
hasMany,
belongsTo
} = DS;

var Post, Comment, Message, User;

module('integration/relationships/inverse_relationships - Inverse Relationships');
Expand Down Expand Up @@ -65,6 +71,34 @@ test("Inverse relationships can be explicitly nullable", function(assert) {
assert.equal(post.inverseFor('participants').name, 'posts', 'Post.participants inverse is User.posts');
});

test("Null inverses are excluded from potential relationship resolutions", function(assert) {
User = Model.extend();

Post = Model.extend({
lastParticipant: belongsTo('user', { inverse: null, async: false }),
participants: hasMany('user', { async: false })
});

User.reopen({
posts: hasMany('post', { async: false })
});

let store = createStore({
user: User,
post: Post
});
let user, post;

run(function() {
user = store.createRecord('user');
post = store.createRecord('post');
});

assert.equal(user.inverseFor('posts').name, 'participants', 'User.posts inverse is Post.participants');
assert.equal(post.inverseFor('lastParticipant'), null, 'Post.lastParticipant has no inverse');
assert.equal(post.inverseFor('participants').name, 'posts', 'Post.participants inverse is User.posts');
});

test("When a record is added to a has-many relationship, the inverse belongsTo can be set explicitly", function(assert) {
Post = DS.Model.extend({
comments: DS.hasMany('comment', { inverse: 'redPost', async: false })
Expand Down
12 changes: 9 additions & 3 deletions tests/integration/serializers/json-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ module("integration/serializer/json - JSONSerializer", {
});
Comment = DS.Model.extend({
body: DS.attr('string'),
post: DS.belongsTo('post', { async: false })
post: DS.belongsTo('post', { inverse: null, async: false })
});
Favorite = DS.Model.extend({
post: DS.belongsTo('post', { async: true, polymorphic: true })
post: DS.belongsTo('post', { inverse: null, async: true, polymorphic: true })
});
env = setupStore({
post: Post,
Expand Down Expand Up @@ -237,7 +237,12 @@ test("serializeHasMany respects keyForRelationship", function(assert) {

run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase", id: "1" });
comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post, id: "1" });
comment = env.store.createRecord('comment', {
body: "Omakase is delicious",
post: post,
id: "1"
});
post.get('comments').pushObject(comment);
});

var json = {};
Expand Down Expand Up @@ -635,6 +640,7 @@ test('Serializer respects `serialize: true` on the attrs hash for a `hasMany` pr
run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase" });
comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post });
post.get('comments').pushObject(comment);
});

var serializer = env.store.serializerFor("post");
Expand Down