Return semantic API responses for malformed API requests
Overview
Kolibri's API endpoints return a 500 for many malformed requests, such as a non-UUID id, a non-integer query parameter, or a request body that isn't a JSON object. Recent issues reported these one endpoint at a time (#15728, #15790, #15797, #15806, #15826, #15829, #15835), and each fix used a different validation approach. This tracking issue sets one approach for validating API request input, so endpoints return a 400 for invalid input and a 404 for an object that doesn't exist.
Scope
- Included: DRF endpoints and API-like Django views (
file_checksums, the facility CSV export views,set_language) that return a 500 for malformed request input. - Included: fixes in the shared API code (the DRF exception handler,
KolibriAuthPermissions, pagination, morango'sUUIDField), the per-endpoint fixes still needed after them, a regression test across every API route, and the validation standard indocs/backend_architecture/api_patterns.rst. - Excluded: zip content serving, management commands, and API bugs not caused by unvalidated input, such as #15797.
Guidance
Every fix under this tracker follows this standard:
- Query parameters are declared on a FilterSet with typed filters, as in
FacilityUserFilter. Query parameters that don't filter, such as a result count, are declared on a Serializer that validatesrequest.query_params. - Request bodies are validated by a Serializer with
is_valid(raise_exception=True), as inStartSessionSerializer. Views don't readrequest.datadirectly. - Ids from the URL are looked up with
get_object()orserialize_object(), never.get(pk=...)on the raw value. - UUIDs in request input use
HexOnlyUUIDField. - A malformed, out-of-range or unknown id in the URL path returns 404. Malformed or out-of-range query parameters and body fields return 400 naming the field.
- Every fix adds a test that sends the malformed input and asserts the 400 or 404.
Strategy
- The shared API code fixes land first. They turn many 500s into 400s or 404s without changes to individual endpoints.
- Each per-endpoint sub-issue lists only the endpoints that still return a 500 with the shared fixes in place, and is blocked by the shared fixes it depends on.
- The morango
UUIDFieldfix ships in a morango release, then Kolibri upgrades morango. - The route-wide regression test lands early with an ignore list of the routes that still fail, and each per-endpoint fix removes its routes from that list.
AI usage
I had Claude Code sweep Kolibri's API code for unvalidated request input and reproduce each 500 with a test. I chose the validation standard, the scope and the split into sub-issues. Claude Code drafted the sections with me.
/assign
Hi! 👋
Thanks so much for your interest! This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues. If there are no unassigned 'help wanted' issues available, please wait until new ones are added.
We really appreciate your willingness to help. 😊
Are you preparing for Google Summer of Code? See our GSoC guidelines.