Skip to content

Commit a8f23a5

Browse files
max-konindanivek
authored andcommitted
fix: deserializing with unconvertCase option (#90)
closes #90
1 parent bbb5b4b commit a8f23a5

File tree

2 files changed

+78
-29
lines changed

2 files changed

+78
-29
lines changed

lib/JSONAPISerializer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ module.exports = class JSONAPISerializer {
418418
const deserializeFunction = relationshipData => {
419419
if (
420420
options.relationships[relationshipKey] &&
421-
options.relationships[relationshipProperty].deserialize
421+
options.relationships[relationshipKey].deserialize
422422
) {
423-
return options.relationships[relationshipProperty].deserialize(relationshipData);
423+
return options.relationships[relationshipKey].deserialize(relationshipData);
424424
}
425425
return relationshipData.id;
426426
};
@@ -435,7 +435,7 @@ module.exports = class JSONAPISerializer {
435435
? this.deserializeIncluded(
436436
d.type,
437437
d.id,
438-
options.relationships[relationshipProperty],
438+
options.relationships[relationshipKey],
439439
included
440440
)
441441
: deserializeFunction(d)
@@ -452,7 +452,7 @@ module.exports = class JSONAPISerializer {
452452
? this.deserializeIncluded(
453453
relationship.data.type,
454454
relationship.data.id,
455-
options.relationships[relationshipProperty],
455+
options.relationships[relationshipKey],
456456
included
457457
)
458458
: deserializeFunction(relationship.data)

test/unit/JSONAPISerializer.test.js

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ describe('JSONAPISerializer', function() {
400400
done();
401401
});
402402

403-
403+
404404
it('should return serialized relationship with unpopulated relationship with mongoDB BSON ObjectID', function(done) {
405405
const serializedRelationshipData = Serializer.serializeRelationship('authors', 'default', new ObjectID());
406406
expect(serializedRelationshipData).to.have.property('type').to.eql('authors');
@@ -430,7 +430,7 @@ describe('JSONAPISerializer', function() {
430430
name: 'Author 1',
431431
gender: 'male'
432432
}, included);
433-
433+
434434
included = [...included.values()];
435435
expect(serializedRelationshipData).to.have.property('type').to.eql('authors');
436436
expect(serializedRelationshipData).to.have.property('id').to.eql('1');
@@ -497,7 +497,7 @@ describe('JSONAPISerializer', function() {
497497
this.id = id;
498498
this.name = name;
499499
}
500-
}
500+
}
501501

502502
const data = new Author({
503503
id: '1',
@@ -513,7 +513,7 @@ describe('JSONAPISerializer', function() {
513513
attributes: { name: 'Kaley Maggio' },
514514
relationships: undefined,
515515
meta: undefined,
516-
links: undefined
516+
links: undefined
517517
}]);
518518
done();
519519
});
@@ -1533,8 +1533,57 @@ describe('JSONAPISerializer', function() {
15331533

15341534
it('should deserialize with \'unconvertCase\' options', function(done) {
15351535
const Serializer = new JSONAPISerializer();
1536-
Serializer.register('articles', {
1537-
unconvertCase: 'snake_case'
1536+
Serializer.register('article', {
1537+
unconvertCase: 'snake_case',
1538+
relationships: {
1539+
article_author: {
1540+
type: 'people',
1541+
},
1542+
}
1543+
});
1544+
Serializer.register('people', {
1545+
unconvertCase: 'snake_case',
1546+
});
1547+
1548+
const data = {
1549+
data: {
1550+
type: 'article',
1551+
id: '1',
1552+
attributes: {
1553+
createdAt: '2015-05-22T14:56:29.000Z'
1554+
},
1555+
relationships: {
1556+
articleAuthor: {
1557+
data: {
1558+
type: 'people',
1559+
id: '1'
1560+
}
1561+
}
1562+
}
1563+
},
1564+
included: [{
1565+
type: 'people',
1566+
id: '1',
1567+
attributes: { firstName: 'Karl' }
1568+
}]
1569+
};
1570+
1571+
const deserializedData = Serializer.deserialize('article', data);
1572+
expect(deserializedData).to.have.property('created_at');
1573+
expect(deserializedData.article_author).to.deep.equal({ id: '1', first_name: 'Karl' });
1574+
done();
1575+
});
1576+
1577+
it('should deserialize with \'unconvertCase\' and \'deserialize\' options', function(done) {
1578+
const Serializer = new JSONAPISerializer();
1579+
Serializer.register('article', {
1580+
unconvertCase: 'snake_case',
1581+
relationships: {
1582+
article_author: {
1583+
deserialize: data => ({ id: data.id, additionalProperty: `${data.type}-${data.id}` }),
1584+
type: 'people',
1585+
},
1586+
}
15381587
});
15391588

15401589
const data = {
@@ -1555,12 +1604,12 @@ describe('JSONAPISerializer', function() {
15551604
}
15561605
};
15571606

1558-
const deserializedData = Serializer.deserialize('articles', data);
1607+
const deserializedData = Serializer.deserialize('article', data);
15591608
expect(deserializedData).to.have.property('created_at');
1560-
expect(deserializedData).to.have.property('article_author');
1609+
expect(deserializedData.article_author).to.deep.equal({ id: '1', additional_property: 'people-1' });
15611610
done();
15621611
});
1563-
1612+
15641613
it('should deserialize with \'unconvertCase\' options with \'alternative_key\' relationship', function(done) {
15651614
const Serializer = new JSONAPISerializer();
15661615
Serializer.register('articles', {
@@ -1572,7 +1621,7 @@ describe('JSONAPISerializer', function() {
15721621
},
15731622
}
15741623
});
1575-
1624+
15761625
const data = {
15771626
data: {
15781627
type: 'article',
@@ -1590,7 +1639,7 @@ describe('JSONAPISerializer', function() {
15901639
}
15911640
}
15921641
};
1593-
1642+
15941643
const deserializedData = Serializer.deserialize('articles', data);
15951644
expect(deserializedData).to.have.property('created_at');
15961645
expect(deserializedData).to.have.property('article_author_id');
@@ -1731,7 +1780,7 @@ describe('JSONAPISerializer', function() {
17311780
it('should deserialize with a custom schema', function(done) {
17321781
const Serializer = new JSONAPISerializer();
17331782
Serializer.register('articles', 'custom');
1734-
1783+
17351784
const data = {
17361785
data: {
17371786
type: 'article',
@@ -1741,7 +1790,7 @@ describe('JSONAPISerializer', function() {
17411790
}
17421791
}
17431792
};
1744-
1793+
17451794
const deserializedData = Serializer.deserialize('articles', data, 'custom');
17461795
expect(deserializedData).to.have.property('createdAt');
17471796
done();
@@ -1855,7 +1904,7 @@ describe('JSONAPISerializer', function() {
18551904
it('should deserialize with a custom schema', function(done) {
18561905
const Serializer = new JSONAPISerializer();
18571906
Serializer.register('articles', 'custom');
1858-
1907+
18591908
const data = {
18601909
data: {
18611910
type: 'article',
@@ -1865,7 +1914,7 @@ describe('JSONAPISerializer', function() {
18651914
}
18661915
}
18671916
};
1868-
1917+
18691918
Serializer.deserializeAsync('articles', data, 'custom').then((deserializedData) => {
18701919
expect(deserializedData).to.have.property('createdAt');
18711920
done();
@@ -2026,7 +2075,7 @@ describe('JSONAPISerializer', function() {
20262075
expect(serializedError.errors[0]).to.have.property('status').to.eql('500');
20272076
expect(serializedError.errors[0]).to.have.property('code').to.eql('ERROR');
20282077
expect(serializedError.errors[0]).to.have.property('detail').to.eql('An error occured');
2029-
2078+
20302079
done();
20312080
});
20322081

@@ -2038,7 +2087,7 @@ describe('JSONAPISerializer', function() {
20382087
expect(serializedErrors).to.have.property('errors').to.be.instanceof(Array).to.have.lengthOf(2);
20392088
expect(serializedErrors.errors[0]).to.have.property('detail').to.eql('First Error');
20402089
expect(serializedErrors.errors[1]).to.have.property('detail').to.eql('Second Error');
2041-
2090+
20422091
done();
20432092
});
20442093

@@ -2048,7 +2097,7 @@ describe('JSONAPISerializer', function() {
20482097
expect(function() {
20492098
Serializer.serializeError(jsonapiError);
20502099
}).to.throw(Error, 'error must be an object');
2051-
2100+
20522101
done();
20532102
});
20542103

@@ -2080,7 +2129,7 @@ describe('JSONAPISerializer', function() {
20802129
expect(function() {
20812130
Serializer.serializeError(jsonapiError3);
20822131
}).to.throw(Error, 'error \'source.parameter\' property must be a string');
2083-
2132+
20842133
done();
20852134
});
20862135

@@ -2120,11 +2169,11 @@ describe('JSONAPISerializer', function() {
21202169
expect(function() {
21212170
Serializer.serializeError(jsonapiErrorBadLink3);
21222171
}).to.throw(Error, '\'links.self.meta\' property must be an object');
2123-
2172+
21242173
expect(function() {
21252174
Serializer.serializeError(jsonapiErrorBadLink4);
21262175
}).to.throw(Error, 'error \'links.self\' must be a string or an object');
2127-
2176+
21282177
done();
21292178
});
21302179

@@ -2148,7 +2197,7 @@ describe('JSONAPISerializer', function() {
21482197

21492198
expect(serializedError).to.have.property('errors').to.be.instanceof(Array).to.have.lengthOf(1);
21502199
expect(serializedError.errors[0]).to.deep.eql(jsonapiError);
2151-
2200+
21522201
done();
21532202
});
21542203

@@ -2179,7 +2228,7 @@ describe('JSONAPISerializer', function() {
21792228
title: 'Second Error',
21802229
detail: 'Second Error'
21812230
}]);
2182-
2231+
21832232
done();
21842233
});
21852234
})
@@ -2200,7 +2249,7 @@ describe('JSONAPISerializer', function() {
22002249
}],
22012250
'kebab-case'
22022251
);
2203-
2252+
22042253
expect(converted).to.deep.equal([{
22052254
'array-of-object': [{ 'first-property': 'test', 'second-property': null, 'third-property': 0 }],
22062255
'array-of-number': [1, 2, 3, 4, 5]
@@ -2223,7 +2272,7 @@ describe('JSONAPISerializer', function() {
22232272
},
22242273
'kebab-case'
22252274
);
2226-
2275+
22272276
expect(converted['array-of-object']).to.deep.equal([{ 'first-property': 'test', 'second-property': null, 'third-property': 0 }]);
22282277
expect(converted['array-of-number']).to.deep.equal([1, 2, 3, 4, 5]);
22292278
expect(converted.date).to.be.a('Date');

0 commit comments

Comments
 (0)