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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Serializer.register(type, options);
* **meta** (optional): Describes meta that contains non-standard meta-information about the relationship. It can be:
* An _object_ (values can be string or function).
* A _function_ with one argument `function(data) { ... }` or with two arguments `function(data, extraData) { ... }`
* **deserialize** (optional): Describes the function which should be used to deserialize a related property which is not included in the JSON:API document. It should be:
* A _function_ with one argument `function(data) { ... }`which defines the format to which a relation should be deserialized. By default, the ID of the related object is returned, which would be equal to `function(data) {return data.id}`. See [issue #65](https://github.com/danivek/json-api-serializer/issues/65).
* **convertCase** (optional): Case conversion for serializing data. Value can be : `kebab-case`, `snake_case`, `camelCase`

**Deserialization options:**
Expand Down
11 changes: 9 additions & 2 deletions lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = class JSONAPISerializer {
schema: joi.string().default('default'),
links: joi.alternatives([joi.func(), joi.object()]).default({}),
meta: joi.alternatives([joi.func(), joi.object()]).default({}),
deserialize: joi.func(),
})).default({}),
topLevelLinks: joi.alternatives([joi.func(), joi.object()]).default({}),
topLevelMeta: joi.alternatives([joi.func(), joi.object()]).default({}),
Expand Down Expand Up @@ -487,21 +488,27 @@ module.exports = class JSONAPISerializer {
if (resourceOpts.relationships[relationshipKey] && resourceOpts.relationships[relationshipKey].alternativeKey) {
relationshipKey = resourceOpts.relationships[relationshipKey].alternativeKey;
}
const deserializeFunction = (relationshipData) => {
if (resourceOpts.relationships[relationshipKey] && resourceOpts.relationships[relationshipProperty].deserialize) {
return resourceOpts.relationships[relationshipProperty].deserialize(relationshipData);
}
return relationshipData.id;
};

if (relationship.data !== undefined) {
if (Array.isArray(relationship.data)) {
// Array data
_set(deserializedData, relationshipKey, relationship.data.map(d => (included
? this.deserializeIncluded(d.type, d.id, resourceOpts.relationships[relationshipProperty], included)
: d.id)));
: deserializeFunction(d))));
} else if (relationship.data === null) {
// null data
_set(deserializedData, relationshipKey, null);
} else {
// Object data
_set(deserializedData, relationshipKey, included
? this.deserializeIncluded(relationship.data.type, relationship.data.id, resourceOpts.relationships[relationshipProperty], included)
: relationship.data.id);
: deserializeFunction(relationship.data));
}
}
});
Expand Down
37 changes: 37 additions & 0 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,43 @@ describe('JSONAPISerializer', function() {
done();
});

it('should deserialize with \'deserialize\' option as a function', function(done) {
const Serializer = new JSONAPISerializer();
Serializer.register('articles', {
relationships: {
author: {
type: 'people',
deserialize: (data) => ({id: data.id, type: data.type})
}
}
});

const data = {
data: {
type: 'article',
id: '1',
attributes: {
title: 'JSON API paints my bikeshed!',
body: 'The shortest article. Ever.',
created: '2015-05-22T14:56:29.000Z'
},
relationships: {
author: {
data: {
type: 'people',
id: '1'
}
}
}
}
};

const deserializedData = Serializer.deserialize('articles', data);
expect(deserializedData.author).to.have.property('id');
expect(deserializedData.author).to.have.property('type');
done();
})

it('should deserialize with \'unconvertCase\' options', function(done) {
const Serializer = new JSONAPISerializer();
Serializer.register('articles', {
Expand Down