Skip to content

fix(multipart): ensure BodyPartReader.read() returns bytes not bytearray#12414

Closed
armorbreak001 wants to merge 1 commit intoaio-libs:masterfrom
armorbreak001:fix/multipart-bytearray-read
Closed

fix(multipart): ensure BodyPartReader.read() returns bytes not bytearray#12414
armorbreak001 wants to merge 1 commit intoaio-libs:masterfrom
armorbreak001:fix/multipart-bytearray-read

Conversation

@armorbreak001
Copy link
Copy Markdown

Summary

documents its return type as bytes, but internally builds the result in a bytearray and returns it directly. This means callers receive a bytearray instead of bytes, which breaks code that passes the result to json.dumps() or other APIs that don't accept bytearray.

Reproduction

From #12404:

reader = await request.multipart()
part = await reader.next()
data = await part.read(decode=True)
print(type(data))  # <class 'bytearray'>, should be <class 'bytes'>
json.dumps({"filename": data})  # TypeError!

Fix

Convert the internal bytearray to bytes before returning from read(). This is a minimal, backward-compatible change — bytes(bytearray) is a no-op copy for the common case.

Fixes #12404

The read() method built its result in a bytearray but returned
it directly, violating the documented return type of bytes.
This caused TypeError when passing the result to json.dumps()
or other APIs that expect bytes but not bytearray.
@armorbreak001 armorbreak001 requested a review from asvetlov as a code owner April 22, 2026 15:07
@armorbreak001
Copy link
Copy Markdown
Author

Duplicate of #12409 (already open, same issue #12404). This is the 3rd attempt on this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: BodyPartReader.filename and read() leak bytearray instead of str/bytes, violating API contract and breaking JSON serialization

2 participants