Browse Source

update body type to allow bytes and cast appropriately

pull/15091/head
svlandeg 4 months ago
parent
commit
e6378e844e
  1. 6
      fastapi/dependencies/utils.py
  2. 4
      fastapi/routing.py

6
fastapi/dependencies/utils.py

@ -598,7 +598,7 @@ async def solve_dependencies(
*,
request: Request | WebSocket,
dependant: Dependant,
body: dict[str, Any] | FormData | None = None,
body: dict[str, Any] | FormData | bytes | None = None,
background_tasks: StarletteBackgroundTasks | None = None,
response: Response | None = None,
dependency_overrides_provider: Any | None = None,
@ -950,7 +950,7 @@ async def _extract_form_body(
async def request_body_to_args(
body_fields: list[ModelField],
received_body: dict[str, Any] | FormData | None,
received_body: dict[str, Any] | FormData | bytes | None,
embed_body_fields: bool,
) -> tuple[dict[str, Any], list[dict[str, Any]]]:
values: dict[str, Any] = {}
@ -981,7 +981,7 @@ async def request_body_to_args(
for field in body_fields:
loc = ("body", get_validation_alias(field))
value: Any | None = None
if body_to_process is not None:
if body_to_process is not None and not isinstance(body_to_process, bytes):
try:
value = body_to_process.get(get_validation_alias(field))
# If the received body is a list, not a dict

4
fastapi/routing.py

@ -26,6 +26,7 @@ from typing import (
Annotated,
Any,
TypeVar,
cast,
)
import anyio
@ -75,6 +76,7 @@ from starlette import routing
from starlette._exception_handler import wrap_app_handling_exceptions
from starlette._utils import is_async_callable
from starlette.concurrency import iterate_in_threadpool, run_in_threadpool
from starlette.datastructures import FormData
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import JSONResponse, Response, StreamingResponse
@ -455,7 +457,7 @@ def get_request_handler(
solved_result = await solve_dependencies(
request=request,
dependant=dependant,
body=body, # ty: ignore[invalid-argument-type]
body=cast(dict[str, Any] | FormData | bytes | None, body),
dependency_overrides_provider=dependency_overrides_provider,
async_exit_stack=async_exit_stack,
embed_body_fields=embed_body_fields,

Loading…
Cancel
Save