diff --git a/addon/serializers/json-api-serializer.js b/addon/serializers/json-api-serializer.js index b398ebeefaa..8b53ea568c5 100644 --- a/addon/serializers/json-api-serializer.js +++ b/addon/serializers/json-api-serializer.js @@ -136,6 +136,10 @@ const JSONAPISerializer = JSONSerializer.extend({ @private */ _normalizeResourceHelper: function(resourceHash) { + Ember.assert(this.warnMessageForUndefinedType(), !Ember.isNone(resourceHash.type), { + id: 'ds.serializer.type-is-undefined' + }); + let modelName = this.modelNameFromPayloadKey(resourceHash.type); if (!this.store._hasModelFor(modelName)) { @@ -465,6 +469,9 @@ const JSONAPISerializer = JSONSerializer.extend({ Ember.runInDebug(function() { JSONAPISerializer.reopen({ + warnMessageForUndefinedType: function() { + return 'Encountered a resource object with an undefined type (resolved resource using ' + this.constructor.toString() + ')'; + }, warnMessageNoModelForType: function(modelName, originalType) { return 'Encountered a resource object with type "' + originalType + '", but no model was found for model name "' + modelName + '" (resolved model name using ' + this.constructor.toString() + '.modelNameFromPayloadKey("' + originalType + '"))'; } diff --git a/tests/integration/serializers/json-api-serializer-test.js b/tests/integration/serializers/json-api-serializer-test.js index 7536f865bce..6d8af4f3954 100644 --- a/tests/integration/serializers/json-api-serializer-test.js +++ b/tests/integration/serializers/json-api-serializer-test.js @@ -134,6 +134,23 @@ test('Warns when normalizing an unknown type', function(assert) { }, /Encountered a resource object with type "UnknownType", but no model was found for model name "unknown-type"/); }); +test('Warns when normalizing with type missing', function(assert) { + var documentHash = { + data: { + id: '1', + attributes: { + foo: 'bar' + } + } + }; + + assert.throws(function() { + run(function() { + env.store.serializerFor('user').normalizeResponse(env.store, User, documentHash, '1', 'findRecord'); + }); + }, /Encountered a resource object with an undefined type/); +}); + test('Serializer should respect the attrs hash when extracting attributes and relationships', function(assert) { env.registry.register("serializer:user", DS.JSONAPISerializer.extend({ attrs: {