Skip to content

Commit f6f86e6

Browse files
authored
normalization in json-api serializer preserves lid #7956 (#9246)
1 parent 64fa97f commit f6f86e6

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

packages/serializer/src/json-api.js

+4
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ const JSONAPISerializer = JSONSerializer.extend({
395395
relationships: this.extractRelationships(modelClass, resourceHash),
396396
};
397397

398+
if (resourceHash.lid) {
399+
data.lid = resourceHash.lid;
400+
}
401+
398402
this.applyTransforms(modelClass, data.attributes);
399403

400404
return { data };

tests/main/tests/integration/serializers/json-api-serializer-test.js

+61
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,67 @@ module('integration/serializers/json-api-serializer - JSONAPISerializer', functi
289289
assert.deepEqual(user.data.relationships.company.data, { id: '2', type: 'company' });
290290
});
291291

292+
test('Serializer should preserve lid in payloads', function (assert) {
293+
const store = this.owner.lookup('service:store');
294+
295+
var jsonHash = {
296+
data: {
297+
type: 'users',
298+
id: '1',
299+
lid: 'user-1',
300+
attributes: {
301+
firstname_attribute_key: 'Yehuda',
302+
title_attribute_key: 'director',
303+
},
304+
relationships: {
305+
company: {
306+
data: { type: 'companies', id: '2', lid: 'company-2' },
307+
},
308+
handles: {
309+
data: [
310+
{ type: 'github-handles', id: '3' },
311+
{ type: 'twitter-handles', id: '4', lid: 'handle-4' },
312+
],
313+
},
314+
},
315+
included: [
316+
{
317+
type: 'companies',
318+
id: '2',
319+
lid: 'company-2',
320+
attributes: {
321+
name: 'Tilde Inc.',
322+
},
323+
},
324+
{
325+
type: 'github-handles',
326+
id: '3',
327+
attributes: {
328+
username: 'wycats',
329+
},
330+
},
331+
{
332+
type: 'twitter-handles',
333+
id: '4',
334+
lid: 'handle-4',
335+
attributes: {
336+
nickname: '@wycats',
337+
},
338+
},
339+
],
340+
},
341+
};
342+
343+
var user = store.serializerFor('user').normalizeResponse(store, User, jsonHash, '1', 'createRecord');
344+
345+
assert.strictEqual(user.data.lid, 'user-1');
346+
assert.deepEqual(user.data.relationships.company.data, { type: 'company', id: '2', lid: 'company-2' });
347+
assert.deepEqual(user.data.relationships.handles.data, [
348+
{ type: 'github-handle', id: '3' },
349+
{ type: 'twitter-handle', id: '4', lid: 'handle-4' },
350+
]);
351+
});
352+
292353
test('Serializer should respect the attrs hash when serializing attributes and relationships', function (assert) {
293354
this.owner.register(
294355
'serializer:user',

0 commit comments

Comments
 (0)