Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/google/adk/tools/openapi_tool/common/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,12 @@ def generate_return_doc(responses: Dict[str, Response]) -> str:

# Only consider 2xx responses for return type hinting.
# Returns the 2xx response with the smallest status code number and with
# content defined.
sorted_responses = sorted(responses.items(), key=lambda item: int(item[0]))
# content defined. Non-numeric OpenAPI response keys (e.g. 'default' or
# range codes like '2XX') are valid and sorted after numeric status codes.
sorted_responses = sorted(
responses.items(),
key=lambda item: int(item[0]) if item[0].isdigit() else float('inf'),
)
qualified_response = next(
filter(
lambda r: r[0].startswith('2') and r[1].content,
Expand Down
19 changes: 19 additions & 0 deletions tests/unittests/tools/openapi_tool/common/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,25 @@ def test_generate_return_doc_2xx_smallest_status_code_response(self):
== expected_doc
)

def test_generate_return_doc_non_numeric_status_keys(self):
# 'default' (and range codes like '2XX') are valid OpenAPI response keys
# and must not crash return-doc generation.
responses = {
'200': {
'description': 'Successful response',
'content': {'application/json': {'schema': {'type': 'string'}}},
},
'default': {
'description': 'Unexpected error',
'content': {'application/json': {'schema': {'type': 'object'}}},
},
}
expected_doc = 'Returns (str): Successful response'
assert (
PydocHelper.generate_return_doc(dict_to_responses(responses))
== expected_doc
)

def test_generate_return_doc_contentful_response(self):
responses = {
'200': {'description': 'No content response'},
Expand Down