From 3bd0c5d97d6ff0532e97a1a756d8b83a5c0e0db8 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Wed, 1 Mar 2017 13:54:18 -0800 Subject: [PATCH] =?UTF-8?q?remove=20isPrimaryType=E2=80=99s=20reliance=20o?= =?UTF-8?q?n=20modelName=20(#4832)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * when interacting with a user-provided modelName, always be sure to normalize it. --- addon/-private/system/store.js | 1 + addon/serializers/rest.js | 5 ++--- .../serializers/rest-serializer-test.js | 15 ++++++++++---- tests/unit/store/has-model-for-test.js | 20 +++++++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 tests/unit/store/has-model-for-test.js diff --git a/addon/-private/system/store.js b/addon/-private/system/store.js index 7d2e6d3824f..3d3a3907394 100644 --- a/addon/-private/system/store.js +++ b/addon/-private/system/store.js @@ -2338,6 +2338,7 @@ Store = Service.extend({ _hasModelFor(modelName) { let owner = getOwner(this); + modelName = normalizeModelName(modelName); if (owner.factoryFor) { return !!owner.factoryFor(`model:${modelName}`); diff --git a/addon/serializers/rest.js b/addon/serializers/rest.js index 8e90eed5330..8db235614d5 100644 --- a/addon/serializers/rest.js +++ b/addon/serializers/rest.js @@ -183,7 +183,7 @@ let RESTSerializer = JSONSerializer.extend({ included: [] }; - let modelClass = store._modelFor(modelName); + let modelClass = store.modelFor(modelName); let serializer = store.serializerFor(modelName); Ember.makeArray(arrayHash).forEach((hash) => { @@ -373,8 +373,7 @@ let RESTSerializer = JSONSerializer.extend({ }, isPrimaryType(store, typeName, primaryTypeClass) { - let typeClass = store.modelFor(typeName); - return typeClass.modelName === primaryTypeClass.modelName; + return store.modelFor(typeName) === primaryTypeClass; }, /** diff --git a/tests/integration/serializers/rest-serializer-test.js b/tests/integration/serializers/rest-serializer-test.js index fd99b33fa8a..a5c37866c56 100644 --- a/tests/integration/serializers/rest-serializer-test.js +++ b/tests/integration/serializers/rest-serializer-test.js @@ -109,8 +109,17 @@ test("normalizeResponse with custom modelNameFromPayloadKey", function(assert) { }; var jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }], - super_villains: [{ id: "1", firstName: "Tom", lastName: "Dale", homePlanet: "1" }] + home_planets: [{ + id: "1", + name: "Umber", + superVillains: [1] + }], + super_villains: [{ + id: "1", + firstName: "Tom", + lastName: "Dale", + homePlanet: "1" + }] }; var array; @@ -277,7 +286,6 @@ test("serialize polymorphicType", function(assert) { }); test("serialize polymorphicType with decamelized modelName", function(assert) { - YellowMinion.modelName = 'yellow-minion'; var tom, ray; run(function() { tom = env.store.createRecord('yellow-minion', { name: "Alex", id: "124" }); @@ -529,7 +537,6 @@ test("serializeIntoHash", function(assert) { }); test("serializeIntoHash with decamelized modelName", function(assert) { - HomePlanet.modelName = 'home-planet'; run(function() { league = env.store.createRecord('home-planet', { name: "Umber", id: "123" }); }); diff --git a/tests/unit/store/has-model-for-test.js b/tests/unit/store/has-model-for-test.js new file mode 100644 index 00000000000..0cd5dd127a5 --- /dev/null +++ b/tests/unit/store/has-model-for-test.js @@ -0,0 +1,20 @@ +import {createStore} from 'dummy/tests/helpers/store'; +import {module, test} from 'qunit'; +import DS from 'ember-data'; + +let store; + +module('unit/store/has-model-For', { + beforeEach() { + store = createStore({ + adapter: DS.Adapter.extend(), + 'one-foo': DS.Model.extend({}), + 'two-foo': DS.Model.extend({}) + }); + } +}); + +test(`hasModelFor correctly normalizes`, function(assert) { + assert.equal(store._hasModelFor('oneFoo'), true); + assert.equal(store._hasModelFor('twoFoo'). true); +});