Skip to content

Commit

Permalink
Merge pull request #4616 from wecc/json-api-key-not-found-assert
Browse files Browse the repository at this point in the history
Assert when original and not normalized key is found in payload
  • Loading branch information
bmac authored Oct 28, 2016
2 parents 905420f + 2790f8b commit b121ffc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
10 changes: 10 additions & 0 deletions addon/serializers/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,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);
}
});
});
}

Expand Down Expand Up @@ -347,6 +352,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);
}
});
});
}

Expand Down
41 changes: 38 additions & 3 deletions tests/integration/serializers/json-api-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -300,6 +301,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({
Expand Down Expand Up @@ -417,7 +450,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}`;
Expand Down Expand Up @@ -478,7 +512,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}`;
Expand Down

0 comments on commit b121ffc

Please sign in to comment.