diff --git a/jsonpath_rfc9535/segments.py b/jsonpath_rfc9535/segments.py index 057a76b..2e0f7e5 100644 --- a/jsonpath_rfc9535/segments.py +++ b/jsonpath_rfc9535/segments.py @@ -145,18 +145,20 @@ def _children(node: JSONPathNode) -> Iterable[JSONPathNode]: while queue: _node, depth = queue.popleft() - if depth > self.env.max_recursion_depth: + if depth >= self.env.max_recursion_depth: raise JSONPathRecursionError( "recursion limit exceeded", token=self.token ) yield _node + # Visit child nodes now or queue them for later? visit_children = random.choice([True, False]) # noqa: S311 + for child in _children(_node): if visit_children: yield child - queue.extend([(child, depth + 1) for child in _children(child)]) + queue.extend([(child, depth + 2) for child in _children(child)]) else: queue.append((child, depth + 1))