diff --git a/fastapi/_compat.py b/fastapi/_compat.py index 227ad837d..ad35d4560 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -282,7 +282,9 @@ 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.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 @@ -524,7 +526,7 @@ else: ) -> Type[BaseModel]: BodyModel = create_model(model_name) for f in fields: - BodyModel.__fields__[f.name] = f # type: ignore[index] + BodyModel.__fields__[f.alias] = f # type: ignore[index] return BodyModel def get_model_fields(model: Type[BaseModel]) -> List[ModelField]: diff --git a/tests/test_dependency_body_duplicated_variable.py b/tests/test_dependency_body_duplicated_variable.py new file mode 100644 index 000000000..77ae8aace --- /dev/null +++ b/tests/test_dependency_body_duplicated_variable.py @@ -0,0 +1,113 @@ +from typing import Awaitable, Callable, List +from unittest.mock import ANY + +from fastapi import Body, Depends, FastAPI +from fastapi.testclient import TestClient + +app = FastAPI() + + +def make_field(name: str) -> Callable[..., Awaitable[str]]: + async def inner(value: str = Body(..., alias=name)) -> str: + return value + + return inner + + +@app.post("/example") +def example( + field_0: str = Body(...), + field_1_: str = Body(..., alias="field_1"), + field_2_: str = Depends(make_field("field_2")), + field_3_: str = Depends(make_field("field_3")), +) -> List[str]: + return [field_0, field_1_, field_2_, field_3_] + + +openapi_schema = { + "openapi": "3.1.0", + "info": {"title": "FastAPI", "version": "0.1.0"}, + "paths": { + "/example": { + "post": { + "summary": "Example", + "operationId": "example_example_post", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Body_example_example_post" + } + } + }, + "required": True, + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "title": "Response Example Example Post", + "type": "array", + "items": {"type": "string"}, + } + } + }, + }, + "422": ANY, + }, + } + } + }, + "components": { + "schemas": { + "Body_example_example_post": { + "title": "Body_example_example_post", + "type": "object", + "properties": { + "field_0": {"title": "Field 0", "type": "string"}, + "field_1": {"title": "Field 1", "type": "string"}, + "field_2": {"title": "Field 2", "type": "string"}, + "field_3": {"title": "Field 3", "type": "string"}, + }, + "required": ["field_0", "field_1", "field_2", "field_3"], + }, + "HTTPValidationError": ANY, + "ValidationError": ANY, + } + }, +} + + +client = TestClient(app) + + +def test_openapi_schema(): + response = client.get("/openapi.json") + assert response.status_code == 200, response.text + assert response.json() == openapi_schema + + +def test_valid(): + response = client.post( + "/example/", + json={"field_0": "a", "field_1": "b", "field_2": "c", "field_3": "d"}, + ) + assert response.status_code == 200, response.text + assert response.json() == ["a", "b", "c", "d"] + + +def test_missing(): + response = client.post("/example/", json={}) + assert response.status_code == 422, response.text + resp_json = response.json() + assert len(resp_json["detail"]) == 4 + assert resp_json["detail"][0]["loc"] == ["body", "field_2"] + assert str(resp_json["detail"][0]["msg"]).lower() == "field required" + assert resp_json["detail"][1]["loc"] == ["body", "field_3"] + assert str(resp_json["detail"][1]["msg"]).lower() == "field required" + assert resp_json["detail"][2]["loc"] == ["body", "field_0"] + assert str(resp_json["detail"][2]["msg"]).lower() == "field required" + assert resp_json["detail"][3]["loc"] == ["body", "field_1"] + assert str(resp_json["detail"][3]["msg"]).lower() == "field required"