From 20810d725450c823b8d9877bead215b29e530264 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Mon, 20 Nov 2017 20:16:03 -0800 Subject: [PATCH] [BUGFIX] inverseFor should respect inverse: null --- addon/-private/system/model/model.js | 2 +- .../inverse-relationships-test.js | 34 +++++++++++++++++++ .../serializers/json-serializer-test.js | 12 +++++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/addon/-private/system/model/model.js b/addon/-private/system/model/model.js index 521d7425088..cca30e02349 100644 --- a/addon/-private/system/model/model.js +++ b/addon/-private/system/model/model.js @@ -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; } diff --git a/tests/integration/relationships/inverse-relationships-test.js b/tests/integration/relationships/inverse-relationships-test.js index bdae33053f5..91641283935 100644 --- a/tests/integration/relationships/inverse-relationships-test.js +++ b/tests/integration/relationships/inverse-relationships-test.js @@ -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'); @@ -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 }) diff --git a/tests/integration/serializers/json-serializer-test.js b/tests/integration/serializers/json-serializer-test.js index 1c8172d1a92..920e6064adf 100644 --- a/tests/integration/serializers/json-serializer-test.js +++ b/tests/integration/serializers/json-serializer-test.js @@ -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, @@ -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 = {}; @@ -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");