Skip to content

FastAPI 0.119.0 breaks direct responseΒ #856

@vincentsarago

Description

@vincentsarago

def add_direct_response(app: FastAPI) -> None:
"""
Setup FastAPI application's endpoints to return Response Object directly, avoiding
Pydantic validation and FastAPI (slow) serialization.
ref: https://gist.github.com/Zaczero/00f3a2679ebc0a25eb938ed82bc63553
"""
def wrap_endpoint(endpoint: Callable, cls: Type[Response]):
@functools.wraps(endpoint)
async def wrapper(*args, **kwargs):
content = await endpoint(*args, **kwargs)
return content if isinstance(content, Response) else cls(content)
return wrapper
for route in app.routes:
if not isinstance(route, APIRoute):
continue
response_class = route.response_class
if isinstance(response_class, DefaultPlaceholder):
response_class = response_class.value
if issubclass(response_class, Response):
route.endpoint = wrap_endpoint(route.endpoint, response_class)
route.dependant = get_dependant(path=route.path_format, call=route.endpoint)
route.app = request_response(route.get_route_handler())

fastapi/fastapi#14168

cc @Zaczero @jonhealy1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions