Skip to content

Commit 2399993

Browse files
fix(compat): send v0.3 REST request bodies with json_name keys (a2aproject#1455)
Fixes a2aproject#1454 Drops `preserving_proto_field_name=True` from the three `MessageToDict` calls (send, stream, push config create), so the body uses `json_name` keys like the spec requires. Against a2a-go's v0.3 server, a send went from `400 "message is required"` to `200` with the agent's reply. The three new tests in `test_rest_transport.py` fail on main. The cross-version tests against a2a-sdk 0.3.24 still pass. Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent f451ed7 commit 2399993

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎src/a2a/compat/v0_3/rest_transport.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async def send_message(
101101
'POST',
102102
'/v1/message:send',
103103
context=context,
104-
json=MessageToDict(req_proto, preserving_proto_field_name=True),
104+
json=MessageToDict(req_proto),
105105
)
106106

107107
resp_proto = ParseDict(
@@ -146,7 +146,7 @@ async def send_message_streaming(
146146
'POST',
147147
'/v1/message:stream',
148148
context=context,
149-
json=MessageToDict(req_proto, preserving_proto_field_name=True),
149+
json=MessageToDict(req_proto),
150150
):
151151
yield event
152152

@@ -223,7 +223,7 @@ async def create_task_push_notification_config(
223223
'POST',
224224
f'/v1/tasks/{request.task_id}/pushNotificationConfigs',
225225
context=context,
226-
json=MessageToDict(req_proto, preserving_proto_field_name=True),
226+
json=MessageToDict(req_proto),
227227
)
228228
resp_proto = ParseDict(
229229
response_data,

‎tests/compat/v0_3/test_rest_transport.py‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,86 @@ async def mock_execute_request(method, path, context=None, json=None):
681681
assert sent['configuration']['blocking'] is True
682682

683683

684+
@pytest.mark.asyncio
685+
async def test_compat_rest_transport_send_message_uses_json_names(transport):
686+
"""The v0.3 spec requires json_name keys in HTTP+JSON bodies."""
687+
sent: dict = {}
688+
689+
async def mock_execute_request(method, path, context=None, json=None):
690+
sent.update(json)
691+
return {}
692+
693+
transport._execute_request = mock_execute_request
694+
695+
req = SendMessageRequest(
696+
message=Message(
697+
message_id='msg-1',
698+
context_id='ctx-1',
699+
task_id='task-1',
700+
role=Role.ROLE_USER,
701+
),
702+
configuration=SendMessageConfiguration(
703+
accepted_output_modes=['text/plain']
704+
),
705+
)
706+
await transport.send_message(req)
707+
708+
assert 'request' not in sent
709+
assert sent['message']['messageId'] == 'msg-1'
710+
assert sent['message']['contextId'] == 'ctx-1'
711+
assert sent['message']['taskId'] == 'task-1'
712+
assert sent['configuration']['acceptedOutputModes'] == ['text/plain']
713+
714+
715+
@pytest.mark.asyncio
716+
async def test_compat_rest_transport_send_message_streaming_uses_json_names(
717+
transport,
718+
):
719+
sent: dict = {}
720+
721+
async def mock_send_stream_request(method, path, context=None, json=None):
722+
sent.update(json)
723+
yield StreamResponse(message=Message(message_id='msg-123'))
724+
725+
transport._send_stream_request = mock_send_stream_request
726+
727+
req = SendMessageRequest(
728+
message=Message(
729+
message_id='msg-1', context_id='ctx-1', role=Role.ROLE_USER
730+
)
731+
)
732+
_ = [event async for event in transport.send_message_streaming(req)]
733+
734+
assert 'request' not in sent
735+
assert sent['message']['messageId'] == 'msg-1'
736+
assert sent['message']['contextId'] == 'ctx-1'
737+
738+
739+
@pytest.mark.asyncio
740+
async def test_compat_rest_transport_create_task_push_notification_config_uses_json_names(
741+
transport,
742+
):
743+
sent: dict = {}
744+
745+
async def mock_execute_request(method, path, context=None, json=None):
746+
sent.update(json)
747+
return {
748+
'name': 'tasks/task-123/pushNotificationConfigs/push-123',
749+
'pushNotificationConfig': {'url': 'http://push', 'id': 'push-123'},
750+
}
751+
752+
transport._execute_request = mock_execute_request
753+
754+
req = TaskPushNotificationConfig(
755+
task_id='task-123', id='push-123', url='http://push'
756+
)
757+
await transport.create_task_push_notification_config(req)
758+
759+
assert sent['configId'] == 'push-123'
760+
assert sent['config']['pushNotificationConfig']['id'] == 'push-123'
761+
assert 'config_id' not in sent
762+
763+
684764
@pytest.mark.asyncio
685765
async def test_compat_rest_transport_get_task_no_history(transport):
686766
async def mock_execute_request(method, path, context=None, params=None):

0 commit comments

Comments
 (0)