Skip to content

Commit

Permalink
Merge pull request #3946 from martndemus/better-error-undefined-type
Browse files Browse the repository at this point in the history
Report better error when `type` is missing from a JSONApi response
  • Loading branch information
bmac committed Nov 24, 2015
2 parents 0a1d7f3 + 8d0230c commit 845d969
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions addon/serializers/json-api-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 + '"))';
}
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/serializers/json-api-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 845d969

Please sign in to comment.