Skip to content

fix(tools): substitute OpenAPI server URL variables, and warn when a placeholder can't be filled - #8467

Open
chaitanya0305 wants to merge 4 commits into
google:mainfrom
chaitanya0305:fix/openapi-server-variables
Open

chaitanya0305 wants to merge 4 commits into
google:mainfrom
chaitanya0305:fix/openapi-server-variables

Conversation

@chaitanya0305

Copy link
Copy Markdown

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:
OpenApiSpecParser used the first server's url exactly as written. OpenAPI server URLs can be templates, such as https://{region}.api.example.com/{basePath}, with each variable's default in servers[0].variables. The variables were never substituted, so every RestApiTool built by OpenAPIToolset sent requests to the literal URL containing {region}.

Solution:
_server_url() replaces each {name} placeholder in the first server's URL with that server variable's default, 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:

  • no variable defines it
  • its variable is not an object
  • its variable has no default
  • variables is not an object

Without 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:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

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, and variables that 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.
$ pytest -n 2 tests/unittests/tools/openapi_tool tests/unittests/tools/apihub_tool
488 passed

With main's openapi_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_url that every generated RestApiTool uses.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

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.

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 1AbdullahButt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. A null default 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}} gives https://None.x.com. That's easy to hit in YAML (default: with nothing after it, or default: ~). The check only asks whether the key exists, so this skips the warning that the PR adds for exactly this situation. The spec requires default to be a string. I'd keep str() for numbers, since default: 8443 for a port is common and turns into "8443" as you'd want. I'd only treat None as 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 more pytest.param with {"region": {"default": None}}.

  2. Out of scope, but worth knowing for a follow-up: only servers[0] at the root is used (:225). Path-level and operation-level servers are still ignored. A spec with root https://api.x.com and an operation servers: [{url: "https://{r}.other.com", variables: {r: {default: eu}}}] still sends that operation to https://api.x.com. This is the same as on main, so it's not a regression. Since _server_url takes one Server Object, it should be easy to reuse there later. A one-line comment at :221 saying 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.

@chaitanya0305

Copy link
Copy Markdown
Author

Thanks for testing it and for the careful review, @1AbdullahButt.

  1. Done in dff9bc7. A null default (default: with nothing after it, or default: ~) is now treated as missing: the placeholder stays unfilled and the "its server variable has no default" warning is logged. Numbers still go through str(), so default: 8443 becomes "8443". I added a null_default case to test_parse_spec_with_unfillable_server_variable. It fails on the previous commit and passes now, and tests/unittests/tools/openapi_tool passes (442).
  2. Agreed that path-level and operation-level servers are a separate follow-up. I added the one-line comment above the base_url line saying those overrides aren't supported yet.

On #8371: happy to go with whichever one the maintainers prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants