Repository navigation
fix(tools): substitute OpenAPI server URL variables, and warn when a placeholder can't be filled - #8467
fix(tools): substitute OpenAPI server URL variables, and warn when a placeholder can't be filled#8467chaitanya0305 wants to merge 4 commits into
Conversation
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.
Test missing defaults, unknown placeholders and invalid variables, and update the comment where the base URL is built.
1AbdullahButt
left a comment
There was a problem hiding this comment.
This looks good to merge after one small edge case. I ran tests/unittests/tools/openapi_tool at f20c670 in Docker (441 passed). With openapi_spec_parser.py reset to the merge base, 6 of the 8 server-variable cases fail, as the description says. I also parsed a few hand-written specs, listed below.
-
A
nulldefault ends up in the URL with no warning (src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py:71-72)."variables": {"region": {"default": None}}giveshttps://None.x.com. That's easy to hit in YAML (default:with nothing after it, ordefault: ~). The check only asks whether the key exists, so this skips the warning that the PR adds for exactly this situation. The spec requiresdefaultto be a string. I'd keepstr()for numbers, sincedefault: 8443for a port is common and turns into"8443"as you'd want. I'd only treatNoneas missing:default = variable.get("default") if isinstance(variable, dict) else None if default is not None: return str(default)
Then add
reason = "its server variable has no default"for that case, plus one morepytest.paramwith{"region": {"default": None}}. -
Out of scope, but worth knowing for a follow-up: only
servers[0]at the root is used (:225). Path-level and operation-levelserversare still ignored. A spec with roothttps://api.x.comand an operationservers: [{url: "https://{r}.other.com", variables: {r: {default: eu}}}]still sends that operation tohttps://api.x.com. This is the same as onmain, so it's not a regression. Since_server_urltakes one Server Object, it should be easy to reuse there later. A one-line comment at:221saying overrides aren't supported yet would save the next reader a search.
Also, #8371 targets the same issue, so it may be worth closing one of the two once a maintainer picks.
|
Thanks for testing it and for the careful review, @1AbdullahButt.
On #8371: happy to go with whichever one the maintainers prefer. |
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
OpenApiSpecParserused the first server'surlexactly as written. OpenAPI server URLs can be templates, such ashttps://{region}.api.example.com/{basePath}, with each variable'sdefaultinservers[0].variables. The variables were never substituted, so everyRestApiToolbuilt byOpenAPIToolsetsent requests to the literal URL containing{region}.Solution:
_server_url()replaces each{name}placeholder in the first server's URL with that server variable'sdefault, in one pass.When a placeholder can't be filled, the URL keeps the placeholder and a warning is logged naming the placeholder, the URL and the reason. A placeholder can't be filled when:
defaultvariablesis not an objectWithout the warning, these requests would fail with no clue as to why. The comment where the base URL is built now describes this.
Testing Plan
Unit Tests:
New tests in
tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py:test_parse_spec_with_server_variables: defaults are substituted.test_parse_spec_with_unfillable_server_variable, parametrized over a missing default, an unknown placeholder, a variable that is not an object, andvariablesthat is not an object. Each one checks that the placeholder stays in the URL and that exactly one warning names it and its reason.test_parse_spec_with_server_variables_logs_nothing: a fully filled URL logs no warning.With
main'sopenapi_spec_parser.py, the 6 new server-variable test cases fail. The pre-commit hooks pass: ruff, isort, pyink, addlicense, ADK compliance and codespell.Manual End-to-End (E2E) Tests:
Not run against a live API. The tests parse specs with templated server URLs and check the
base_urlthat every generatedRestApiTooluses.Checklist
Additional context
This adds the edge-case tests, the updated comment and the warning log that @llalitkumarrr asked for in #8360, where they asked me to open this PR. Thanks for confirming the bug and the fix on
main. #8371 by @feiiiiii5 also addresses this issue.