Skip to content

Commit

Permalink
fix deserialize with unconvertCase option
Browse files Browse the repository at this point in the history
  • Loading branch information
danivek committed Oct 3, 2019
1 parent 73e93cf commit 8fddac2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ module.exports = class JSONAPISerializer {
? this.deserializeIncluded(
d.type,
d.id,
options.relationships[relationshipProperty],
options.relationships[relationshipKey],
included
)
: deserializeFunction(d)
Expand All @@ -452,7 +452,7 @@ module.exports = class JSONAPISerializer {
? this.deserializeIncluded(
relationship.data.type,
relationship.data.id,
options.relationships[relationshipProperty],
options.relationships[relationshipKey],
included
)
: deserializeFunction(relationship.data)
Expand Down
33 changes: 23 additions & 10 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,15 @@ describe('JSONAPISerializer', function() {

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

Expand All @@ -1552,23 +1560,28 @@ describe('JSONAPISerializer', function() {
}
}
}
}
},
included: [{
type: 'people',
id: '1',
attributes: { firstName: 'Karl' }
}]
};

const deserializedData = Serializer.deserialize('articles', data);
const deserializedData = Serializer.deserialize('article', data);
expect(deserializedData).to.have.property('created_at');
expect(deserializedData).to.have.property('article_author');
expect(deserializedData.article_author).to.deep.equal({ id: '1', first_name: 'Karl' });
done();
});

it('should deserialize with \'unconvertCase\' and \'deserialize\' options', function(done) {
const Serializer = new JSONAPISerializer();
Serializer.register('articles', {
Serializer.register('article', {
unconvertCase: 'snake_case',
relationships: {
article_author: {
deserialize: data => data.attributes,
type: 'authors',
deserialize: data => ({ id: data.id, additionalProperty: `${data.type}-${data.id}` }),
type: 'people',
},
}
});
Expand All @@ -1584,16 +1597,16 @@ describe('JSONAPISerializer', function() {
articleAuthor: {
data: {
type: 'people',
attributes: { firstName: 'Karl' }
id: '1'
}
}
}
}
};

const deserializedData = Serializer.deserialize('articles', data);
const deserializedData = Serializer.deserialize('article', data);
expect(deserializedData).to.have.property('created_at');
expect(deserializedData.article_author).to.deep.equal({ first_name: 'Karl' });
expect(deserializedData.article_author).to.deep.equal({ id: '1', additional_property: 'people-1' });
done();
});

Expand Down

0 comments on commit 8fddac2

Please sign in to comment.