OpenAPIToolset: server URL variables are never substituted, so every request goes to the literal {var} URL

オープン 初心者向け
#8,360 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
openapi, python
領域
api

調査の方向性

_collect_operations が openapi_spec["servers"][0]["url"] を読み取って base_url を設定するために使う OpenApiSpecParser のコードから始め、次に RestApiTool._prepare_request_params がパステンプレートだけを埋めていることを確認してください。宣言された default で各 {name} を置き換える小さなヘルパーを追加し、ルートサーバーに使ってください。再現スクリプトが 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時間
マージ済み PR(30日)
6

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

google/adk-python のほかの issue

google/adk-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。