Skip to content

Commit

Permalink
Merge pull request #3763 from wecc/json-api-serializer-unknown-type-w…
Browse files Browse the repository at this point in the history
…arning

[BUGFIX beta] JSONAPISerializer warns for unknown type
  • Loading branch information
fivetanley committed Sep 13, 2015
2 parents 5e14d74 + 1269b59 commit 0dc4565
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/ember-data/lib/serializers/json-api-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var dasherize = Ember.String.dasherize;
@namespace DS
@extends DS.JSONSerializer
*/
export default JSONSerializer.extend({
const JSONAPISerializer = JSONSerializer.extend({

/**
@method _normalizeDocumentHelper
Expand Down Expand Up @@ -137,6 +137,14 @@ export default JSONSerializer.extend({
*/
_normalizeResourceHelper: function(resourceHash) {
let modelName = this.modelNameFromPayloadKey(resourceHash.type);

if (!this.store._hasModelFor(modelName)) {
Ember.warn(this.warnMessageNoModelForType(modelName, resourceHash.type), false, {
id: 'ds.serializer.model-for-type-missing'
});
return null;
}

let modelClass = this.store.modelFor(modelName);
let serializer = this.store.serializerFor(modelName);
let { data } = serializer.normalize(modelClass, resourceHash);
Expand Down Expand Up @@ -448,3 +456,12 @@ export default JSONSerializer.extend({
}
});

Ember.runInDebug(function() {
JSONAPISerializer.reopen({
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 + '"))';
}
});
});

export default JSONAPISerializer;
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,21 @@ test('Calling pushPayload works', function() {
equal(get(user, 'handles.lastObject.nickname'), '@wycats', 'handles.lastObject.nickname is correct');
});
});

test('Warns when normalizing an unknown type', function() {
var documentHash = {
data: {
type: 'UnknownType',
id: '1',
attributes: {
foo: 'bar'
}
}
};

warns(function() {
run(function() {
env.store.serializerFor('user').normalizeResponse(env.store, User, documentHash, '1', 'findRecord');
});
}, /Encountered a resource object with type "UnknownType", but no model was found for model name "unknown-type"/);
});

0 comments on commit 0dc4565

Please sign in to comment.