Skip to content

Commit

Permalink
[BUGFIX release] Normalize attrs keys
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmanuel committed Nov 6, 2015
1 parent f1ccad8 commit 7a9cd27
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
20 changes: 16 additions & 4 deletions packages/ember-data/lib/serializers/json-api-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,30 @@ const JSONAPISerializer = JSONSerializer.extend({
@param {Object} attribute
*/
serializeAttribute: function(snapshot, json, key, attribute) {
var type = attribute.type;
// TODO deprecation if key contains -

const type = attribute.type;

if (this._canSerialize(key)) {
json.attributes = json.attributes || {};

var value = snapshot.attr(key);
let value = snapshot.attr(key);
if (type) {
var transform = this.transformFor(type);
const transform = this.transformFor(type);
value = transform.serialize(value);
}

var payloadKey = this._getMappedKey(key);
let payloadKey;

const normalizedKey = this.keyForAttribute(key);
const normalizedPayloadKey = this._getMappedKey(normalizedKey);

if (normalizedKey !== normalizedPayloadKey) {
payloadKey = normalizedPayloadKey;
} else {
payloadKey = this._getMappedKey(key);
}

if (payloadKey === key) {
payloadKey = this.keyForAttribute(key, 'serialize');
}
Expand Down
9 changes: 6 additions & 3 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,15 +712,18 @@ export default Serializer.extend({
*/
normalizeUsingDeclaredMapping: function(typeClass, hash) {
var attrs = get(this, 'attrs');
var payloadKey, key;
var payloadKey, normalizedKey, key;

if (attrs) {
for (key in attrs) {
// TODO deprecation if key contains -

payloadKey = this._getMappedKey(key);
normalizedKey = this.keyForAttribute(key);
if (!hash.hasOwnProperty(payloadKey)) { continue; }

if (payloadKey !== key) {
hash[key] = hash[payloadKey];
if (payloadKey !== normalizedKey) {
hash[normalizedKey] = hash[payloadKey];
delete hash[payloadKey];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ test('Warns when normalizing an unknown type', function() {
test('Serializer should respect the attrs hash when extracting attributes and relationships', function() {
env.registry.register("serializer:user", DS.JSONAPISerializer.extend({
attrs: {
firstName: 'firstname_attribute_key',
'last-name': 'lastname_attribute_key',
title: "title_attribute_key",
company: { key: 'company_relationship_key' }
}
Expand All @@ -135,6 +137,8 @@ test('Serializer should respect the attrs hash when extracting attributes and re
type: 'users',
id: '1',
attributes: {
'firstname_attribute_key': 'Yehuda',
'lastname_attribute_key': 'Katz',
'title_attribute_key': 'director'
},
relationships: {
Expand All @@ -154,13 +158,17 @@ test('Serializer should respect the attrs hash when extracting attributes and re

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

equal(user.data.attributes.firstName, 'Yehuda');
equal(user.data.attributes.lastName, 'Katz');
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: {
firstName: 'firstname_attribute_key',
'last-name': 'lastname_attribute_key',
title: "title_attribute_key",
company: { key: 'company_relationship_key' }
}
Expand All @@ -178,11 +186,13 @@ test('Serializer should respect the attrs hash when serializing attributes and r
}
});
company = env.store.peekRecord('company', 1);
user = env.store.createRecord('user', { firstName: "Yehuda", title: "director", company: company });
user = env.store.createRecord('user', { firstName: "Yehuda", lastName: 'Katz', 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['firstname_attribute_key'], 'Yehuda');
equal(payload.data.attributes['lastname_attribute_key'], 'Katz');
equal(payload.data.attributes['title_attribute_key'], "director");
});
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,30 @@ test('Serializer should respect the attrs hash when extracting records', functio
deepEqual(post.data.relationships.comments.data, [{ id: "1", type: "comment" }, { id: "2", type: "comment" }]);
});

test('Serializer should map `attrs` attributes directly when keyForAttribute also has a transform', function() {
Post = DS.Model.extend({
authorName: DS.attr('string')
});
env = setupStore({
post: Post
});
env.registry.register("serializer:post", DS.JSONSerializer.extend({
keyForAttribute: Ember.String.underscore,
attrs: {
authorName: 'author_name_key'
}
}));

var jsonHash = {
id: "1",
author_name_key: "DHH"
};

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

equal(post.data.attributes.authorName, "DHH");
});

test('Serializer should respect the attrs hash when serializing records', function() {
Post.reopen({
parentPost: DS.belongsTo('post', { inverse: null, async: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,28 @@ test('normalize should allow for different levels of normalization', function()
equal(array.data[0].relationships.superVillain.data.id, 1);
});

test('normalize should allow for different levels of normalization - attributes', function() {
env.registry.register('serializer:application', DS.RESTSerializer.extend({
attrs: {
name: 'full_name'
},
keyForAttribute: function(attr) {
return Ember.String.decamelize(attr);
}
}));

var jsonHash = {
evilMinions: [{ id: "1", full_name: "Tom Dale" }]
};
var array;

run(function() {
array = env.restSerializer.normalizeResponse(env.store, EvilMinion, jsonHash, null, 'findAll');
});

equal(array.data[0].attributes.name, 'Tom Dale');
});

test("serializeIntoHash", function() {
run(function() {
league = env.store.createRecord('home-planet', { name: "Umber", id: "123" });
Expand Down

0 comments on commit 7a9cd27

Please sign in to comment.