|
|
|
@ -1,11 +1,9 @@ |
|
|
|
""" |
|
|
|
Regression test, Error 422 if Form is declared before File |
|
|
|
See https://github.com/tiangolo/fastapi/discussions/9116 |
|
|
|
|
|
|
|
Showcases regression introduced in commit ab2b86f |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
import pathlib |
|
|
|
from pathlib import Path |
|
|
|
from typing import List |
|
|
|
|
|
|
|
import pytest |
|
|
|
@ -18,16 +16,16 @@ app = FastAPI() |
|
|
|
|
|
|
|
@app.post("/file_before_form") |
|
|
|
def file_before_form( |
|
|
|
file: bytes = File(...), |
|
|
|
city: str = Form(...), |
|
|
|
file: bytes = File(), |
|
|
|
city: str = Form(), |
|
|
|
): |
|
|
|
return {"file_content": file, "city": city} |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/file_after_form") |
|
|
|
def file_after_form( |
|
|
|
city: str = Form(...), |
|
|
|
file: bytes = File(...), |
|
|
|
city: str = Form(), |
|
|
|
file: bytes = File(), |
|
|
|
): |
|
|
|
return {"file_content": file, "city": city} |
|
|
|
|
|
|
|
@ -35,14 +33,14 @@ def file_after_form( |
|
|
|
@app.post("/file_list_before_form") |
|
|
|
def file_list_before_form( |
|
|
|
files: Annotated[List[bytes], File()], |
|
|
|
city: Annotated[str, Form(...)], |
|
|
|
city: Annotated[str, Form()], |
|
|
|
): |
|
|
|
return {"file_contents": files, "city": city} |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/file_list_after_form") |
|
|
|
def file_list_after_form( |
|
|
|
city: Annotated[str, Form(...)], |
|
|
|
city: Annotated[str, Form()], |
|
|
|
files: Annotated[List[bytes], File()], |
|
|
|
): |
|
|
|
return {"file_contents": files, "city": city} |
|
|
|
@ -52,21 +50,21 @@ client = TestClient(app) |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
def tmp_file_1(tmp_path) -> pathlib.Path: |
|
|
|
def tmp_file_1(tmp_path: Path) -> Path: |
|
|
|
f = tmp_path / "example1.txt" |
|
|
|
f.write_text("foo") |
|
|
|
return f |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
def tmp_file_2(tmp_path) -> pathlib.Path: |
|
|
|
def tmp_file_2(tmp_path: Path) -> Path: |
|
|
|
f = tmp_path / "example2.txt" |
|
|
|
f.write_text("bar") |
|
|
|
return f |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("endpoint_path", ("/file_before_form", "/file_after_form")) |
|
|
|
def test_file_form_order(endpoint_path: str, tmp_file_1): |
|
|
|
def test_file_form_order(endpoint_path: str, tmp_file_1: Path): |
|
|
|
response = client.post( |
|
|
|
url=endpoint_path, |
|
|
|
data={"city": "Thimphou"}, |
|
|
|
@ -79,7 +77,7 @@ def test_file_form_order(endpoint_path: str, tmp_file_1): |
|
|
|
@pytest.mark.parametrize( |
|
|
|
"endpoint_path", ("/file_list_before_form", "/file_list_after_form") |
|
|
|
) |
|
|
|
def test_file_list_form_order(endpoint_path: str, tmp_file_1, tmp_file_2): |
|
|
|
def test_file_list_form_order(endpoint_path: str, tmp_file_1: Path, tmp_file_2: Path): |
|
|
|
response = client.post( |
|
|
|
url=endpoint_path, |
|
|
|
data={"city": "Thimphou"}, |
|
|
|
|