Skip to content

Commit

Permalink
chore: support JSONRepsonse dumps callable return type bytes
Browse files Browse the repository at this point in the history
the `dumps` parameter is used to serialize the `JSONResponse` to text.  this is often done with python's built in `json.dumps` but other times via `orjson.dumps`

however, to use `orjson.dumps`, you have to ignore the suggested type (`str`) because `orjson.dumps` returns a `bytes`

to correct this, the typing for the dumps callable has been switched to be an `AnyStr` so either `bytes` or `str` returning callable can be passed
  • Loading branch information
imnotjames committed Sep 27, 2024
1 parent da1c646 commit 6ba5dc7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions sanic/response/convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def json(
status: int = 200,
headers: Optional[Dict[str, str]] = None,
content_type: str = "application/json",
dumps: Optional[Callable[..., str]] = None,
dumps: Optional[Callable[..., AnyStr]] = None,
**kwargs: Any,
) -> JSONResponse:
"""Returns response object with body in json format.
Expand All @@ -48,7 +48,7 @@ def json(
status (int, optional): HTTP response code. Defaults to `200`.
headers (Dict[str, str], optional): Custom HTTP headers. Defaults to `None`.
content_type (str, optional): The content type (string) of the response. Defaults to `"application/json"`.
dumps (Callable[..., str], optional): A custom json dumps function. Defaults to `None`.
dumps (Callable[..., AnyStr], optional): A custom json dumps function. Defaults to `None`.
**kwargs (Any): Remaining arguments that are passed to the json encoder.
Returns:
Expand Down
8 changes: 5 additions & 3 deletions sanic/response/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class JSONResponse(HTTPResponse):
status (int, optional): HTTP response number. Defaults to `200`.
headers (Optional[Union[Header, Dict[str, str]]], optional): Headers to be returned. Defaults to `None`.
content_type (str, optional): Content type to be returned (as a header). Defaults to `"application/json"`.
dumps (Optional[Callable[..., str]], optional): The function to use for json encoding. Defaults to `None`.
dumps (Optional[Callable[..., AnyStr]], optional): The function to use for json encoding. Defaults to `None`.
**kwargs (Any, optional): The kwargs to pass to the json encoding function. Defaults to `{}`.
""" # noqa: E501

Expand All @@ -283,13 +283,15 @@ def __init__(
status: int = 200,
headers: Optional[Union[Header, Dict[str, str]]] = None,
content_type: str = "application/json",
dumps: Optional[Callable[..., str]] = None,
dumps: Optional[Callable[..., AnyStr]] = None,
**kwargs: Any,
):
self._initialized = False
self._body_manually_set = False

self._use_dumps = dumps or BaseHTTPResponse._dumps
self._use_dumps: Callable[..., str | bytes] = (
dumps or BaseHTTPResponse._dumps
)
self._use_dumps_kwargs = kwargs

self._raw_body = body
Expand Down

0 comments on commit 6ba5dc7

Please sign in to comment.