Browse Source

consider alias in form

pull/10319/head
harol 2 years ago
parent
commit
8c1eb0f984
  1. 2
      fastapi/_compat.py
  2. 6
      tests/test_alias_in_form.py

2
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.field_info.alias: (f.field_info.annotation, f.field_info) for f in fields}
field_params = {f"{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

6
tests/test_alias_in_form.py

@ -1,5 +1,3 @@
from typing import Annotated
from fastapi import FastAPI, Form
from starlette.testclient import TestClient
@ -7,11 +5,11 @@ from starlette.testclient import TestClient
app:FastAPI = FastAPI()
@app.post("/testing_alias")
async def check_alias(id_test: Annotated[int, Form(alias="otherId")]):
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:Annotated[int, Form()]):
async def check_without_alias(id_test:int = Form()):
return {"id_test":id_test}

Loading…
Cancel
Save