From fc82b2334bd5bce50835ec02ad4cf565bc9a8bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9stor=20P=C3=A9rez?= <25409753+prryplatypus@users.noreply.github.com> Date: Mon, 10 Jul 2023 10:57:27 +0200 Subject: [PATCH] Fix JSONResponse default content type (#2738) Fix JSONResponse default content type (#2737) --- sanic/app.py | 2 -- sanic/constants.py | 2 -- sanic/http/tls/creators.py | 1 - sanic/mixins/startup.py | 1 - sanic/request.py | 1 - sanic/response/types.py | 2 +- sanic/server/legacy.py | 1 - sanic/server/websockets/frame.py | 1 - sanic/server/websockets/impl.py | 1 - sanic/touchup/meta.py | 1 - sanic/utils.py | 1 - tests/test_response_json.py | 9 +++++++++ 12 files changed, 10 insertions(+), 13 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 0d232d6e..f7e0ff7e 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -894,7 +894,6 @@ class Sanic(BaseSanic, StartupMixin, metaclass=TouchUpMeta): ] = None run_middleware = True try: - await self.dispatch( "http.routing.before", inline=True, @@ -926,7 +925,6 @@ class Sanic(BaseSanic, StartupMixin, metaclass=TouchUpMeta): and request.stream.request_body and not route.extra.ignore_body ): - if hasattr(handler, "is_stream"): # Streaming handler: lift the size limit request.stream.request_max_size = float("inf") diff --git a/sanic/constants.py b/sanic/constants.py index ab23c86d..97b41cd6 100644 --- a/sanic/constants.py +++ b/sanic/constants.py @@ -4,7 +4,6 @@ from sanic.compat import UpperStrEnum class HTTPMethod(UpperStrEnum): - GET = auto() POST = auto() PUT = auto() @@ -15,7 +14,6 @@ class HTTPMethod(UpperStrEnum): class LocalCertCreator(UpperStrEnum): - AUTO = auto() TRUSTME = auto() MKCERT = auto() diff --git a/sanic/http/tls/creators.py b/sanic/http/tls/creators.py index 8ffc8650..ad3b1b4f 100644 --- a/sanic/http/tls/creators.py +++ b/sanic/http/tls/creators.py @@ -126,7 +126,6 @@ class CertCreator(ABC): local_tls_key, local_tls_cert, ) -> CertCreator: - creator: Optional[CertCreator] = None cert_creator_options: Tuple[ diff --git a/sanic/mixins/startup.py b/sanic/mixins/startup.py index a6126433..1e463bd7 100644 --- a/sanic/mixins/startup.py +++ b/sanic/mixins/startup.py @@ -1109,7 +1109,6 @@ class StartupMixin(metaclass=SanicMeta): app: StartupMixin, server_info: ApplicationServerInfo, ) -> None: # no cov - try: # We should never get to this point without a server # This is primarily to keep mypy happy diff --git a/sanic/request.py b/sanic/request.py index 592869e5..bd7b5663 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -146,7 +146,6 @@ class Request: head: bytes = b"", stream_id: int = 0, ): - self.raw_url = url_bytes try: self._parsed_url = parse_url(url_bytes) diff --git a/sanic/response/types.py b/sanic/response/types.py index fe3c941e..9ebdb0d6 100644 --- a/sanic/response/types.py +++ b/sanic/response/types.py @@ -232,7 +232,7 @@ class JSONResponse(HTTPResponse): body: Optional[Any] = None, status: int = 200, headers: Optional[Union[Header, Dict[str, str]]] = None, - content_type: Optional[str] = None, + content_type: str = "application/json", dumps: Optional[Callable[..., str]] = None, **kwargs: Any, ): diff --git a/sanic/server/legacy.py b/sanic/server/legacy.py index 824287e5..a8018ce2 100644 --- a/sanic/server/legacy.py +++ b/sanic/server/legacy.py @@ -94,7 +94,6 @@ def watchdog(sleep_interval, reload_dirs): try: while True: - changed = set() for filename in itertools.chain( _iter_module_files(), diff --git a/sanic/server/websockets/frame.py b/sanic/server/websockets/frame.py index b31e93c1..130dc5fa 100644 --- a/sanic/server/websockets/frame.py +++ b/sanic/server/websockets/frame.py @@ -52,7 +52,6 @@ class WebsocketFrameAssembler: paused: bool def __init__(self, protocol) -> None: - self.protocol = protocol self.read_mutex = asyncio.Lock() diff --git a/sanic/server/websockets/impl.py b/sanic/server/websockets/impl.py index 5e258e97..2125faa7 100644 --- a/sanic/server/websockets/impl.py +++ b/sanic/server/websockets/impl.py @@ -686,7 +686,6 @@ class WebsocketImplProtocol: :raises TypeError: for unsupported inputs """ async with self.conn_mutex: - if self.ws_proto.state in (CLOSED, CLOSING): raise WebsocketClosed( "Cannot write to websocket interface after it is closed." diff --git a/sanic/touchup/meta.py b/sanic/touchup/meta.py index 682600d6..011570da 100644 --- a/sanic/touchup/meta.py +++ b/sanic/touchup/meta.py @@ -11,7 +11,6 @@ class TouchUpMeta(SanicMeta): methods = attrs.get("__touchup__") attrs["__touched__"] = False if methods: - for method in methods: if method not in attrs: raise SanicException( diff --git a/sanic/utils.py b/sanic/utils.py index 51d94d08..76bf003c 100644 --- a/sanic/utils.py +++ b/sanic/utils.py @@ -75,7 +75,6 @@ def load_module_from_file_location( location = location.decode(encoding) if isinstance(location, Path) or "/" in location or "$" in location: - if not isinstance(location, Path): # A) Check if location contains any environment variables # in format ${some_env_var}. diff --git a/tests/test_response_json.py b/tests/test_response_json.py index c89dba42..aa1c61dd 100644 --- a/tests/test_response_json.py +++ b/tests/test_response_json.py @@ -213,3 +213,12 @@ def test_pop_list(json_app: Sanic): _, resp = json_app.test_client.get("/json-pop") assert resp.body == json_dumps(["b"]).encode() + + +def test_json_response_class_sets_proper_content_type(json_app: Sanic): + @json_app.get("/json-class") + async def handler(request: Request): + return JSONResponse(JSON_BODY) + + _, resp = json_app.test_client.get("/json-class") + assert resp.headers["content-type"] == "application/json"