diff --git a/fastapi/_compat.py b/fastapi/_compat.py index d3353a53a..1d13a0c80 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -88,7 +88,7 @@ if PYDANTIC_V2: @property def alias(self) -> str: - a = self.field_info.alias + a = self.field_info.validation_alias or self.field_info.alias return a if a is not None else self.name @property @@ -274,7 +274,7 @@ if PYDANTIC_V2: *, fields: Sequence[ModelField], model_name: str ) -> Type[BaseModel]: field_params = { - f"{f.field_info.alias}": (f.field_info.annotation, f.field_info) + f"{f.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] diff --git a/tests/test_alias_and_validation_alias_in_form.py b/tests/test_alias_and_validation_alias_in_form.py new file mode 100644 index 000000000..b3c85d9de --- /dev/null +++ b/tests/test_alias_and_validation_alias_in_form.py @@ -0,0 +1,47 @@ +from starlette.testclient import TestClient + +from fastapi import FastAPI, Form + +app: FastAPI = FastAPI() + + +@app.post("/testing_alias") +async def check_alias(id_test: int = Form(alias="otherId")): + return {"other_id": id_test} + +@app.post("/testing_validation_alias") +async def check_validation_alias(id_test: int = Form(validation_alias="otherId")): + return {"other_id": id_test} + + +client = TestClient(app) + + +def test_get_alias(): + response = client.post("/testing_alias", data={"otherId": "1"}) + assert response.status_code == 200 + assert response.json() == {"other_id": 1} + +def test_get_validation_alias(): + response = client.post("/testing_validation_alias", data={"otherId": "1"}) + assert response.status_code == 200 + assert response.json() == {"other_id": 1} + +def test_file_alias_schema(): + response = client.get("/openapi.json") + assert response.status_code == 200, response.text + schema = response.json() + + assert ( + "otherId" + in schema["components"]["schemas"]["Body_check_alias_testing_alias_post"][ + "properties" + ] + ) + + assert ( + "otherId" + in schema["components"]["schemas"]["Body_check_validation_alias_testing_validation_alias_post"][ + "properties" + ] + ) diff --git a/tests/test_alias_in_form.py b/tests/test_alias_in_form.py deleted file mode 100644 index e8a11b6c7..000000000 --- a/tests/test_alias_in_form.py +++ /dev/null @@ -1,29 +0,0 @@ -from fastapi import FastAPI, Form -from starlette.testclient import TestClient - -app: FastAPI = FastAPI() - - -@app.post("/testing_alias") -async def check_alias(id_test: int = Form(alias="otherId")): - return {"other_id": id_test} - - -@app.patch("/testing") -async def check_without_alias(id_test: 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}