Browse Source

🎨 [pre-commit.ci] Auto format from pre-commit.com hooks

pull/10319/head
pre-commit-ci[bot] 1 year ago
parent
commit
6bd8a838b0
  1. 3
      fastapi/_compat.py
  2. 12
      tests/test_alias_and_validation_alias_in_form.py

3
fastapi/_compat.py

@ -274,8 +274,7 @@ if PYDANTIC_V2:
*, fields: Sequence[ModelField], model_name: str *, fields: Sequence[ModelField], model_name: str
) -> Type[BaseModel]: ) -> Type[BaseModel]:
field_params = { field_params = {
f"{f.alias}": (f.field_info.annotation, f.field_info) f"{f.alias}": (f.field_info.annotation, f.field_info) for f in fields
for f in fields
} }
BodyModel: Type[BaseModel] = create_model(model_name, **field_params) # type: ignore[call-overload] BodyModel: Type[BaseModel] = create_model(model_name, **field_params) # type: ignore[call-overload]
return BodyModel return BodyModel

12
tests/test_alias_and_validation_alias_in_form.py

@ -1,6 +1,5 @@
from starlette.testclient import TestClient
from fastapi import FastAPI, Form from fastapi import FastAPI, Form
from starlette.testclient import TestClient
app: FastAPI = FastAPI() app: FastAPI = FastAPI()
@ -9,6 +8,7 @@ app: FastAPI = FastAPI()
async def check_alias(id_test: int = Form(alias="otherId")): async def check_alias(id_test: int = Form(alias="otherId")):
return {"other_id": id_test} return {"other_id": id_test}
@app.post("/testing_validation_alias") @app.post("/testing_validation_alias")
async def check_validation_alias(id_test: int = Form(validation_alias="otherId")): async def check_validation_alias(id_test: int = Form(validation_alias="otherId")):
return {"other_id": id_test} return {"other_id": id_test}
@ -22,11 +22,13 @@ def test_get_alias():
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"other_id": 1} assert response.json() == {"other_id": 1}
def test_get_validation_alias(): def test_get_validation_alias():
response = client.post("/testing_validation_alias", data={"otherId": "1"}) response = client.post("/testing_validation_alias", data={"otherId": "1"})
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"other_id": 1} assert response.json() == {"other_id": 1}
def test_file_alias_schema(): def test_file_alias_schema():
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
@ -41,7 +43,7 @@ def test_file_alias_schema():
assert ( assert (
"otherId" "otherId"
in schema["components"]["schemas"]["Body_check_validation_alias_testing_validation_alias_post"][ in schema["components"]["schemas"][
"properties" "Body_check_validation_alias_testing_validation_alias_post"
] ]["properties"]
) )

Loading…
Cancel
Save