]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🐛 Fix form field regression (#12194)
authorNico Tonnhofer <github@wurstnase.de>
Fri, 13 Sep 2024 09:51:00 +0000 (11:51 +0200)
committerGitHub <noreply@github.com>
Fri, 13 Sep 2024 09:51:00 +0000 (11:51 +0200)
fastapi/dependencies/utils.py
tests/test_forms_single_model.py

index f18eace9d430e397a3dac5125ba8ccbea4cdec39..7548cf0c7932abdb7586b6d4b548ce988b4bd6e1 100644 (file)
@@ -788,7 +788,7 @@ async def _extract_form_body(
                     tg.start_soon(process_fn, sub_value.read)
             value = serialize_sequence_value(field=field, value=results)
         if value is not None:
-            values[field.name] = value
+            values[field.alias] = value
     for key, value in received_body.items():
         if key not in values:
             values[key] = value
index 7ed3ba3a2df55b512c18fdc2845f5dc3d7a85fac..880ab3820077229ce43c7c6a62929a79bfcea033 100644 (file)
@@ -3,7 +3,7 @@ from typing import List, Optional
 from dirty_equals import IsDict
 from fastapi import FastAPI, Form
 from fastapi.testclient import TestClient
-from pydantic import BaseModel
+from pydantic import BaseModel, Field
 from typing_extensions import Annotated
 
 app = FastAPI()
@@ -14,6 +14,7 @@ class FormModel(BaseModel):
     lastname: str
     age: Optional[int] = None
     tags: List[str] = ["foo", "bar"]
+    alias_with: str = Field(alias="with", default="nothing")
 
 
 @app.post("/form/")
@@ -32,6 +33,7 @@ def test_send_all_data():
             "lastname": "Sanchez",
             "age": "70",
             "tags": ["plumbus", "citadel"],
+            "with": "something",
         },
     )
     assert response.status_code == 200, response.text
@@ -40,6 +42,7 @@ def test_send_all_data():
         "lastname": "Sanchez",
         "age": 70,
         "tags": ["plumbus", "citadel"],
+        "with": "something",
     }
 
 
@@ -51,6 +54,7 @@ def test_defaults():
         "lastname": "Sanchez",
         "age": None,
         "tags": ["foo", "bar"],
+        "with": "nothing",
     }
 
 
@@ -100,13 +104,13 @@ def test_no_data():
                     "type": "missing",
                     "loc": ["body", "username"],
                     "msg": "Field required",
-                    "input": {"tags": ["foo", "bar"]},
+                    "input": {"tags": ["foo", "bar"], "with": "nothing"},
                 },
                 {
                     "type": "missing",
                     "loc": ["body", "lastname"],
                     "msg": "Field required",
-                    "input": {"tags": ["foo", "bar"]},
+                    "input": {"tags": ["foo", "bar"], "with": "nothing"},
                 },
             ]
         }