Skip to content

fix(tools): reject blank RestApiTool path parameters - #8034

Open
benclarkeio wants to merge 5 commits into
google:mainfrom
benclarkeio:fix/rest-api-tool-blank-path-param
Open

benclarkeio wants to merge 5 commits into
google:mainfrom
benclarkeio:fix/rest-api-tool-blank-path-param

Conversation

@benclarkeio

@benclarkeio benclarkeio commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

An omitted path parameter already returns a retryable error. A None value can instead reach the server as /pets/None/toys, and a blank value can leave a whole path segment empty or whitespace-only. The resulting server error often gives the caller little information about which argument to fix.

Solution:

  • Treat None as a missing path argument. A required parameter with a schema default uses the default when its argument is None, as it does when the argument is omitted.
  • Reject substitutions that leave a segment blank or reduce it to a dot segment. Allow blank values within a segment that has other content, such as /exports/{suffix}.csv.
  • Use the argument name exposed by the tool in retry errors, including for the existing dot-segment check. The tool returns an {"error": ...} response before making an HTTP request.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
$ pytest tests/unittests/tools/openapi_tool
440 passed

Tests cover missing and null arguments with and without a schema default, blank and dot segments, and empty or whitespace-only values embedded in a filename.

Manual End-to-End (E2E) Tests:

The repro from #8032 (plus a None value), after the change:

{} -> InputValidationError: Missing required path parameter 'pet_id'.
{'pet_id': None} -> InputValidationError: Missing required path parameter 'pet_id'.
{'pet_id': '..'} -> InputValidationError: Path parameter 'pet_id' must not contain current-directory or parent-directory segments.
{'pet_id': ''} -> InputValidationError: Path parameter 'pet_id' must not be empty.
{'pet_id': '  '} -> InputValidationError: Path parameter 'pet_id' must not be empty.

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.

- Raise InputValidationError naming the parameter when a path parameter
  value is empty or whitespace-only, so the model gets a retryable tool
  error instead of a request to a path with an empty segment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

None still gets through because _to_param_string(None) becomes "None", so the request goes to /pets/None/toys. Treat null like a blank value.

Suggested change
name=original_k, value="" if v is None else _to_param_string(v)

Here's a commit you can cherry pick if that's easier: its-amann@ec80409

@its-amann

its-amann commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

looks good, thanks for picking it up. could you add Co-authored-by: Aman Agnihotri <amanagnihotri902@gmail.com> to 7dcd392 so the None path param fix keeps credit

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran this branch on Python 3.12: test_rest_api_tool.py passes (170 passed). The blank and dot segment handling with Formatter().parse looks right to me, and /exports/{petId}.csv staying allowed is a good call.

One interaction to consider before merge. call() fills schema defaults only when the key is absent:

# rest_api_tool.py ~L699
for api_param in api_params:
  if api_param.py_name not in api_args:
    if api_param.required and ... and api_param.param_schema.default is not None:
      api_args[api_param.py_name] = api_param.param_schema.default

Now that _prepare_request_params treats None as missing, an explicit None skips the default and errors out. I checked with a required petId path param that has schema.default="fido" and a mocked _request:

args result
{} request to /pets/fido/toys
{"pet_id": None} Missing required path parameter 'pet_id'., no request
{"pet_id": " "} Path parameter 'pet_id' must not be empty., no request

The third row is right. The second is better than main (which would send /pets/None/toys), but the model asked for "no value" and a default exists, so the request should probably go out with fido. Models do send explicit nulls for optional-looking fields fairly often. Checking api_args.get(api_param.py_name) is None in that loop, at least for param_location == "path", would make the two places agree. A parametrize case for {"user_id": None} with a default would pin it. If you'd rather keep this PR narrow, that's reasonable too; it could go in #8032 as a follow-up.

Minor: the new blank_path_params: dict[str, str] maps original name to py_name, but _encode_path_param now also gets name=param_k. A short comment on the dict saying "original_name -> name the model sees" would save the next reader a trip through params_map.

benclarkeio and others added 4 commits November 16, 2026 16:40
- Skip a path parameter whose value is None, so it is reported as a
  missing required parameter instead of being sent as the string "None".
- Cover the None case in the missing path parameter test.

Co-authored-by: Aman Agnihotri <amanagnihotri902@gmail.com>
- Pass the parameter's Python name to _encode_path_param, so the empty
  and dot-segment errors say 'pet_id' like the missing parameter error
  does, instead of the spec's 'petId'.
- Allow blank substitutions when the segment still contains literal or parameter content.
- Keep retryable errors for blank and dot segments using the exposed argument name.
- Exercise both rejected segments and permitted filename substitutions.
- Fill a required parameter's schema default when its argument is None,
  not only when it is absent, so an explicit null uses the default
  instead of returning a missing path parameter error.
- Cover a None argument in the required parameter defaults test.
- Note what blank_path_params maps.
@benclarkeio
benclarkeio force-pushed the fix/rest-api-tool-blank-path-param branch from 3f38849 to 53a3a32 Compare November 16, 2026 16:58
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.

RestApiTool sends empty or whitespace-only path parameters

4 participants