Files
ServerKit/backend/app/api/_request.py
T
Juan Denis 55d8fb1e44 Read route bodies through json_object()
328 more routes used a JSON body as a dict straight from
request.get_json(), so a body of null, [] or a bare string crashed them
into a 500. Bare reads become json_object(); the
'get_json(silent=True) or {}' reads of action routes become
json_object(optional=True), which still treats a missing body as {}.
Routes that take arrays or pass the body through untouched are left
alone. The full backend suite shows no new failures.

Also carries three files' bare section citations (apps, backups,
notifications) and the extended citation guard, which needed them.
2026-10-10 20:53:43 -04:00

23 lines
873 B
Python

"""Request-body parsing for routes."""
from flask import request
from app.exceptions import ValidationError
def json_object(optional=False):
"""The request body as a dict, or a 400.
``request.get_json()`` hands back whatever JSON was sent, so a body of
``null``, ``[]`` or a bare string reached ``data.get(...)`` and crashed
the route into a 500. Flask already rejects a missing or non-JSON body
(415/400); with ``optional=True`` that case is an empty dict instead,
for action endpoints whose body is all optional settings.
"""
data = request.get_json(silent=True) if optional else request.get_json()
if data is None and optional:
return {}
if not isinstance(data, dict):
raise ValidationError('Request body must be a JSON object',
code='validation.body_not_object')
return data