Skip to content

fix(openapi): skip non-numeric response keys in generate_return_doc#6175

Open
garyzava wants to merge 2 commits into
google:mainfrom
garyzava:fix/openapi-return-doc-non-numeric-keys
Open

fix(openapi): skip non-numeric response keys in generate_return_doc#6175
garyzava wants to merge 2 commits into
google:mainfrom
garyzava:fix/openapi-return-doc-non-numeric-keys

Conversation

@garyzava

Copy link
Copy Markdown

Summary

PydocHelper.generate_return_doc sorts the OpenAPI responses with
int(item[0]), which crashes with ValueError: invalid literal for int() with base 10 on valid non-numeric response keys like default or range patterns
(2XX, 4XX). This skips non-numeric keys in the sort so a tool can be
generated from such specs.

Fixes #6174

What changed

  • src/google/adk/tools/openapi_tool/common/common.py - exclude non-numeric
    keys from the sort in generate_return_doc.
  • tests/unittests/tools/openapi_tool/common/test_common.py - add regression
    tests for a default key, a range key, and a responses dict with only
    non-numeric keys.

Why

The line after the sort already keeps only responses whose key starts with 2
and that have content, so non-numeric keys (default, 1XX-5XX) are never
used anyway. Dropping them from the sort removes the crash without changing any
valid behavior. This matches operation_parser, which filters startswith('2')
before using the keys.

How I tested

$ pytest tests/unittests/tools/openapi_tool/common/test_common.py -k generate_return_doc -q
9 passed

The three new tests fail before the fix with
ValueError: invalid literal for int() with base 10: 'default' (and '4XX')
and pass after. pyink and isort report no changes on both files.

Backward compatibility

No API change. Specs with only numeric response keys behave exactly as before;
specs with default/range keys now work instead of crashing.

PydocHelper.generate_return_doc sorted the responses with
int(item[0]), which raises ValueError: invalid literal for int() with
base 10 on valid OpenAPI response keys that are not numeric status
codes, such as 'default' and range patterns like '2XX'/'4XX'.

Skip non-numeric keys in the sort. The next statement already keeps
only keys starting with '2' that have content, so dropping non-numeric
keys changes no valid behavior and just avoids the crash. This mirrors
operation_parser, which filters startswith('2') before using the keys.

Adds regression tests for a 'default' key, a range key, and a
responses dict with only non-numeric keys (returns '').
@adk-bot adk-bot added the tools [Component] This issue is related to tools label Jun 24, 2026
@rohityan rohityan self-assigned this Jun 24, 2026
@rohityan rohityan added the needs review [Status] The PR/issue is awaiting review from the maintainer label Jun 26, 2026
@rohityan rohityan requested a review from DeanChensj June 26, 2026 03:03
@rohityan

Copy link
Copy Markdown
Collaborator

Hi @garyzava, Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@rohityan

Copy link
Copy Markdown
Collaborator

Hi @DeanChensj , can you please review this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs review [Status] The PR/issue is awaiting review from the maintainer tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI tool: generate_return_doc crashes on non-numeric response keys (default, NXX)

3 participants