Skip to content

Commit 66086f6

Browse files
extend bytearray consistency to body-path + add regression test
Self-review caught the asymmetry: previous commit unified bytearray handling on the text-path only. All 7 adapters still used `isinstance(body, bytes)` on the body-path — a bytearray body would fall through to `str(bytearray(...))` producing the repr string `"bytearray(b'...')"` instead of the decoded content. Applied `(bytes, bytearray)` symmetrically across all 7 adapters (discord, linear, google_chat, github, whatsapp, telegram, teams). Also added a `test_handles_bytearray_body_attribute` regression test covering the new path — parameterized across the 3 static-method adapters that the test matrix reaches.
1 parent bf415ab commit 66086f6

8 files changed

Lines changed: 20 additions & 10 deletions

File tree

src/chat_sdk/adapters/discord/adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,8 @@ async def _get_request_body(self, request: Any) -> str:
14191419
if hasattr(body, "read"):
14201420
raw_read = body.read
14211421
raw = await raw_read() if inspect.iscoroutinefunction(raw_read) else raw_read()
1422-
return raw.decode("utf-8") if isinstance(raw, bytes) else raw
1423-
return body.decode("utf-8") if isinstance(body, bytes) else str(body)
1422+
return raw.decode("utf-8") if isinstance(raw, (bytes, bytearray)) else raw
1423+
return body.decode("utf-8") if isinstance(body, (bytes, bytearray)) else str(body)
14241424
text_attr = getattr(request, "text", None)
14251425
if text_attr is not None:
14261426
if callable(text_attr):

src/chat_sdk/adapters/github/adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ async def _get_request_body(request: Any) -> str:
11061106
body = body()
11071107
if inspect.isawaitable(body):
11081108
body = await body
1109-
return body.decode("utf-8") if isinstance(body, bytes) else str(body)
1109+
return body.decode("utf-8") if isinstance(body, (bytes, bytearray)) else str(body)
11101110
return ""
11111111

11121112
@staticmethod

src/chat_sdk/adapters/google_chat/adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ async def handle_webhook(
785785
raw_body = raw_body()
786786
if inspect.isawaitable(raw_body):
787787
raw_body = await raw_body
788-
body = raw_body.decode("utf-8") if isinstance(raw_body, bytes) else str(raw_body)
788+
body = raw_body.decode("utf-8") if isinstance(raw_body, (bytes, bytearray)) else str(raw_body)
789789
elif isinstance(request, dict):
790790
body = json.dumps(request)
791791
else:

src/chat_sdk/adapters/linear/adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,8 @@ async def _get_request_body(self, request: Any) -> str:
994994
if hasattr(body, "read"):
995995
raw_read = body.read
996996
raw = await raw_read() if inspect.iscoroutinefunction(raw_read) else raw_read()
997-
return raw.decode("utf-8") if isinstance(raw, bytes) else raw
998-
return body.decode("utf-8") if isinstance(body, bytes) else str(body)
997+
return raw.decode("utf-8") if isinstance(raw, (bytes, bytearray)) else raw
998+
return body.decode("utf-8") if isinstance(body, (bytes, bytearray)) else str(body)
999999
text_attr = getattr(request, "text", None)
10001000
if text_attr is not None:
10011001
if callable(text_attr):

src/chat_sdk/adapters/teams/adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,8 +1783,8 @@ async def _get_request_body(self, request: Any) -> str:
17831783
if hasattr(body, "read"):
17841784
raw_read = body.read
17851785
raw = await raw_read() if inspect.iscoroutinefunction(raw_read) else raw_read()
1786-
return raw.decode("utf-8") if isinstance(raw, bytes) else raw
1787-
return body.decode("utf-8") if isinstance(body, bytes) else str(body)
1786+
return raw.decode("utf-8") if isinstance(raw, (bytes, bytearray)) else raw
1787+
return body.decode("utf-8") if isinstance(body, (bytes, bytearray)) else str(body)
17881788
text_attr = getattr(request, "text", None)
17891789
if text_attr is not None:
17901790
if callable(text_attr):

src/chat_sdk/adapters/telegram/adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,7 @@ async def _get_request_body(request: Any) -> str:
19341934
if callable(body):
19351935
result = body()
19361936
body = await result if inspect.isawaitable(result) else result
1937-
if isinstance(body, bytes):
1937+
if isinstance(body, (bytes, bytearray)):
19381938
return body.decode("utf-8")
19391939
return str(body)
19401940
return ""

src/chat_sdk/adapters/whatsapp/adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ async def _get_request_body(request: Any) -> str:
10731073
body = body()
10741074
if inspect.isawaitable(body):
10751075
body = await body
1076-
if isinstance(body, bytes):
1076+
if isinstance(body, (bytes, bytearray)):
10771077
return body.decode("utf-8")
10781078
return str(body)
10791079
return ""

tests/test_request_body_extraction.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,13 @@ async def test_handles_bytes_body_attribute(name: str, extractor: Any) -> None:
212212
"""`request.body` as a bytes property is decoded as UTF-8."""
213213
result = await extractor(_PropertyBodyRequest(b'{"ok": true}'))
214214
assert result == '{"ok": true}', f"{name} failed bytes body attr"
215+
216+
217+
@pytest.mark.parametrize("name,extractor", _adapters())
218+
async def test_handles_bytearray_body_attribute(name: str, extractor: Any) -> None:
219+
"""`request.body` as bytearray — bytes/bytearray symmetry on the body
220+
path, catching the asymmetry where we fixed text-path for bytearray
221+
but left body-path checking only `isinstance(body, bytes)`.
222+
"""
223+
result = await extractor(_BytearrayBodyRequest(bytearray(b'{"ok": true}')))
224+
assert result == '{"ok": true}', f"{name} failed bytearray body attr"

0 commit comments

Comments
 (0)