Skip to content

fix(tools): substitute OpenAPI path parameters whose names contain dots - #8176

Open
abhishekacharya200 wants to merge 1 commit into
google:mainfrom
abhishekacharya200:fix-openapi-dotted-path-params
Open

abhishekacharya200 wants to merge 1 commit into
google:mainfrom
abhishekacharya200:fix-openapi-dotted-path-params

Conversation

@abhishekacharya200

Copy link
Copy Markdown

Link to Issue or Description of Change

2. Or, if no issue exists, describe the change:

Problem:
RestApiTool builds the request path with str.format(**path_params). str.format reads . and [ in a field name as attribute and index access, so an OpenAPI path like /files/{file.name} looks up file and raises KeyError: 'file' before the request is sent. Dotted names are valid in OpenAPI path templates (RFC 6570 allows . in variable names).

Repro:

from google.adk.tools.openapi_tool.openapi_spec_parser.openapi_spec_parser import OpenApiSpecParser
from google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool import RestApiTool

spec = {
    "openapi": "3.0.0",
    "info": {"title": "t", "version": "1"},
    "servers": [{"url": "https://example.com"}],
    "paths": {"/files/{file.name}": {"get": {
        "operationId": "getFile",
        "parameters": [{"name": "file.name", "in": "path", "required": True, "schema": {"type": "string"}}],
        "responses": {"200": {"description": "ok"}},
    }}},
}
tool = RestApiTool.from_parsed_operation(OpenApiSpecParser().parse(spec)[0])
tool._prepare_request_params(tool._operation_parser.get_parameters(), {"file_name": "a.txt"})
# KeyError: 'file'

Solution:
Replace each {name} placeholder by looking up the whole name in the encoded path parameters. A placeholder with no value still raises KeyError with its name, so the existing missing-parameter error is unchanged.

Testing Plan

Unit Tests:

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

Added test_prepare_request_params_path_param_name_with_dot_or_bracket (file.name, items[0]). It fails with KeyError before the change.

pytest tests/unittests/tools/openapi_tool/
437 passed

Manual End-to-End (E2E) Tests:

Ran the repro above. Before: KeyError: 'file'. After: the request URL is https://example.com/files/a.txt.

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.

RestApiTool filled the path template with str.format, which reads "." and
"[" in a field name as attribute and index access. A path such as
/files/{file.name} looked up "file" and raised KeyError before any request
was sent. Replace each {name} placeholder by looking up the whole name.
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.

2 participants