Skip to content

Commit e3ba445

Browse files
committed
Fix nested Attributes are not dasherized. Fix #19.
1 parent c3e8fb0 commit e3ba445

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

lib/serializer-utils.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ module.exports = function (collectionName, record, payload, opts) {
99
}
1010

1111
function keyForAttribute(attribute) {
12-
if (_.isFunction(opts.keyForAttribute)) {
13-
return opts.keyForAttribute(attribute);
12+
if (_.isPlainObject(attribute)) {
13+
return _.mapKeys(attribute, function (value, key) {
14+
return keyForAttribute(key);
15+
});
16+
} else if (_.isArray(attribute)) {
17+
return attribute.map(function (attr) {
18+
return keyForAttribute(attr);
19+
});
1420
} else {
15-
return dasherize(attribute);
21+
if (_.isFunction(opts.keyForAttribute)) {
22+
return opts.keyForAttribute(attribute);
23+
} else {
24+
return dasherize(attribute);
25+
}
1626
}
1727
}
1828

@@ -99,7 +109,8 @@ module.exports = function (collectionName, record, payload, opts) {
99109
}
100110
} else {
101111
// Embedded without relationships.
102-
dest.attributes[keyForAttribute(attribute)] = current[attribute];
112+
dest.attributes[keyForAttribute(attribute)] =
113+
keyForAttribute(current[attribute]);
103114
}
104115
} else {
105116
dest.attributes[keyForAttribute(attribute)] = current[attribute];

test/serializer.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,14 @@ describe('Options', function () {
123123
id: '1',
124124
firstName: 'Sandro',
125125
lastName: 'Munda',
126+
books: [{ createdAt: '2015-08-04T06:09:24.864Z' }],
127+
address: { zipCode: 42912 }
126128
};
127129

128130
var json = new JsonApiSerializer('user', dataSet, {
129-
attributes: ['firstName', 'lastName'],
131+
attributes: ['firstName', 'lastName', 'books', 'address'],
132+
books: { attributes: ['createdAt'] },
133+
address: { attributes: ['zipCode'] },
130134
pluralizeType: false,
131135
keyForAttribute: function (attribute) {
132136
return inflection.underscore(attribute);
@@ -136,7 +140,14 @@ describe('Options', function () {
136140
expect(json.data.type).equal('user');
137141
expect(json.data).to.have.property('attributes').that.is
138142
.an('object')
139-
.eql({ 'first_name': 'Sandro', 'last_name': 'Munda' });
143+
.eql({
144+
'first_name': 'Sandro',
145+
'last_name': 'Munda',
146+
books: [{ 'created_at': '2015-08-04T06:09:24.864Z' }],
147+
address: { 'zip_code': 42912 }
148+
});
149+
150+
console.log(require('util').inspect(json, { depth: null }));
140151

141152
done(null, json);
142153
});
@@ -307,8 +318,8 @@ describe('JSON API Serializer', function () {
307318
expect(json.data[0].attributes).to.have.property('address')
308319
.that.is.an('object')
309320
.eql({
310-
addressLine1: '406 Madison Court',
311-
zipCode: '49426',
321+
'address-line1': '406 Madison Court',
322+
'zip-code': '49426',
312323
country: 'USA'
313324
});
314325

0 commit comments

Comments
 (0)