Skip to content

Commit

Permalink
fix(db): Do not allow creating objects with wrong parent
Browse files Browse the repository at this point in the history
If parent is set to same id as object it might cause loops
in some of pipeline programs. Block such updates.

Signed-off-by: Denys Fedoryshchenko <[email protected]>
  • Loading branch information
nuclearcat committed Sep 23, 2024
1 parent 146bd7a commit 07bb3cb
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ async def _create_recursively(self, hierarchy: Hierarchy, parent: Node,
obj.parent = parent.id
if obj.id:
obj.update()
if obj.parent == obj.id:
raise ValueError("Parent cannot be the same as the object")
res = await col.replace_one(
{'_id': ObjectId(obj.id)}, obj.dict(by_alias=True)
)
Expand Down Expand Up @@ -214,6 +216,8 @@ async def update(self, obj):
raise ValueError("Cannot update object with no id")
col = self._get_collection(obj.__class__)
obj.update()
if obj.parent == obj.id:
raise ValueError("Parent cannot be the same as the object")
res = await col.replace_one(
{'_id': ObjectId(obj.id)}, obj.dict(by_alias=True)
)
Expand Down

0 comments on commit 07bb3cb

Please sign in to comment.