Skip to content

fix(loaders): handle self-referencing types to avoid infinite recursion#122

Open
Mxiansen wants to merge 1 commit into
TencentCloud:masterfrom
Mxiansen:fix/loaders-self-ref-infinite-recursion
Open

fix(loaders): handle self-referencing types to avoid infinite recursion#122
Mxiansen wants to merge 1 commit into
TencentCloud:masterfrom
Mxiansen:fix/loaders-self-ref-infinite-recursion

Conversation

@Mxiansen

@Mxiansen Mxiansen commented Jun 18, 2026

Copy link
Copy Markdown
  • Add path-level cycle detection and a max_depth=30 fallback in the parameter unfolding and skeleton-generation core logic.
  • Truncate self-referential nodes into leaves and render them as RecursiveRef<TypeName> placeholders; document rendering is updated accordingly.
  • Introduce deep-nested argument extraction, depth validation, over-limit error reporting, and JSON-file input hint generation.
  • Refactor build_action_parameters to accept extra unfolded parameters, and pre-detect value-less orphan --key tokens with a precise error before argparse.
  • Introduce MAX_INPUT_DEPTH and RECURSIVE_HINT_FILE_OPTION constants to centralize recursion and hint behavior.
  • Reduce redundant get_service_model calls via path-level schema caching, lowering per-command overhead.
  • Add unit tests for the cli_unfold_argument, command, loaders, and document_handler modules.

Details

  • Add a path-level visited set in the parameter unfolding logic to detect self-references and cyclic references.
  • Add max_depth=30 as a recursion-depth safety net; verified to cover all current SDK data.
  • Optimize _get_param_info to truncate the current node to a leaf when a recursion is hit.
  • Optimize _generate_param_skeleton to use RecursiveRef<TypeName> as the placeholder for recursive references.
  • Optimize _get_unfold_param_info / _recur_get_unfold_param_info to prevent unfold parameters from expanding infinitely.
  • Optimize _filling_unfold_param_info to mark truncated leaves as Object and prompt users to use --cli-input-json file://<path/to/request.json>.
  • Split _extract_deep_nested_args into single-responsibility helpers such as _detect_deep_nesting_attempt and _build_recursive_hint.
  • Unify the hint wording to --cli-input-json file://<path/to/request.json>, making the file:// prefix explicit; add a fallback hint for the normal (non-unfold) mode that infers the top-level field name from the truncated leaf path.
  • Value-less orphan --key tokens (no following value) are now explicitly rejected before argparse with a precise error, instead of leaking downstream and being misreported as "Unknown options".
  • Add tests/test_cli_unfold_argument.py, tests/test_command.py, tests/test_loaders.py, and tests/test_document_handler.py, totaling 191 cases covering the critical paths.
  • Fully preserve behavior for normal non-recursive APIs: none of the compensation paths trigger when the data has no self-reference.

@Mxiansen Mxiansen force-pushed the fix/loaders-self-ref-infinite-recursion branch from b198a1d to 34d3ae8 Compare June 24, 2026 06:59
Mxiansen added a commit to Mxiansen/tencentcloud-cli-intl-en that referenced this pull request Jun 24, 2026
Some APIs (e.g. billing/CreateGatherRule, billing/CreateAllocationRule,
billing/ModifyAllocationRule) declare self-referencing request types,
typically AllocationRuleExpression.Children: list<AllocationRuleExpression>.

The current expansion logic in tccli/loaders.py walks the type graph
without any cycle detection or depth bound, recursing forever and ending
with RecursionError, breaking:
- tccli <svc> <action> help
- tccli <svc> <action> --generate-cli-skeleton
- tccli <svc> <action> --cli-unfold-argument ...

Fix: introduce a path-level visited frozenset plus max_depth=20 fallback
on all three DFS expansion paths. When a cycle is hit (or depth is
exceeded), the node is rendered as a placeholder leaf instead of
recursing further. visited is path-scoped (visited | {member}), so
sibling fields sharing a normal struct are not mistakenly truncated.

