mirror of https://github.com/tiangolo/fastapi.git
🐛 Fix validating form params declared with classes (list, tuple, set, etc) (#856)
This commit is contained in:
parent
5db99a27cf
commit
874d24181e
|
|
@ -629,9 +629,9 @@ async def request_body_to_args(
|
||||||
for field in required_params:
|
for field in required_params:
|
||||||
value: Any = None
|
value: Any = None
|
||||||
if received_body is not None:
|
if received_body is not None:
|
||||||
if field.shape in sequence_shapes and isinstance(
|
if (
|
||||||
received_body, FormData
|
field.shape in sequence_shapes or field.type_ in sequence_types
|
||||||
):
|
) and isinstance(received_body, FormData):
|
||||||
value = received_body.getlist(field.alias)
|
value = received_body.getlist(field.alias)
|
||||||
else:
|
else:
|
||||||
value = received_body.get(field.alias)
|
value = received_body.get(field.alias)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
from fastapi import FastAPI, Form
|
||||||
|
from starlette.testclient import TestClient
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/form/python-list")
|
||||||
|
def post_form_param_list(items: list = Form(...)):
|
||||||
|
return items
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/form/python-set")
|
||||||
|
def post_form_param_set(items: set = Form(...)):
|
||||||
|
return items
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/form/python-tuple")
|
||||||
|
def post_form_param_tuple(items: tuple = Form(...)):
|
||||||
|
return items
|
||||||
|
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_python_list_param_as_form():
|
||||||
|
response = client.post(
|
||||||
|
"/form/python-list", data={"items": ["first", "second", "third"]}
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == ["first", "second", "third"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_python_set_param_as_form():
|
||||||
|
response = client.post(
|
||||||
|
"/form/python-set", data={"items": ["first", "second", "third"]}
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert set(response.json()) == {"first", "second", "third"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_python_tuple_param_as_form():
|
||||||
|
response = client.post(
|
||||||
|
"/form/python-tuple", data={"items": ["first", "second", "third"]}
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == ["first", "second", "third"]
|
||||||
Loading…
Reference in New Issue