failed NO_PATCH NO_PATCH UNSUBMITTED empty_patch(None) · None tool calls · 0 s · fastapi/fastapi
🐛 Fix validation error when `File` is declared after `Form` parameter This PR updates the fix (6185a92) from @jsg921019 so that tests pass with the latest FastAPI code (0.110.0). It also adds a test to cover the case with multiple files, and another to ensure the order of parameters in an endpoint definition does not affect the way FastAPI handles the request. **Problem** When declaring an endpoint which accepts a `File` but also a `Form` parameter, the `File` parameter must be declared first or the endpoint would respond with error 422. **Context** - See discussion #9116 - And probably also #4625
| # | Tool | Arguments | Result |
|---|---|---|---|
| No trace captured. | |||
.F
=================================== FAILURES ===================================
____________________ test_file_form_order[/file_after_form] ____________________
endpoint_path = '/file_after_form'
tmp_file_1 = PosixPath('/private/tmp/swe_work/baseline_full_v2/fastapi_11194/b/tmp/pytest-of-jp/pytest-0/test_file_form_order__file_aft0/example1.txt')
@pytest.mark.parametrize("endpoint_path", ("/file_before_form", "/file_after_form"))
def test_file_form_order(endpoint_path: str, tmp_file_1: Path):
response = client.post(
url=endpoint_path,
data={"city": "Thimphou"},
files={"file": (tmp_file_1.name, tmp_file_1.read_bytes())},
)
> assert response.status_code == 200, response.text
E AssertionError: {"detail":[{"type":"bytes_type","loc":["body","file"],"msg":"Input should be a valid bytes","input":{"filename":"example1.txt","file":{"_file":{},"_max_size":1048576,"_rolled":false,"_TemporaryFileArgs":{"mode":"w+b","buffering":-1,"suffix":null,"prefix":null,"encoding":null,"newline":null,"dir":null,"errors":null}},"size":3,"headers":{"content-disposition":"form-data; name=\"file\"; filename=\"example1.txt\"","content-type":"text/plain"},"_max_mem_size":1048576}}]}
E assert 422 == 200
E + where 422 = <Response [422 Unprocessable Entity]>.status_code
tests/test_file_and_form_order_issue_9116.py:73: AssertionError
=============================== warnings summary ===============================
../../../../../../../Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.48.0-py3-none-any/starlette/testclient.py:51
/Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.48.0-py3-none-any/starlette/testclient.py:51: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
_PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed, 1 passed, 1 warning in 0.49s