From 2790f8bbdd21d72ff06d1c0c45c991853c2f0cf3 Mon Sep 17 00:00:00 2001 From: Christoffer Persson Date: Sat, 22 Oct 2016 02:27:56 +0300 Subject: [PATCH] Assert when original and not normalized key is found in payload --- addon/serializers/json-api.js | 10 +++++ .../serializers/json-api-serializer-test.js | 41 +++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/addon/serializers/json-api.js b/addon/serializers/json-api.js index 52acb8871b2..107ed1b3d0c 100644 --- a/addon/serializers/json-api.js +++ b/addon/serializers/json-api.js @@ -265,6 +265,11 @@ const JSONAPISerializer = JSONSerializer.extend({ if (resourceHash.attributes[attributeKey] !== undefined) { attributes[key] = resourceHash.attributes[attributeKey]; } + runInDebug(() => { + if (resourceHash.attributes[attributeKey] === undefined && resourceHash.attributes[key] !== undefined) { + assert(`Your payload for '${modelClass.modelName}' contains '${key}', but your serializer is setup to look for '${attributeKey}'. This is most likely because Ember Data's JSON API serializer dasherizes attribute keys by default. You should subclass JSONAPISerializer and implement 'keyForAttribute(key) { return key; }' to prevent Ember Data from customizing your attribute keys.`, false); + } + }); }); } @@ -314,6 +319,11 @@ const JSONAPISerializer = JSONSerializer.extend({ relationships[key] = this.extractRelationship(relationshipHash); } + runInDebug(() => { + if (resourceHash.relationships[relationshipKey] === undefined && resourceHash.relationships[key] !== undefined) { + assert(`Your payload for '${modelClass.modelName}' contains '${key}', but your serializer is setup to look for '${relationshipKey}'. This is most likely because Ember Data's JSON API serializer dasherizes relationship keys by default. You should subclass JSONAPISerializer and implement 'keyForRelationship(key) { return key; }' to prevent Ember Data from customizing your relationship keys.`, false); + } + }); }); } diff --git a/tests/integration/serializers/json-api-serializer-test.js b/tests/integration/serializers/json-api-serializer-test.js index 64aa398ba17..342a0c7f2a7 100644 --- a/tests/integration/serializers/json-api-serializer-test.js +++ b/tests/integration/serializers/json-api-serializer-test.js @@ -22,7 +22,8 @@ module('integration/serializers/json-api-serializer - JSONAPISerializer', { lastName: DS.attr('string'), title: DS.attr('string'), handles: DS.hasMany('handle', { async: true, polymorphic: true }), - company: DS.belongsTo('company', { async: true }) + company: DS.belongsTo('company', { async: true }), + reportsTo: DS.belongsTo('user', { async: true, inverse: null }) }); Handle = DS.Model.extend({ @@ -292,6 +293,38 @@ testInDebug('JSON warns when combined with EmbeddedRecordsMixin', function(asser }, /The JSONAPISerializer does not work with the EmbeddedRecordsMixin/); }); +testInDebug('Asserts when normalized attribute key is not found in payload but original key is', function(assert) { + var jsonHash = { + data: { + type: 'users', + id: '1', + attributes: { + 'firstName': 'Yehuda' + } + } + }; + assert.expectAssertion(function() { + env.store.serializerFor("user").normalizeResponse(env.store, User, jsonHash, '1', 'findRecord'); + }, /Your payload for 'user' contains 'firstName', but your serializer is setup to look for 'first-name'/); +}); + +testInDebug('Asserts when normalized relationship key is not found in payload but original key is', function(assert) { + var jsonHash = { + data: { + type: 'users', + id: '1', + relationships: { + 'reportsTo': { + data: null + } + } + } + }; + assert.expectAssertion(function() { + env.store.serializerFor("user").normalizeResponse(env.store, User, jsonHash, '1', 'findRecord'); + }, /Your payload for 'user' contains 'reportsTo', but your serializer is setup to look for 'reports-to'/); +}); + if (isEnabled("ds-payload-type-hooks")) { test('mapping of payload type can be customized via modelNameFromPayloadType', function(assert) { env.registry.register('serializer:user', DS.JSONAPISerializer.extend({ @@ -409,7 +442,8 @@ if (isEnabled("ds-payload-type-hooks")) { handles: { serialize: true }, firstName: { serialize: false }, lastName: { serialize: false }, - title: { serialize: false } + title: { serialize: false }, + reportsTo: { serialize: false } }, payloadTypeFromModelName: function(modelName) { return `api::v1::${modelName}`; @@ -470,7 +504,8 @@ if (isEnabled("ds-payload-type-hooks")) { handles: { serialize: true }, firstName: { serialize: false }, lastName: { serialize: false }, - title: { serialize: false } + title: { serialize: false }, + reportsTo: { serialize: false } }, payloadKeyFromModelName: function(modelName) { return `api::v1::${modelName}`;