fix(loaders): handle self-referencing types to avoid infinite recursion#122
Open
Mxiansen wants to merge 1 commit into
Open
fix(loaders): handle self-referencing types to avoid infinite recursion#122Mxiansen wants to merge 1 commit into
Mxiansen wants to merge 1 commit into
Conversation
b198a1d to
34d3ae8
Compare
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
34d3ae8 to
fb367f9
Compare
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
fb367f9 to
ac5ca5e
Compare
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
76d8281 to
2ee8601
Compare
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
2ee8601 to
e75c488
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
max_depth=30fallback in the parameter unfolding and skeleton-generation core logic.RecursiveRef<TypeName>placeholders; document rendering is updated accordingly.build_action_parametersto accept extra unfolded parameters, and pre-detect value-less orphan--keytokens with a precise error before argparse.MAX_INPUT_DEPTHandRECURSIVE_HINT_FILE_OPTIONconstants to centralize recursion and hint behavior.get_service_modelcalls via path-level schema caching, lowering per-command overhead.cli_unfold_argument,command,loaders, anddocument_handlermodules.Details
visitedset in the parameter unfolding logic to detect self-references and cyclic references.max_depth=30as a recursion-depth safety net; verified to cover all current SDK data._get_param_infoto truncate the current node to a leaf when a recursion is hit._generate_param_skeletonto useRecursiveRef<TypeName>as the placeholder for recursive references._get_unfold_param_info/_recur_get_unfold_param_infoto prevent unfold parameters from expanding infinitely._filling_unfold_param_infoto mark truncated leaves asObjectand prompt users to use--cli-input-json file://<path/to/request.json>._extract_deep_nested_argsinto single-responsibility helpers such as_detect_deep_nesting_attemptand_build_recursive_hint.--cli-input-json file://<path/to/request.json>, making thefile://prefix explicit; add a fallback hint for the normal (non-unfold) mode that infers the top-level field name from the truncated leaf path.--keytokens (no following value) are now explicitly rejected before argparse with a precise error, instead of leaking downstream and being misreported as "Unknown options".tests/test_cli_unfold_argument.py,tests/test_command.py,tests/test_loaders.py, andtests/test_document_handler.py, totaling 191 cases covering the critical paths.