Repository navigation
fix(tools): reject blank RestApiTool path parameters - #8034
benclarkeio wants to merge 5 commits into
Conversation
- 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.
There was a problem hiding this comment.
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.
| 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
|
looks good, thanks for picking it up. could you add |
mangeshraut712
left a comment
There was a problem hiding this comment.
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.defaultNow 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.
- 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.
3f38849 to
53a3a32
Compare
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
Nonevalue 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:
Noneas a missing path argument. A required parameter with a schema default uses the default when its argument isNone, as it does when the argument is omitted./exports/{suffix}.csv.{"error": ...}response before making an HTTP request.Testing Plan
Unit Tests:
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
Nonevalue), after the change:Checklist