Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
coverage/
.idea/
7 changes: 6 additions & 1 deletion lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,12 @@ module.exports = class JSONAPISerializer {
const relationship = data.relationships[relationshipProperty];

// Support alternativeKey options for relationships
let relationshipKey = relationshipProperty;
let relationshipKey;
if (resourceOpts.unconvertCase) {
relationshipKey = this._convertCase(relationshipProperty, resourceOpts.unconvertCase);
} else {
relationshipKey = relationshipProperty;
}
if (resourceOpts.relationships[relationshipKey] && resourceOpts.relationships[relationshipKey].alternativeKey) {
relationshipKey = resourceOpts.relationships[relationshipKey].alternativeKey;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-api-serializer",
"version": "1.15.0",
"version": "1.15.1",
"description": "Framework agnostic JSON API serializer.",
"main": "index.js",
"scripts": {
Expand Down
36 changes: 36 additions & 0 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,42 @@ describe('JSONAPISerializer', function() {
expect(deserializedData).to.have.property('article_author');
done();
});

it('should deserialize with \'unconvertCase\' options with \'alternative_key\' relationship', function(done) {
const Serializer = new JSONAPISerializer();
Serializer.register('articles', {
unconvertCase: 'snake_case',
relationships: {
article_author: {
alternativeKey: 'article_author_id',
type: 'authors',
},
}
});

const data = {
data: {
type: 'article',
id: '1',
attributes: {
createdAt: '2015-05-22T14:56:29.000Z'
},
relationships: {
articleAuthor: {
data: {
type: 'people',
id: '1'
}
}
}
}
};

const deserializedData = Serializer.deserialize('articles', data);
expect(deserializedData).to.have.property('created_at');
expect(deserializedData).to.have.property('article_author_id');
done();
});

it('should deserialize all attributes of data except for blacklisted attributes', function(done) {
const data = {
Expand Down