Skip to content

Commit

Permalink
Merge pull request #3847 from weinberg/pr-3845-squash
Browse files Browse the repository at this point in the history
[BUGFIX beta] JSONAPI serializer not respecting 'attrs' hash
  • Loading branch information
bmac committed Oct 13, 2015
2 parents d583b31 + 1027b39 commit 5e6820d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/ember-data/lib/serializers/json-api-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,13 @@ const JSONAPISerializer = JSONSerializer.extend({
@return {Object} the normalized resource hash
*/
normalize: function(modelClass, resourceHash) {
this.normalizeUsingDeclaredMapping(modelClass, resourceHash);
if (resourceHash.attributes) {
this.normalizeUsingDeclaredMapping(modelClass, resourceHash.attributes);
}

if (resourceHash.relationships) {
this.normalizeUsingDeclaredMapping(modelClass, resourceHash.relationships);
}

let data = {
id: this.extractId(modelClass, resourceHash),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module('integration/serializers/json-api-serializer - JSONAPISerializer', {
User = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
title: DS.attr('string'),
handles: DS.hasMany('handle', { async: true, polymorphic: true }),
company: DS.belongsTo('company', { async: true })
});
Expand Down Expand Up @@ -120,3 +121,68 @@ test('Warns when normalizing an unknown type', function() {
});
}, /Encountered a resource object with type "UnknownType", but no model was found for model name "unknown-type"/);
});

test('Serializer should respect the attrs hash when extracting attributes and relationships', function() {
env.registry.register("serializer:user", DS.JSONAPISerializer.extend({
attrs: {
title: "title_attribute_key",
company: { key: 'company_relationship_key' }
}
}));

var jsonHash = {
data: {
type: 'users',
id: '1',
attributes: {
'title_attribute_key': 'director'
},
relationships: {
'company_relationship_key': {
data: { type: 'companies', id: '2' }
}
}
},
included: [{
type: 'companies',
id: '2',
attributes: {
name: 'Tilde Inc.'
}
}]
};

var user = env.store.serializerFor("user").normalizeResponse(env.store, User, jsonHash, '1', 'findRecord');

equal(user.data.attributes.title, "director");
deepEqual(user.data.relationships.company.data, { id: "2", type: "company" });
});

test('Serializer should respect the attrs hash when serializing attributes and relationships', function() {
env.registry.register("serializer:user", DS.JSONAPISerializer.extend({
attrs: {
title: "title_attribute_key",
company: { key: 'company_relationship_key' }
}
}));
var company, user;

run(function() {
env.store.push({
data: {
type: 'company',
id: '1',
attributes: {
name: "Tilde Inc."
}
}
});
company = env.store.peekRecord('company', 1);
user = env.store.createRecord('user', { firstName: "Yehuda", title: "director", company: company });
});

var payload = env.store.serializerFor("user").serialize(user._createSnapshot());

equal(payload.data.relationships['company_relationship_key'].data.id, "1");
equal(payload.data.attributes['title_attribute_key'], "director");
});

0 comments on commit 5e6820d

Please sign in to comment.