Skip to content

fix: ensure BodyPartReader.read() returns bytes instead of bytearray#12409

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

fix: ensure BodyPartReader.read() returns bytes instead of bytearray#12409
armorbreak001 wants to merge 1 commit intoaio-libs:masterfrom
armorbreak001:fix/multipart-read-bytes-return-type

Conversation

@armorbreak001
Copy link
Copy Markdown

Summary

BodyPartReader.read() documents its return type as bytes, but both the decoded and raw code paths returned the internal bytearray buffer unchanged. This caused TypeError: Object of type bytearray is not JSON serializable when piping multipart data into json.dumps, msgpack, or any other serializer with strict type requirements.

Changes

  • Convert decoded_data (decode=True path) to bytes() before returning
  • Convert data (raw path) to bytes() before returning

Both are 2-line changes in aiohttp/multipart.py that make the return values match the documented API contract.

Fixes #12404

The read() method's return type annotation promises bytes, but both
the decoded and raw paths returned the internal bytearray buffer.
This caused TypeError with json.dumps and other serializers that
don't accept bytearray.

Convert both return values to bytes() to match the documented API.
@armorbreak001 armorbreak001 requested a review from asvetlov as a code owner April 22, 2026 05:15
@Dreamsorcerer
Copy link
Copy Markdown
Member

The PR description clearly repeats the disputed details in the bug report, suggesting no person has actually looked at the 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