From b015ef25dca5f03a77b147780fce1c651cd14872 Mon Sep 17 00:00:00 2001 From: Dan Sudol Date: Mon, 22 Aug 2016 23:53:06 +0100 Subject: [PATCH] fixing json api data type field for polymorphic models --- addon/converter/jsonapi-fixture-converter.js | 12 ++++-------- tests/dummy/app/tests/factories/hat.js | 3 +++ tests/unit/jsonapi-adapter-test.js | 9 +++++++++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/addon/converter/jsonapi-fixture-converter.js b/addon/converter/jsonapi-fixture-converter.js index 906adfac..5584d22d 100644 --- a/addon/converter/jsonapi-fixture-converter.js +++ b/addon/converter/jsonapi-fixture-converter.js @@ -12,7 +12,7 @@ class JSONAPIFixtureConverter extends Converter { constructor(store, options = { transformKeys: true, serializeMode: false }) { super(store, options); - this.typeTransformFn = this.serializeMode ? this.typeTransformViaSerializer : this.noTransformFn; + this.typeTransformFn = this.serializeMode ? this.typeTransformViaSerializer : dasherize; this.defaultKeyTransformFn = dasherize; this.polymorphicTypeTransformFn = dasherize; this.included = []; @@ -58,13 +58,9 @@ class JSONAPIFixtureConverter extends Converter { @param {Object or DS.Model instance} record @param {Object} relationship */ - normalizeAssociation(record, relationship) { + normalizeAssociation(record) { if (Ember.typeOf(record) === 'object') { - if (relationship.options.polymorphic) { - return { type: this.typeTransformFn(dasherize(record.type)), id: record.id }; - } else { - return { type: this.typeTransformFn(record.type), id: record.id }; - } + return { type: this.typeTransformFn(record.type), id: record.id }; } else { return { type: this.typeTransformFn(record.constructor.modelName), id: record.id }; } @@ -86,7 +82,7 @@ class JSONAPIFixtureConverter extends Converter { */ convertSingle(modelName, fixture) { let data = { - type: this.typeTransformFn(modelName), + type: this.typeTransformFn(fixture.type || modelName), attributes: this.extractAttributes(modelName, fixture), }; diff --git a/tests/dummy/app/tests/factories/hat.js b/tests/dummy/app/tests/factories/hat.js index 0eb210d7..778442d5 100644 --- a/tests/dummy/app/tests/factories/hat.js +++ b/tests/dummy/app/tests/factories/hat.js @@ -1,7 +1,10 @@ import FactoryGuy from 'ember-data-factory-guy'; FactoryGuy.define('hat', { + traits: { + big: {type: "SmallHat"}, + small: {type: "BigHat"}, round: {shape: "round"}, square: {shape: "square"}, with_user: { user: {} }, diff --git a/tests/unit/jsonapi-adapter-test.js b/tests/unit/jsonapi-adapter-test.js index 9454cba8..81f6c7fa 100644 --- a/tests/unit/jsonapi-adapter-test.js +++ b/tests/unit/jsonapi-adapter-test.js @@ -95,6 +95,15 @@ test("returns a relationship with an index and key", function() { deepEqual(user.get(1).company, { id: 2, type: 'company' }); }); +module(title(adapter, 'FactoryGuy#buildList custom'), inlineSetup(App, serializerType)); + +test("mock returns inherited models with proper types", function(assert) { + let list = buildList('hat', 'big', 'small'); + let data = list.data; + equal(data[0].type, 'small-hat'); + equal(data[1].type, 'big-hat'); +}); + module(title(adapter, 'FactoryGuy#build custom'), inlineSetup(App, serializerType)); test("with traits defining model attributes", function() {