Skip to content

Commit

Permalink
Fix overly eager detection of embedded standalone collections
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobeltrame committed Oct 18, 2021
1 parent c9715b5 commit bdca8e6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/normalize.js
Expand Up @@ -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],
Expand Down
69 changes: 69 additions & 0 deletions test/normalize.spec.js
Expand Up @@ -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', () => {
Expand Down

0 comments on commit bdca8e6

Please sign in to comment.