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

Assert when original and not normalized key is found in payload #4616

Merged
merged 1 commit into from
Oct 28, 2016
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
10 changes: 10 additions & 0 deletions addon/serializers/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,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 @@ -314,6 +319,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 @@ -292,6 +293,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 @@ -409,7 +442,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 @@ -470,7 +504,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