Skip to content

Commit

Permalink
Merge pull request #15254 from strapi/fix/relational-path
Browse files Browse the repository at this point in the history
[fix] Relation path setup
  • Loading branch information
ronronscelestes committed Dec 27, 2022
2 parents b767d40 + e5e2018 commit b76c413
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Expand Up @@ -24,7 +24,8 @@ export const findLeafByPathAndReplace = (endpath, replaceWith) => {
* and the current path is not undefined in the accumulator
* then we assume it's a leaf and we can replace it.
*/
if (endpath === curr && acc[curr] !== undefined) {

if (ind === currentArr.length - 1 && endpath === curr && acc[curr] !== undefined) {
set(acc, curr, replaceWith);

return acc;
Expand Down
Expand Up @@ -122,4 +122,34 @@ describe('findLeafByPathAndReplace', () => {
},
});
});

it('should only replace the leaf if it is the last item in the array of paths', () => {
const obj = {
a: {
a: [
{
a: 'd',
},
],
},
};

const path = ['a', 'a', 'a'];

const [lastPath] = path.slice(-1);

const findLeaf = findLeafByPathAndReplace(lastPath, []);

path.reduce(findLeaf, obj);

expect(obj).toMatchObject({
a: {
a: [
{
a: [],
},
],
},
});
});
});

0 comments on commit b76c413

Please sign in to comment.