Source code for smarter.lib.django.middleware.json

"""
Middleware that guarantees JSON-formatted error responses for clients requesting JSON content.

This middleware intercepts non-JSON error responses and converts them
into standardized ``JsonResponse`` objects when the client explicitly
indicates JSON support through HTTP content negotiation headers.

Key Features
============

- Converts non-JSON error responses into JSON
- Preserves original HTTP status codes
- Supports sync and async Django execution
- Performs HTTP Accept-header content negotiation
- Leaves successful responses untouched
- Preserves existing JSON responses
- Feature-flag enablement via Django Waffle

Behavior
========

For each response, the middleware:

#. Determines whether the client accepts JSON responses
#. Detects whether the response represents an error condition
#. Detects whether the response is already JSON
#. Converts eligible error responses into ``JsonResponse`` objects
#. Preserves the original HTTP status code

Only error responses are normalized.

Responses with status codes below ``400 Bad Request`` are returned
unchanged.

Content Negotiation
===================

The middleware performs lightweight content negotiation using the
request ``Accept`` header.

JSON normalization is enabled only when the client advertises support
for one of the configured JSON content types.

Supported JSON content types include:

- ``application/json``
- ``application/problem+json``

Error Payload Format
====================

Normalized JSON responses use the following structure:

.. code-block:: json

   {
     "error": {
       "status_code": 404,
       "message": "Not Found"
     }
   }

The payload preserves:

- the original HTTP status code
- the original HTTP reason phrase

Async Compatibility
===================

The middleware supports both synchronous and asynchronous Django
execution models.

Coroutine-based request handlers are detected automatically during
middleware initialization.

Async request execution delegates synchronous middleware processing
through ``sync_to_async()`` to preserve compatibility with Django's
middleware lifecycle.

Response Handling Rules
=======================

The middleware intentionally avoids modifying:

- successful responses
- existing JSON responses
- clients not requesting JSON content

This ensures compatibility with:

- HTML browser workflows
- Django admin
- traditional server-rendered views
- API clients expecting structured JSON errors

Feature Flags
=============

Middleware activation is controlled using Django Waffle:

- ``ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR``

When disabled, the middleware behaves as a transparent pass-through.

Logging
=======

The middleware emits structured logs for:

- middleware initialization
- request processing
- JSON error normalization events

Classes
=======

.. autosummary::
   :toctree: generated/

   SmarterJsonErrorMiddleware

Dependencies
============

- Django
- asgiref
- Django Waffle

Warnings
========

This middleware performs lightweight content negotiation based solely
on ``Accept`` headers and does not implement full RFC-compliant
negotiation semantics.

Clients advertising wildcard media types without explicit JSON support
may not receive normalized JSON errors.

Notes
=====

This middleware depends on helper functionality provided by
:class:`smarter.common.mixins.SmarterMiddlewareMixin`.

The middleware preserves Django response semantics by avoiding
modification of successful responses and preserving original HTTP
status codes during normalization.
"""

from __future__ import annotations

from collections.abc import Awaitable
from functools import cached_property
from http import HTTPStatus

from django.http import JsonResponse
from django.http.request import HttpRequest
from django.http.response import HttpResponseBase

from smarter.common.mixins import SmarterHelperMixin, SmarterMiddlewareMixin
from smarter.lib import logging
from smarter.lib.django import waffle
from smarter.lib.django.waffle import SmarterWaffleSwitches
from smarter.lib.typing import SmarterDictType

logger = logging.getSmarterLogger(__name__, any_switches=[SmarterWaffleSwitches.MIDDLEWARE_LOGGING])

if waffle.switch_is_active(SmarterWaffleSwitches.ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR):
    logger.debug(
        "is %s",
        SmarterHelperMixin().formatted_state_ready,
    )
else:
    logger.debug(
        "is %s. Enable with Django waffle in the admin console.",
        SmarterHelperMixin().formatted_state_not_ready,
    )


