Skip to content

Commit ccf971c

Browse files
fix: return 422 for invalid historyLength in REST get_task
Replace unhandled int() conversion with Pydantic validation so non-numeric historyLength query params (e.g. ?historyLength=abc) return 422 InvalidParamsError instead of 500.
1 parent cced34d commit ccf971c

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/a2a/server/request_handlers/rest_handler.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections.abc import AsyncIterable, AsyncIterator
44
from typing import TYPE_CHECKING, Any
55

6+
from pydantic import ValidationError
67
from google.protobuf.json_format import MessageToDict, MessageToJson, Parse
78

89

@@ -21,6 +22,7 @@
2122
from a2a.types import (
2223
AgentCard,
2324
GetTaskPushNotificationConfigParams,
25+
InvalidParamsError,
2426
TaskIdParams,
2527
TaskNotFoundError,
2628
TaskQueryParams,
@@ -257,8 +259,15 @@ async def on_get_task(
257259
"""
258260
task_id = request.path_params['id']
259261
history_length_str = request.query_params.get('historyLength')
260-
history_length = int(history_length_str) if history_length_str else None
261-
params = TaskQueryParams(id=task_id, history_length=history_length)
262+
try:
263+
params = TaskQueryParams(
264+
id=task_id,
265+
history_length=history_length_str if history_length_str else None,
266+
)
267+
except ValidationError:
268+
raise ServerError(
269+
error=InvalidParamsError(message='historyLength must be a valid integer')
270+
)
262271
task = await self.request_handler.on_get_task(params, context)
263272
if task:
264273
return MessageToDict(proto_utils.ToProto.task(task))

tests/server/apps/rest/test_rest_fastapi_app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,5 +399,17 @@ async def test_send_message_rejected_task(
399399
assert expected_response == actual_response
400400

401401

402+
@pytest.mark.anyio
403+
async def test_get_task_invalid_history_length_returns_422(
404+
client: AsyncClient,
405+
) -> None:
406+
"""Non-numeric historyLength query param returns 422 InvalidParamsError."""
407+
response = await client.get('/v1/tasks/some-task-id?historyLength=abc')
408+
assert response.status_code == 422
409+
data = response.json()
410+
assert 'message' in data
411+
assert 'historylength' in data['message'].lower()
412+
413+
402414
if __name__ == '__main__':
403415
pytest.main([__file__])

0 commit comments

Comments
 (0)