From 9ecc31fdcad3c18301bda12dfd1e086e2c1c60f4 Mon Sep 17 00:00:00 2001 From: borealisalastor Date: Thu, 19 Nov 2026 20:05:58 +0800 Subject: [PATCH] fix(utils): keep resource rows without a parent in the tree #1479 --- backend/common/utils/tree_utils.py | 15 +++++-- backend/tests/test_resource_tree_roots.py | 51 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 backend/tests/test_resource_tree_roots.py diff --git a/backend/common/utils/tree_utils.py b/backend/common/utils/tree_utils.py index 8398d65b1..c2a297e11 100644 --- a/backend/common/utils/tree_utils.py +++ b/backend/common/utils/tree_utils.py @@ -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 \ No newline at end of file diff --git a/backend/tests/test_resource_tree_roots.py b/backend/tests/test_resource_tree_roots.py new file mode 100644 index 000000000..f1b8a6c67 --- /dev/null +++ b/backend/tests/test_resource_tree_roots.py @@ -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"] \ No newline at end of file