Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use return type annotation for response_model by default. #875

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions fastapi/routing.py
Expand Up @@ -171,6 +171,12 @@ async def app(websocket: WebSocket) -> None:
return app


def get_callable_return_type(func: Callable) -> Optional[Type[Any]]:
sig = inspect.signature(func)
has_return_annotation = sig.return_annotation is not inspect.Signature.empty
return sig.return_annotation if has_return_annotation else None


class APIWebSocketRoute(routing.WebSocketRoute):
def __init__(
self,
Expand Down Expand Up @@ -230,6 +236,9 @@ def __init__(
self.unique_id = generate_operation_id_for_path(
name=self.name, path=self.path_format, method=list(methods)[0]
)
if response_model is None:
# We try to fallback to the response annotation in case response model not defined
response_model = get_callable_response_type(endpoint)
self.response_model = response_model
if self.response_model:
assert (
Expand Down