Browse Source

Code review fixes

pull/11306/head
Markus Sintonen 10 months ago
parent
commit
6b5fefadab
  1. 2
      fastapi/dependencies/utils.py
  2. 13
      tests/test_root_model.py

2
fastapi/dependencies/utils.py

@ -931,7 +931,7 @@ async def request_body_to_args(
if (
single_not_embedded_field
and resolves_to_basemodel(first_field.type_)
and lenient_issubclass(first_field.type_, BaseModel)
and isinstance(received_body, FormData)
):
fields_to_extract = get_cached_model_fields(first_field.type_)

13
tests/test_root_model.py

@ -280,6 +280,10 @@ def test_root_model_union(left: Any, right: Any):
def handler2(b: Annotated[Model, Body()]):
return {"b": b}
@app2.post("/body2")
def handler2(q: Model) -> Model:
return q
client2 = TestClient(app2)
for val in ["2025-01-02", 42]:
response = client2.get(f"/query?q={val}")
@ -289,6 +293,15 @@ def test_root_model_union(left: Any, right: Any):
assert response.status_code == 200, response.text
assert response.json() == {"b": val}
# By default, RootModel is regarded as a body parameter
response = (
client2.post("/body2", json=val)
if issubclass(left, RootModel) or issubclass(right, RootModel)
else client2.post(f"/body2?q={val}")
)
assert response.status_code == 200, response.text
assert response.json() == val
response = client2.get("/query?q=bad")
assert response.status_code == 422, response.text
assert {detail["msg"] for detail in response.json()["detail"]} == {

Loading…
Cancel
Save