Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report better error when type is missing from a JSONApi response #3946

Merged
merged 1 commit into from Nov 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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