msgpack.py: allow pydantic BaseModel in content

This commit is contained in:
Tal Leibman 2020-12-25 11:10:43 +02:00 committed by Tom Hacohen
parent c1534f6587
commit 25cb4fec0c

View File

@ -1,6 +1,7 @@
import typing as t import typing as t
import msgpack import msgpack
from fastapi.routing import APIRoute, get_request_handler from fastapi.routing import APIRoute, get_request_handler
from pydantic import BaseModel
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import Response from starlette.responses import Response
@ -19,6 +20,8 @@ class MsgpackResponse(Response):
media_type = "application/msgpack" media_type = "application/msgpack"
def render(self, content: t.Any) -> bytes: def render(self, content: t.Any) -> bytes:
if isinstance(content, BaseModel):
content = content.dict()
return msgpack.packb(content, use_bin_type=True) return msgpack.packb(content, use_bin_type=True)