In addition, when a self-referencing leaf is reached the truncated node
now carries stable flags (recursive_truncated / recursive_type) and an
extended document hint mentioning --cli-input-json and
--generate-cli-skeleton. tccli/command.py uses these flags to detect the
case where users keep drilling into a self-referencing path with
--cli-unfold-argument (e.g. --RuleList.RuleDetail.Children.0.RuleValue),
and prints a targeted hint pointing at the offending option, the
self-referencing type, and the recommended JSON-based usages, instead of
only raising the generic "Unknown options" error.

Same fix as TencentCloud/tencentcloud-cli#122.

Related TAPD: https://tapd.woa.com/tapd_fe/10161711/story/detail/1010161711126433671
Internal MR: https://git.woa.com/tencentcloud-internal/tencentcloud-cli/merge_requests/18
@Mxiansen Mxiansen force-pushed the fix/loaders-self-ref-infinite-recursion branch from 34d3ae8 to fb367f9 Compare June 24, 2026 07:48
Mxiansen added a commit to Mxiansen/tencentcloud-cli-intl-en that referenced this pull request Jun 24, 2026
Some APIs (e.g. billing/CreateGatherRule, billing/CreateAllocationRule,
billing/ModifyAllocationRule) declare self-referencing request types,
typically AllocationRuleExpression.Children: list<AllocationRuleExpression>.

The current expansion logic in tccli/loaders.py walks the type graph
without any cycle detection or depth bound, recursing forever and ending
with RecursionError, breaking:
- tccli <svc> <action> help
- tccli <svc> <action> --generate-cli-skeleton
- tccli <svc> <action> --cli-unfold-argument ...

Fix: introduce a path-level visited frozenset plus max_depth=20 fallback
on all three DFS expansion paths. When a cycle is hit (or depth is
exceeded), the node is rendered as a placeholder leaf instead of
recursing further. visited is path-scoped (visited | {member}), so
sibling fields sharing a normal struct are not mistakenly truncated.

In addition, when a self-referencing leaf is reached the truncated node
now carries stable flags (recursive_truncated / recursive_type) and an
extended document hint mentioning --cli-input-json and
--generate-cli-skeleton. tccli/command.py uses these flags to detect the
case where users keep drilling into a self-referencing path with
--cli-unfold-argument (e.g. --RuleList.RuleDetail.Children.0.RuleValue),
and prints a targeted hint pointing at the offending option, the
self-referencing type, and the recommended JSON-based usages, instead of
only raising the generic "Unknown options" error.

Same fix as TencentCloud/tencentcloud-cli#122.

Related TAPD: https://tapd.woa.com/tapd_fe/10161711/story/detail/1010161711126433671
Internal MR: https://git.woa.com/tencentcloud-internal/tencentcloud-cli/merge_requests/18
@Mxiansen Mxiansen force-pushed the fix/loaders-self-ref-infinite-recursion branch from fb367f9 to ac5ca5e Compare June 24, 2026 12:02
Mxiansen added a commit to Mxiansen/tencentcloud-cli-intl-en that referenced this pull request Jun 24, 2026
Some APIs (e.g. billing/CreateGatherRule, billing/CreateAllocationRule,
billing/ModifyAllocationRule) declare self-referencing request types,
typically AllocationRuleExpression.Children: list<AllocationRuleExpression>.

The current expansion logic in tccli/loaders.py walks the type graph
without any cycle detection or depth bound, recursing forever and ending
with RecursionError, breaking:
- tccli <svc> <action> help
- tccli <svc> <action> --generate-cli-skeleton
- tccli <svc> <action> --cli-unfold-argument ...

Fix: introduce a path-level visited frozenset plus max_depth=20 fallback
on all three DFS expansion paths. When a cycle is hit (or depth is
exceeded), the node is rendered as a placeholder leaf instead of
recursing further. visited is path-scoped (visited | {member}), so
sibling fields sharing a normal struct are not mistakenly truncated.

In addition, when a self-referencing leaf is reached the truncated node
now carries stable flags (recursive_truncated / recursive_type) and an
extended document hint mentioning --cli-input-json and
--generate-cli-skeleton. tccli/command.py uses these flags to detect the
case where users keep drilling into a self-referencing path with
--cli-unfold-argument (e.g. --RuleList.RuleDetail.Children.0.RuleValue),
and prints a targeted hint pointing at the offending option, the
self-referencing type, and the recommended JSON-based usages, instead of
only raising the generic "Unknown options" error.

