OpenAPIToolset: server URL variables are never substituted, so every request goes to the literal {var} URL
維護者通常 3 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
從 OpenApiSpecParser 中 _collect_operations 用來讀取 openapi_spec["servers"][0]["url"] 並設定 base_url 的程式碼開始,然後檢查 RestApiTool._prepare_request_params 如何只填充路徑範本。新增一個小型輔助函式,將每個 {name} 替換為其宣告的 default,並將其用於根伺服器。當重現腳本輸出 https://us.api.example.com/v2,且 tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py 中的測試通過時,即為完成。
由索引模型根據 Issue 內容生成。
描述
Summary
OpenApiSpecParser uses the first root server's url exactly as written and ignores its variables. Templated server URLs are common in OpenAPI 3 for regional, tenant or versioned APIs, for example https://{region}.api.example.com/{basePath} with declared defaults.
For such a spec, every RestApiTool built by OpenAPIToolset (and the APIHub toolset, which uses the same parser) sends its requests to the literal https://{region}.api.example.com/%7BbasePath%7D/.... On a real network every call fails with a DNS or connection error. The OpenAPI spec says a server variable's default must be used when no other value is supplied.
Reproduction
Run on current main (4e3aff79). An httpx.MockTransport records the request, so no network is needed.
import asyncio, httpx
from google.adk.tools.openapi_tool.openapi_spec_parser.openapi_toolset import OpenAPIToolset
spec = {"openapi": "3.0.3", "info": {"title": "t", "version": "1"},
"servers": [{"url": "https://{region}.api.example.com/{basePath}",
"variables": {"region": {"default": "us"}, "basePath": {"default": "v2"}}}],
"paths": {"/pets": {"get": {"operationId": "listPets", "responses": {"200": {"description": "ok"}}}}}}
seen = []
def handler(req):
seen.append(str(req.url))
return httpx.Response(200, json={"ok": True})
ts = OpenAPIToolset(spec_dict=spec,
httpx_client_factory=lambda: httpx.AsyncClient(transport=httpx.MockTransport(handler)),
allow_private_network_access=True)
tool = ts._tools[0]
print("endpoint base_url:", tool.endpoint.base_url)
asyncio.run(tool.call(args={}, tool_context=None))
print("requests seen:", seen)
Actual
endpoint base_url: https://{region}.api.example.com/{basePath}
requests seen: ['https://{region}.api.example.com/%7BbasePath%7D/pets']
Expected
endpoint base_url: https://us.api.example.com/v2
requests seen: ['https://us.api.example.com/v2/pets']
Cause
OpenApiSpecParser._collect_operations sets base_url from openapi_spec["servers"][0]["url"]. RestApiTool._prepare_request_params fills in only the path template, so nothing replaces the server variables later.
Proposed fix
Add a small private helper _server_url(server) that replaces each {name} in the server URL with variables[name]["default"] in a single pass, and use it for the root server.
- A plain URL without variables is unchanged.
- A variable without a
default, or a placeholder that is not declared, is left as it is (same as now), so nothing crashes or guesses a host. - Malformed
variables(not a mapping) are ignored, as now.
Related: #8064 / #8088 choose which server applies to each operation (operation- and path-level servers). #8088 says server variables are out of scope, so this issue is separate. The two compose: the server that #8088 picks for an operation can go through the same helper. If #8088 lands first, I'll rebase on it.
There is a regression test in tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py. It fails on main and passes with the fix; the openapi_tool tests pass (436).
I used an AI coding assistant (Claude) to find and analyse this; I reproduced it and checked the fix myself.
I've tested a fix with a regression test: https://github.com/chaitanya0305/adk-python/compare/main...chaitanya0305:adk-python:fix/openapi-server-variables. If this is accepted, I'll open the PR straight away.
- 主要語言
- Python
- 星號
- 22.3k
- 分支
- 4.4k
- 平均合併
- 1 天 2 小時
- 30 天內合併 PR
- 6
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
google/adk-python 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 82/100
google/adk-python#8359 ·
維護者通常 3 天內回覆
-
Can pin to litellm version be changed?可能已有人在做 @llalitkumarrr 於 10 天前認領。 未關閉models
難度 1/5 1 小時以內 新手友好度 65/100
google/adk-python#8129 · 2 則留言 · 已指派 1 人 ·
維護者通常 3 天內回覆
-
OpenAPI tools ignore operation- and path-level server overrides可能已有人在做 @sanketpatil06 於 14 天前認領。 未關閉tools
難度 2/5 1-3 小時 新手友好度 75/100
google/adk-python#8064 · 4 則留言 · 已指派 1 人 ·
維護者通常 3 天內回覆
-
Two docstrings in openapi_tool document parameters the signatures do not accept可能已有人在做 @surajksharma07 於 17 天前認領。 未關閉
難度 1/5 1 小時以內 新手友好度 88/100
google/adk-python#8024 · 1 則留言 · 已指派 1 人 ·
維護者通常 3 天內回覆
-
`adk run` does not support arrow keys or input history可能已有人在做 @llalitkumarrr 於 17 天前認領。 未關閉cli
難度 1/5 1-3 小時 新手友好度 84/100
google/adk-python#8022 · 2 則留言 · 已指派 1 人 ·
維護者通常 3 天內回覆
查看 google/adk-python 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
vllm-project/vllm#70030 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
TigreGotico/tugaphone#408 ·
維護者通常 1 天內回覆
-
t: bug
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 3 天內回覆
-
needs-triage
難度 1/5 1 小時以內 新手友好度 85/100
Azure/azure-sdk-for-python#50155 · 1 個 reaction ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 66/100
維護者通常 1 天內回覆