From 297d2594f8363e16ff0737042e37009c452841c9 Mon Sep 17 00:00:00 2001
From: harol <halvaradov@newport.com.pe>
Date: Sun, 24 Sep 2023 22:12:24 -0500
Subject: [PATCH] Fix not consider alias in Form

---
 .gitignore                  |  1 +
 fastapi/_compat.py          |  2 +-
 tests/test_alias_in_form.py | 28 ++++++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 tests/test_alias_in_form.py

diff --git a/.gitignore b/.gitignore
index 9be494cec..e5a2765fc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -28,3 +28,4 @@ archive.zip
 
 # macOS
 .DS_Store
+.python-version
diff --git a/fastapi/_compat.py b/fastapi/_compat.py
index eb55b08f2..25225baef 100644
--- a/fastapi/_compat.py
+++ b/fastapi/_compat.py
@@ -262,7 +262,7 @@ if PYDANTIC_V2:
     def create_body_model(
         *, fields: Sequence[ModelField], model_name: str
     ) -> Type[BaseModel]:
-        field_params = {f.name: (f.field_info.annotation, f.field_info) for f in fields}
+        field_params = {f.field_info.alias: (f.field_info.annotation, f.field_info) for f in fields}
         BodyModel: Type[BaseModel] = create_model(model_name, **field_params)  # type: ignore[call-overload]
         return BodyModel
 
diff --git a/tests/test_alias_in_form.py b/tests/test_alias_in_form.py
new file mode 100644
index 000000000..3459b90bc
--- /dev/null
+++ b/tests/test_alias_in_form.py
@@ -0,0 +1,28 @@
+from typing import Annotated
+
+from fastapi import FastAPI, Form
+from starlette.testclient import TestClient
+
+
+app:FastAPI = FastAPI()
+
+@app.post("/testing_alias")
+async def check_alias(id_test: Annotated[int, Form(alias="otherId")]):
+    return {"other_id":id_test}
+
+@app.patch("/testing")
+async def check_without_alias(id_test:Annotated[int, Form()]):
+    return {"id_test":id_test}
+
+
+client = TestClient(app)
+
+def test_without_alias():
+    response = client.patch("/testing", data={"id_test":1})
+    assert response.status_code == 200
+    assert response.json() == {"id_test":1}
+
+def test_get_alias():
+    response = client.post("/testing_alias", data={"otherId":"1"})
+    assert response.status_code == 200
+    assert response.json() == {"other_id":1}
\ No newline at end of file