Same fix as TencentCloud/tencentcloud-cli#122.

Related TAPD: https://tapd.woa.com/tapd_fe/10161711/story/detail/1010161711126433671
Internal MR: https://git.woa.com/tencentcloud-internal/tencentcloud-cli/merge_requests/18
@Mxiansen Mxiansen force-pushed the fix/loaders-self-ref-infinite-recursion branch 7 times, most recently from 76d8281 to 2ee8601 Compare July 2, 2026 06:35
Some APIs (e.g. billing/CreateGatherRule, billing/CreateAllocationRule,
billing/ModifyAllocationRule) declare self-referencing request types,
typically AllocationRuleExpression.Children: list<AllocationRuleExpression>.

The original expansion logic in tccli/loaders.py walked the type graph
without any cycle detection or depth bound, recursing forever and ending
with RecursionError, breaking:
- tccli <svc> <action> help
- tccli <svc> <action> --generate-cli-skeleton
- tccli <svc> <action> --cli-unfold-argument ...

Core fix:
- Introduce a path-level visited frozenset plus max_depth=20 fallback on
  all three DFS expansion paths in tccli/loaders.py. When a cycle is hit
  (or depth is exceeded), the node is rendered as a placeholder leaf
  instead of recursing further. visited is path-scoped (visited | {member}),
  so sibling fields sharing a normal struct are not mistakenly truncated.
- tccli/document_handler.py adapts to the new contract: when members is
  a type-name string (truncated placeholder) instead of a dict, render
  it as <RecursiveRef:TypeName> and stop recursion, so help --detail no
  longer raises TypeError on self-referencing APIs.

Beyond the truncation safety net, this change makes --cli-unfold-argument
actually usable for users who legitimately need to drill into a
self-referencing type for a few extra levels:

- tccli/loaders.py: introduce MAX_INPUT_DEPTH=30 and a shared
  RECURSIVE_HINT_FILE_OPTION constant. Simplify the truncation hint to
  a single --cli-input-json file://... alternative.

- tccli/cli_unfold_argument.py: build_action_parameters() accepts an
  optional extra_unfold_args dict; when provided, those flat key/value
  pairs are fed through the same convert_to_dict + handle_array pipeline
  as the argparse Namespace, so the request body has one consistent
  shape. With extra_unfold_args=None the behaviour is byte-identical.

- tccli/command.py: ActionCommand gains _extract_deep_nested_args /
  _resolve_inner_leaf_type / _build_oversized_hint and supporting
  helpers. Under --cli-unfold-argument, after argparse leaves a
  remaining list, walk it (--k v / --k=v, list nargs='*'), match
  the longest recursive-truncated prefix, and:
    * forward keys with depth <= MAX_INPUT_DEPTH into extra_unfold_args,
      auto-wrapping a scalar into [scalar] when the schema's leaf type
      is list (looked up via service_model.objects walk);
    * record keys whose flat depth exceeds MAX_INPUT_DEPTH into
      oversized_tokens, surfaced through a dedicated hint that points
      at --cli-input-json file://.
  The scan is driven by the function's input remaining (not sys.argv),
  so unit tests can exercise it directly through ac(args, globals).
  An orphan-value drift fallback covers the case where argparse
  reorders consecutive unknown options and parks a value at the tail.

Compatibility:
- For non-recursive APIs and for self-referencing APIs that don't drill
  below the truncation point, output is byte-identical before/after
  this patch (verified against cvm/RunInstances, cvm/DescribeInstances,
  billing/DescribeBillSummary).
- For self-referencing APIs that DO drill below, requests with flat
  depth <= 30 now go through; depth > 30 is rejected at CLI layer with
  a targeted hint pointing at --cli-input-json file://.
- Both --key value and --key=value argparse forms are supported.

Related TAPD: https://tapd.woa.com/tapd_fe/10161711/story/detail/1010161711126433671
Internal MR: https://git.woa.com/tencentcloud-internal/tencentcloud-cli/merge_requests/18
@Mxiansen Mxiansen force-pushed the fix/loaders-self-ref-infinite-recursion branch from 2ee8601 to e75c488 Compare July 2, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant