Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions backend/common/utils/tree_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ def build_tree_generic(nodes: List[T], root_pid: Any = None) -> List[T]:
tree: List[T] = []

for node in nodes:
if node.pid == root_pid:
pid = node.pid
if node.id is not None and pid == node.id:
# A self-parented row stays visible, but must never become its own
# child: serializing the result would recurse until it fails.
tree.append(node)
elif node.pid in node_dict:
node_dict[node.pid].children.append(node)
continue
# Callers pass a sentinel such as "root", while freshly created rows keep
# an empty pid and older rows can store NULL. Both describe "no parent"
# and used to be dropped from the tree entirely.
if pid == root_pid or pid is None or pid == "":
tree.append(node)
elif pid in node_dict:
node_dict[pid].children.append(node)

return tree
51 changes: 51 additions & 0 deletions backend/tests/test_resource_tree_roots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Guard: resource trees keep rows whose parent is unset.

``list_resource`` builds the dashboard tree with ``root_pid="root"``, but
``CreateDashboard`` defaults ``pid`` to an empty string and rows created earlier
can store NULL. ``build_tree_generic`` only accepted the exact ``root_pid`` as a
root and ignored empty parents, so those rows disappeared from the list.
A row that points at itself additionally became its own child, which makes any
recursive serialization of the result fail.
"""

from pydantic import BaseModel, Field

from common.utils.tree_utils import build_tree_generic


class Node(BaseModel):
id: str
pid: str | None
children: list["Node"] = Field(default_factory=list)


def _nodes(*rows):
return [Node(id=node_id, pid=pid) for node_id, pid in rows]


def test_rows_without_parent_id_are_roots():
tree = build_tree_generic(_nodes(("a", "root"), ("b", ""), ("c", None)), root_pid="root")

assert [node.id for node in tree] == ["a", "b", "c"]


def test_children_attach_to_their_parent():
tree = build_tree_generic(_nodes(("a", "root"), ("b", "a"), ("c", "b")), root_pid="root")

assert [node.id for node in tree] == ["a"]
assert [node.id for node in tree[0].children] == ["b"]
assert [node.id for node in tree[0].children[0].children] == ["c"]


def test_self_parented_row_is_not_its_own_child():
tree = build_tree_generic(_nodes(("a", "root"), ("x", "x")), root_pid="root")

assert [node.id for node in tree] == ["a", "x"]
assert tree[0].children == []
assert tree[1].children == []


def test_rows_with_unknown_parent_stay_out_of_the_tree():
tree = build_tree_generic(_nodes(("a", "root"), ("b", "missing")), root_pid="root")

assert [node.id for node in tree] == ["a"]
Loading