From bdca8e68b4633265e07dba01bbcec25239021c77 Mon Sep 17 00:00:00 2001 From: Carlo Beltrame Date: Mon, 18 Oct 2021 23:45:19 +0200 Subject: [PATCH] Fix overly eager detection of embedded standalone collections --- src/normalize.js | 2 +- test/normalize.spec.js | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/normalize.js b/src/normalize.js index 82ba2a9..5cb05de 100644 --- a/src/normalize.js +++ b/src/normalize.js @@ -99,7 +99,7 @@ function mergeEmbeddedStandaloneCollections(embedded, links, opts) { keys(embedded).forEach((uri) => { keys(embedded[uri]).forEach((rel) => { - if (uri in links && rel in links[uri]) { + if (Array.isArray(embedded[uri][rel]) && uri in links && rel in links[uri]) { ret[uri][rel] = links[uri][rel]; ret[links[uri][rel].href] = { [opts.embeddedStandaloneListKey]: embedded[uri][rel], diff --git a/test/normalize.spec.js b/test/normalize.spec.js index 6ed6d17..25e917a 100644 --- a/test/normalize.spec.js +++ b/test/normalize.spec.js @@ -1525,6 +1525,75 @@ describe('complex', () => { expect(result).to.deep.eql(output); }); + + it('can handle embedded non-collection entities with additional link', () => { + const json3 = { + _links: { + self: { + href: '/camp_collaborations/5f8a9480c7a7', + }, + user: { + href: '/users/16fbb7665f7c', + }, + camp: { + href: '/camps/562c83359b44', + }, + }, + _embedded: { + camp: { + _links: { + self: { + href: '/camps/562c83359b44', + }, + }, + title: 'enim', + }, + user: { + _links: { + self: { + href: '/users/16fbb7665f7c', + }, + }, + displayName: 'voluptates', + }, + }, + id: '5f8a9480c7a7', + }; + + const output3 = { + '/camp_collaborations/5f8a9480c7a7': { + id: '5f8a9480c7a7', + user: { + href: '/users/16fbb7665f7c', + }, + camp: { + href: '/camps/562c83359b44', + }, + _meta: { + self: '/camp_collaborations/5f8a9480c7a7', + }, + }, + '/camps/562c83359b44': { + _meta: { + self: '/camps/562c83359b44', + }, + title: 'enim', + }, + '/users/16fbb7665f7c': { + _meta: { + self: '/users/16fbb7665f7c', + }, + displayName: 'voluptates', + }, + }; + + const result = normalize(json3, { + camelizeKeys: false, + embeddedStandaloneListKey: 'items', + }); + + expect(result).to.deep.eql(output3); + }); }); describe('base URI removal', () => {