[docs] class SmarterJsonErrorMiddleware(SmarterMiddlewareMixin): """Middleware that converts non-JSON error responses into JSON responses for clients requesting JSON content.""" JSON_CONTENT_TYPES = ( "application/json", "application/problem+json", ) def __call__(self, request: HttpRequest) -> HttpResponseBase | Awaitable[HttpResponseBase]: """ Convert a sync error response to JSON, or hand the request to :meth:`__acall__` in async mode. An amnesty path, or a request while ``ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR`` is off, gets the next handler's response unchanged. :param request: The incoming request. :returns: The response, or a :class:`~django.http.JsonResponse` with the same status code (a coroutine in async mode). """ if self.async_mode: return self.__acall__(request) if self.deserves_amnesty(request.path): return super().__call__(request) if not waffle.switch_is_active(SmarterWaffleSwitches.ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR): return super().__call__(request) response = super().__call__(request) # the switch was checked above, so skip process_response(), which would check it again. return self.normalize_json_error_response(request=request, response=response) # type: ignore async def __acall__(self, request: HttpRequest) -> HttpResponseBase: """ Convert an async error response to JSON, unless ``ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR`` is off. :param request: The incoming request. :returns: The response, or a :class:`~django.http.JsonResponse` with the same status code. """ if not await waffle.async_switch_is_active(SmarterWaffleSwitches.ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR): return await super().__acall__(request) response = await super().__acall__(request) # the switch was checked above, so skip async_process_response(), which would check it again. return self.normalize_json_error_response(request=request, response=response)
[docs] @cached_property def formatted_class_name(self) -> str: """Return the formatted class name for logging. It names the instance, not a request. """ class_name = f"{__name__}.{self.__class__.__name__}[{id(self)}]" return self.formatted_text(class_name)
[docs] def process_response(self, request: HttpRequest, response: HttpResponseBase) -> HttpResponseBase: """ Convert an error response to JSON for clients that accept JSON. It does nothing unless the ``ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR`` waffle switch is active. :param request: The request. :param response: The response from the view. :returns: The response, or a :class:`~django.http.JsonResponse` with the same status code. """ if not waffle.switch_is_active(SmarterWaffleSwitches.ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR): return response return self.normalize_json_error_response(request=request, response=response)
[docs] async def async_process_response(self, request: HttpRequest, response: HttpResponseBase) -> HttpResponseBase: """ Async version of :meth:`process_response`. :param request: The request. :param response: The response from the view. :returns: The response, or a :class:`~django.http.JsonResponse` with the same status code. """ if not await waffle.async_switch_is_active(SmarterWaffleSwitches.ENABLE_MIDDLEWARE_SMARTER_JSON_ERROR): return response # normalize_json_error_response() only reads headers and builds a dict: no I/O, so it # doesn't need a worker thread. return self.normalize_json_error_response(request=request, response=response)
[docs] def normalize_json_error_response(self, request: HttpRequest, response: HttpResponseBase) -> HttpResponseBase: """ Convert non-JSON error responses into JsonResponse objects. Only responses with a status of 400 or more, to clients that accept JSON, are converted. :param request: The request. :param response: The response from the view. :returns: The response, or a :class:`~django.http.JsonResponse` with the same status code. """ # the status code is the cheapest check, and most responses aren't errors. if response.status_code < HTTPStatus.BAD_REQUEST: return response if not self.client_accepts_json(request): return response if self.response_is_json(response): return response payload = self.build_error_payload(response) return JsonResponse(payload, status=response.status_code)
[docs] def client_accepts_json(self, request: HttpRequest) -> bool: """ Return whether the client accepts JSON responses. :param request: The request. :returns: True if the ``Accept`` header names one of :attr:`JSON_CONTENT_TYPES`. """ accept_header = request.headers.get("Accept", "").lower() if not accept_header: return False return any(content_type in accept_header for content_type in self.JSON_CONTENT_TYPES)
[docs] @staticmethod def response_is_json(response: HttpResponseBase) -> bool: """ Return whether the response is already JSON. :param response: The response from the view. :returns: True if its ``Content-Type`` contains ``json``. """ content_type = response.get( "Content-Type", "", ).lower() return "json" in content_type
[docs] @staticmethod def build_error_payload(response: HttpResponseBase) -> SmarterDictType: """ Build the JSON error payload that replaces an error response. :param response: The error response. :returns: ``{"error": {"status_code": ..., "message": ...}}``, with the response's status code and reason phrase. """ payload: SmarterDictType = { "error": { "status_code": response.status_code, "message": response.reason_phrase, } } return payload