From 8f6806344496383f9f4e58351556c7782dc2b9ae Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Wed, 25 Nov 2026 22:50:40 +0530 Subject: [PATCH 1/4] fix(tools): substitute OpenAPI server URL variables with their defaults OpenApiSpecParser used the root server's url verbatim as every operation's base URL. A templated server such as https://{region}.api.example.com was therefore requested with the literal braces in the host, so every tool built from the spec failed. OpenAPI requires the variable's default to be used when no other value is supplied. --- .../openapi_spec_parser.py | 15 ++++++++++++++- .../test_openapi_spec_parser.py | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py index 1745698e410..ddb641d630e 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py @@ -47,6 +47,19 @@ _SCHEMA_CONTAINER_KEYS: Set[str] = frozenset({"schema", "schemas"}) +def _server_url(server: Dict[str, Any]) -> str: + """Returns a Server Object's URL with each variable set to its default. + + A server URL such as ``https://{region}.example.com`` is a template, and + OpenAPI requires the default value to be used when no other is supplied. + """ + url = server.get("url", "") + for name, variable in (server.get("variables") or {}).items(): + if isinstance(variable, dict) and "default" in variable: + url = url.replace(f"{{{name}}}", str(variable["default"])) + return url + + class OperationEndpoint(BaseModel): base_url: str path: str @@ -180,7 +193,7 @@ def _collect_operations( # Taking first server url, or default to empty string if not present base_url = "" if openapi_spec.get("servers"): - base_url = openapi_spec["servers"][0].get("url", "") + base_url = _server_url(openapi_spec["servers"][0]) # Get global security scheme (if any). global_scheme_name = _required_scheme_name(openapi_spec.get("security")) diff --git a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py index f8f29d1a968..938d3fd7fa9 100644 --- a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +++ b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py @@ -294,6 +294,25 @@ def test_parse_spec_with_servers(openapi_spec_generator): assert parsed_operations[0].endpoint.base_url == "https://api.example.com" +def test_parse_spec_with_server_variables(openapi_spec_generator): + """Server URL variables are replaced with their default values.""" + openapi_spec = create_minimal_openapi_spec() + openapi_spec["servers"] = [{ + "url": "https://{region}.api.example.com/{basePath}", + "variables": { + "region": {"default": "us", "enum": ["us", "eu"]}, + "basePath": {"default": "v2"}, + }, + }] + + parsed_operations = openapi_spec_generator.parse(openapi_spec) + + assert len(parsed_operations) == 1 + assert ( + parsed_operations[0].endpoint.base_url == "https://us.api.example.com/v2" + ) + + def test_parse_spec_with_no_servers(openapi_spec_generator): """Test with no servers defined (should default to empty string).""" openapi_spec = create_minimal_openapi_spec() From 8cde4f504c404757dd749de3236043df381f9fae Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Wed, 25 Nov 2026 23:01:39 +0530 Subject: [PATCH 2/4] fix(tools): ignore malformed server variables and substitute them in one pass --- .../openapi_spec_parser/openapi_spec_parser.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py index ddb641d630e..fad58107182 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py @@ -15,6 +15,7 @@ from __future__ import annotations import copy +import re from typing import Any from typing import Dict from typing import List @@ -54,10 +55,17 @@ def _server_url(server: Dict[str, Any]) -> str: OpenAPI requires the default value to be used when no other is supplied. """ url = server.get("url", "") - for name, variable in (server.get("variables") or {}).items(): + variables = server.get("variables") + if not isinstance(variables, dict): + return url + + def _default(match: re.Match[str]) -> str: + variable = variables.get(match.group(1)) if isinstance(variable, dict) and "default" in variable: - url = url.replace(f"{{{name}}}", str(variable["default"])) - return url + return str(variable["default"]) + return match.group(0) + + return re.sub(r"\{([^{}]+)\}", _default, url) class OperationEndpoint(BaseModel): From f20c670bf627049ef6a87ff67012665aa7ebf2c1 Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Tue, 1 Dec 2026 12:38:29 +0530 Subject: [PATCH 3/4] fix(tools): log a warning when a server URL placeholder cannot be filled Test missing defaults, unknown placeholders and invalid variables, and update the comment where the base URL is built. --- .../openapi_spec_parser.py | 27 ++++++- .../test_openapi_spec_parser.py | 80 +++++++++++++++++++ 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py index fad58107182..81cbf9a9b28 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py @@ -15,6 +15,7 @@ from __future__ import annotations import copy +import logging import re from typing import Any from typing import Dict @@ -47,22 +48,41 @@ _SCHEMA_CONTAINER_KEYS: Set[str] = frozenset({"schema", "schemas"}) +logger = logging.getLogger("google_adk." + __name__) + def _server_url(server: Dict[str, Any]) -> str: """Returns a Server Object's URL with each variable set to its default. A server URL such as ``https://{region}.example.com`` is a template, and OpenAPI requires the default value to be used when no other is supplied. + A placeholder that has no variable, or whose variable is not an object with + a ``default``, is left as is and a warning is logged, since requests to + that URL will fail. """ url = server.get("url", "") variables = server.get("variables") if not isinstance(variables, dict): - return url + variables = {} def _default(match: re.Match[str]) -> str: - variable = variables.get(match.group(1)) + name = match.group(1) + variable = variables.get(name) if isinstance(variable, dict) and "default" in variable: return str(variable["default"]) + if variable is None: + reason = "no server variable defines it" + elif not isinstance(variable, dict): + reason = "its server variable is not an object" + else: + reason = "its server variable has no default" + logger.warning( + "Cannot fill placeholder {%s} in server URL %r: %s. Requests will be" + " sent to the URL with the placeholder unfilled.", + name, + url, + reason, + ) return match.group(0) return re.sub(r"\{([^{}]+)\}", _default, url) @@ -198,7 +218,8 @@ def _collect_operations( """Collects operations from an OpenAPI spec.""" operations = [] - # Taking first server url, or default to empty string if not present + # Use the first server's URL, with its server variables replaced by their + # defaults, or an empty string if no server is defined. base_url = "" if openapi_spec.get("servers"): base_url = _server_url(openapi_spec["servers"][0]) diff --git a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py index 938d3fd7fa9..cb6ece3759d 100644 --- a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +++ b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging from typing import Any from typing import Dict @@ -313,6 +314,85 @@ def test_parse_spec_with_server_variables(openapi_spec_generator): ) +@pytest.mark.parametrize( + ("url", "variables", "expected_url", "placeholder", "reason"), + [ + pytest.param( + "https://{region}.api.example.com/{basePath}", + {"region": {"enum": ["us", "eu"]}, "basePath": {"default": "v2"}}, + "https://{region}.api.example.com/v2", + "region", + "has no default", + id="missing_default", + ), + pytest.param( + "https://{region}.api.example.com/{version}", + {"region": {"default": "us"}}, + "https://us.api.example.com/{version}", + "version", + "no server variable defines it", + id="unknown_placeholder", + ), + pytest.param( + "https://{region}.api.example.com", + {"region": "us"}, + "https://{region}.api.example.com", + "region", + "is not an object", + id="invalid_variable", + ), + pytest.param( + "https://{region}.api.example.com", + ["region"], + "https://{region}.api.example.com", + "region", + "no server variable defines it", + id="invalid_variables", + ), + ], +) +def test_parse_spec_with_unfillable_server_variable( + openapi_spec_generator, + caplog, + url, + variables, + expected_url, + placeholder, + reason, +): + """A placeholder that can't be filled stays in the URL and is logged.""" + openapi_spec = create_minimal_openapi_spec() + openapi_spec["servers"] = [{"url": url, "variables": variables}] + + with caplog.at_level(logging.WARNING): + parsed_operations = openapi_spec_generator.parse(openapi_spec) + + assert parsed_operations[0].endpoint.base_url == expected_url + warnings = [ + r.getMessage() for r in caplog.records if r.levelno == logging.WARNING + ] + assert len(warnings) == 1 + assert f"{{{placeholder}}}" in warnings[0] + assert reason in warnings[0] + + +def test_parse_spec_with_server_variables_logs_nothing( + openapi_spec_generator, caplog +): + """Filling every placeholder logs no warning.""" + openapi_spec = create_minimal_openapi_spec() + openapi_spec["servers"] = [{ + "url": "https://{region}.api.example.com", + "variables": {"region": {"default": "eu"}}, + }] + + with caplog.at_level(logging.WARNING): + parsed_operations = openapi_spec_generator.parse(openapi_spec) + + assert parsed_operations[0].endpoint.base_url == "https://eu.api.example.com" + assert not [r for r in caplog.records if r.levelno == logging.WARNING] + + def test_parse_spec_with_no_servers(openapi_spec_generator): """Test with no servers defined (should default to empty string).""" openapi_spec = create_minimal_openapi_spec() From dff9bc7e5784cc04de9358b69123b20300327c50 Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Wed, 2 Dec 2026 21:18:47 +0530 Subject: [PATCH 4/4] fix(tools): treat a null OpenAPI server variable default as missing --- .../openapi_spec_parser/openapi_spec_parser.py | 10 ++++++---- .../openapi_spec_parser/test_openapi_spec_parser.py | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py index 81cbf9a9b28..b59747f2a72 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py @@ -57,7 +57,7 @@ def _server_url(server: Dict[str, Any]) -> str: A server URL such as ``https://{region}.example.com`` is a template, and OpenAPI requires the default value to be used when no other is supplied. A placeholder that has no variable, or whose variable is not an object with - a ``default``, is left as is and a warning is logged, since requests to + a non-null ``default``, is left as is and a warning is logged, since requests to that URL will fail. """ url = server.get("url", "") @@ -68,8 +68,9 @@ def _server_url(server: Dict[str, Any]) -> str: def _default(match: re.Match[str]) -> str: name = match.group(1) variable = variables.get(name) - if isinstance(variable, dict) and "default" in variable: - return str(variable["default"]) + default = variable.get("default") if isinstance(variable, dict) else None + if default is not None: + return str(default) if variable is None: reason = "no server variable defines it" elif not isinstance(variable, dict): @@ -219,7 +220,8 @@ def _collect_operations( operations = [] # Use the first server's URL, with its server variables replaced by their - # defaults, or an empty string if no server is defined. + # defaults, or an empty string if no server is defined. Path-level and + # operation-level `servers` overrides are not supported yet. base_url = "" if openapi_spec.get("servers"): base_url = _server_url(openapi_spec["servers"][0]) diff --git a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py index cb6ece3759d..4d228b05e0e 100644 --- a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +++ b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py @@ -325,6 +325,14 @@ def test_parse_spec_with_server_variables(openapi_spec_generator): "has no default", id="missing_default", ), + pytest.param( + "https://{region}.api.example.com", + {"region": {"default": None}}, + "https://{region}.api.example.com", + "region", + "has no default", + id="null_default", + ), pytest.param( "https://{region}.api.example.com/{version}", {"region": {"default": "us"}},