From 5a294647d2101f862e7cca68113172a75235cc3b Mon Sep 17 00:00:00 2001 From: sly7-7 Date: Tue, 5 Mar 2024 15:45:56 +0100 Subject: [PATCH] normalization in json-api serializer preserves lid #7956 --- packages/serializer/src/json-api.js | 4 ++ .../serializers/json-api-serializer-test.js | 61 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/packages/serializer/src/json-api.js b/packages/serializer/src/json-api.js index 05e730e7db8..42d018a2232 100644 --- a/packages/serializer/src/json-api.js +++ b/packages/serializer/src/json-api.js @@ -395,6 +395,10 @@ const JSONAPISerializer = JSONSerializer.extend({ relationships: this.extractRelationships(modelClass, resourceHash), }; + if (resourceHash.lid) { + data.lid = resourceHash.lid; + } + this.applyTransforms(modelClass, data.attributes); return { data }; diff --git a/tests/main/tests/integration/serializers/json-api-serializer-test.js b/tests/main/tests/integration/serializers/json-api-serializer-test.js index 616ea7727c4..e2a644f3d10 100644 --- a/tests/main/tests/integration/serializers/json-api-serializer-test.js +++ b/tests/main/tests/integration/serializers/json-api-serializer-test.js @@ -289,6 +289,67 @@ module('integration/serializers/json-api-serializer - JSONAPISerializer', functi assert.deepEqual(user.data.relationships.company.data, { id: '2', type: 'company' }); }); + test('Serializer should preserve lid in payloads', function (assert) { + const store = this.owner.lookup('service:store'); + + var jsonHash = { + data: { + type: 'users', + id: '1', + lid: 'user-1', + attributes: { + firstname_attribute_key: 'Yehuda', + title_attribute_key: 'director', + }, + relationships: { + company: { + data: { type: 'companies', id: '2', lid: 'company-2' }, + }, + handles: { + data: [ + { type: 'github-handles', id: '3' }, + { type: 'twitter-handles', id: '4', lid: 'handle-4' }, + ], + }, + }, + included: [ + { + type: 'companies', + id: '2', + lid: 'company-2', + attributes: { + name: 'Tilde Inc.', + }, + }, + { + type: 'github-handles', + id: '3', + attributes: { + username: 'wycats', + }, + }, + { + type: 'twitter-handles', + id: '4', + lid: 'handle-4', + attributes: { + nickname: '@wycats', + }, + }, + ], + }, + }; + + var user = store.serializerFor('user').normalizeResponse(store, User, jsonHash, '1', 'createRecord'); + + assert.strictEqual(user.data.lid, 'user-1'); + assert.deepEqual(user.data.relationships.company.data, { type: 'company', id: '2', lid: 'company-2' }); + assert.deepEqual(user.data.relationships.handles.data, [ + { type: 'github-handle', id: '3' }, + { type: 'twitter-handle', id: '4', lid: 'handle-4' }, + ]); + }); + test('Serializer should respect the attrs hash when serializing attributes and relationships', function (assert) { this.owner.register( 'serializer:user',