mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Defend against self-referencing composites
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
parent
822f4394fe
commit
db774c18d9
@ -235,6 +235,26 @@ Available parent ids are:
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("given items with same id and parent id, throws", () => {
|
||||||
|
const someItem = {
|
||||||
|
id: "some-id",
|
||||||
|
parentId: "some-id",
|
||||||
|
};
|
||||||
|
|
||||||
|
const someRoot = {
|
||||||
|
id: "root",
|
||||||
|
someParentId: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
const items = [someItem, someRoot];
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
getComposite({
|
||||||
|
source: items,
|
||||||
|
});
|
||||||
|
}).toThrow('Tried to get a composite, but found items with self as parent: "some-id"');
|
||||||
|
});
|
||||||
|
|
||||||
it("given undefined ids, throws", () => {
|
it("given undefined ids, throws", () => {
|
||||||
const root = {
|
const root = {
|
||||||
someParentId: undefined,
|
someParentId: undefined,
|
||||||
|
|||||||
@ -50,6 +50,20 @@ export default <T>({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const selfReferencingIds = pipeline(
|
||||||
|
source,
|
||||||
|
filter((x) => getId(x) === getParentId(x)),
|
||||||
|
map(getId),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (selfReferencingIds.length) {
|
||||||
|
throw new Error(
|
||||||
|
`Tried to get a composite, but found items with self as parent: "${selfReferencingIds.join(
|
||||||
|
'", ',
|
||||||
|
)}"`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const duplicateIds = pipeline(
|
const duplicateIds = pipeline(
|
||||||
source,
|
source,
|
||||||
countBy(getId),
|
countBy(getId),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user