Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using MultipartWriter.append_json breaks in 3.9.4 with AssertionError "assert CONTENT_DISPOSITION in payload.headers" #8326

Closed
1 task done
nicolasdialpad opened this issue Apr 12, 2024 · 8 comments · Fixed by #8335
Labels

Comments

@nicolasdialpad
Copy link

Describe the bug

After upgrading to 3.9.4 the following example code that used to work in 3.9.3 starts failing. I think the issue was introduced in 7d0be3f

To Reproduce

import aiohttp
import asyncio


async def main():
    async with aiohttp.ClientSession() as session:
        mpwriter = aiohttp.MultipartWriter('form-data')
        mpwriter.append_json({'a': 1}) 
        response = await session.post('https://httpbingo.org/post', data=mpwriter)
        print(await response.text())


asyncio.run(main())

Expected behavior

This should print a text response, but instead it fails with an AssertionError

Logs/tracebacks

Traceback (most recent call last):
  File "/Users/nico/test.py", line 13, in <module>
    asyncio.run(main())
  File "/opt/homebrew/Cellar/python@3.9/3.9.18_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/opt/homebrew/Cellar/python@3.9/3.9.18_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/Users/nico/test.py", line 8, in main
    mpwriter.append_json({'a': 1})
  File "/Users/nico/virtualenvs/gaevenv3.9/lib/python3.9/site-packages/aiohttp/multipart.py", line 887, in append_json
    return self.append_payload(JsonPayload(obj, headers=headers))
  File "/Users/nico/virtualenvs/gaevenv3.9/lib/python3.9/site-packages/aiohttp/multipart.py", line 851, in append_payload
    assert CONTENT_DISPOSITION in payload.headers
AssertionError


### Python Version

```console
Python 3.9.18

aiohttp Version

Name: aiohttp
Version: 3.9.4
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author:
Author-email:
License: Apache 2
Location: /Users/nico/virtualenvs/gaevenv3.9/lib/python3.9/site-packages
Requires: aiosignal, async-timeout, attrs, frozenlist, multidict, yarl
Required-by: gcloud-aio-auth

multidict Version

Name: multidict
Version: 5.2.0
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /Users/nico/virtualenvs/gaevenv3.9/lib/python3.9/site-packages
Requires:
Required-by: aiohttp, sanic, yarl

yarl Version

Name: yarl
Version: 1.9.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache-2.0
Location: /Users/nico/virtualenvs/gaevenv3.9/lib/python3.9/site-packages
Requires: idna, multidict
Required-by: aiohttp

OS

macOS 14.1.1

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@Olegt0rr
Copy link
Contributor

Olegt0rr commented Apr 13, 2024

@Dreamsorcerer
Copy link
Member

Through these convenience methods, we should probably add Content-Disposition automatically. But, I guess the one question is how to name the parts? Shouldn't there be an expected name by the receiving service, which the user should be using?

@Dreamsorcerer
Copy link
Member

Note that these are just asserts, so you can likely use python -O as a workaround at the moment.

@Olegt0rr
Copy link
Contributor

Olegt0rr commented Apr 15, 2024

Note that these are just asserts, so you can likely use python -O as a workaround at the moment.

But this update breaks all existing tests

What's wrong with this usage?
https://github.com/aiogram/aiogram/blob/b49939aaff2b1e37520a207b01ea124412641d01/aiogram/webhook/aiohttp_server.py#L155-L163

What should I edit to make 3.9.4 works?
It seems like there's a dead end here with new assertions

@Dreamsorcerer
Copy link
Member

What's wrong with this usage?

Wait, that code does set content-disposition. Why are we getting the error there too?
I was just looking at the code in the start of this issue, which doesn't set content-disposition.

Think you can create a test quickly in a PR here?

@Olegt0rr
Copy link
Contributor

What's wrong with this usage?

Wait, that code does set content-disposition. Why are we getting the error there too? I was just looking at the code in the start of this issue, which doesn't set content-disposition.

Think you can create a test quickly in a PR here?

Sure, wait a minute

@Dreamsorcerer
Copy link
Member

Ah, wait, you're getting the payload after the append and then setting it, but the check is happening during the append. Maybe we can move the check later...

@Olegt0rr
Copy link
Contributor

#8332

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants