Repository navigation
Commit 257626b
fix: reject decoded values that are not objects in parse_lenient (a2aproject#1431)
## Problem
`parse_lenient` calls `ParseDict(..., ignore_unknown_fields=True)`.
`ParseDict` iterates the decoded value as if it were an object's keys.
So when a decoded value is not a `dict`:
- `[]` parses as an empty message
- `None`, a number or a boolean raises a bare `TypeError` (`'NoneType'
object is not iterable`)
The REST and JSON-RPC client transports pass decoded response JSON to
`parse_lenient`. Here is `RestTransport.get_task` against a server that
answers `200` with each body, on main:
```
client get_task, 200 []: returned Task(id='')
client get_task, 200 null: TypeError: 'NoneType' object is not iterable
```
The caller gets an empty `Task` with no error. The JSON-RPC transport
does the same with a `"result"` of `[]` or `null`.
## Fix
`parse_lenient` raises `ParseError` when a decoded value is not a
`dict`. Raw text and bytes still go straight to `Parse`, unchanged.
With the fix:
```
client get_task, 200 []: ParseError: Expected a JSON object for Task, got list.
client get_task, 200 null: ParseError: Expected a JSON object for Task, got NoneType.
```
This matches `test_malformed_known_field_still_raises` from a2aproject#1354:
unknown fields are ignored, but a malformed payload raises `ParseError`.
## Tests
- `tests/utils/test_proto_utils.py`: decoded values `[]`, `None`, `0`,
`True` raise `ParseError`. A separate test checks that a `str` goes to
the raw-text branch (`'id'` fails JSON decoding there, not the object
check).
- `tests/client/transports/test_rest_client.py`: `get_task` with a body
of `[]` or `null`.
- `tests/client/transports/test_jsonrpc_client.py`: `get_task` with a
`result` of `[]` or `null`.
The 8 new `ParseError` cases fail on main. Two existing REST tests,
`test_send_message_with_timeout_context` and
`test_send_message_with_default_extensions`, never set a response body.
The mocked `json()` returned a `MagicMock`, which only parsed because of
this bug, so they now return `{}`.
- `bash scripts/lint.sh`: success. The 3 ruff diagnostics it prints are
in `client_factory.py` and `transports/__init__.py`, which this change
does not touch.
- `uv run pytest` on Windows, Python 3.10, skipping
`tests/integration/cross_version` and
`tests/integration/push_notifications`, which do not run on Windows
(`os.killpg`, spawn-mode multiprocessing): 2557 passed, 207 skipped, 4
xfailed, 1 xpassed
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>1 parent 2ba8e7b commit 257626b
4 files changed
Lines changed: 71 additions & 1 deletion
File tree
- src/a2a/utils
- tests
- client/transports
- utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
82 | 90 | | |
83 | 91 | | |
84 | 92 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
829 | 829 | | |
830 | 830 | | |
831 | 831 | | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
832 | 850 | | |
833 | 851 | | |
834 | 852 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
| 235 | + | |
235 | 236 | | |
236 | 237 | | |
237 | 238 | | |
| |||
305 | 306 | | |
306 | 307 | | |
307 | 308 | | |
| 309 | + | |
308 | 310 | | |
309 | 311 | | |
310 | 312 | | |
| |||
918 | 920 | | |
919 | 921 | | |
920 | 922 | | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
482 | 482 | | |
483 | 483 | | |
484 | 484 | | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
0 commit comments