]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:bug: Fix validating form params declared with classes (list, tuple, set, etc) (...
authorNik <snive2013@yandex.ru>
Fri, 17 Jan 2020 11:45:55 +0000 (14:45 +0300)
committerSebastián Ramírez <tiangolo@gmail.com>
Fri, 17 Jan 2020 11:45:55 +0000 (12:45 +0100)
fastapi/dependencies/utils.py
tests/test_forms_from_non_typing_sequences.py [new file with mode: 0644]

index 956fffff472de5809f852ffce70f6ecfabd14bdf..a1cc0b9808d4cd15844aabada18454b4ae0b9843 100644 (file)
@@ -629,9 +629,9 @@ async def request_body_to_args(
         for field in required_params:
             value: Any = None
             if received_body is not None:
-                if field.shape in sequence_shapes and isinstance(
-                    received_body, FormData
-                ):
+                if (
+                    field.shape in sequence_shapes or field.type_ in sequence_types
+                ) and isinstance(received_body, FormData):
                     value = received_body.getlist(field.alias)
                 else:
                     value = received_body.get(field.alias)
diff --git a/tests/test_forms_from_non_typing_sequences.py b/tests/test_forms_from_non_typing_sequences.py
new file mode 100644 (file)
index 0000000..0e47e90
--- /dev/null
@@ -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"]