API: Empty query string parameters (e.g., `?search=`, `?type=`) return no results instead of being ignored
Issue summary
When consuming the Wagtail v2 API, passing an empty string to any filter parameter (such as ?search=, ?type=, or custom field filters) returns an empty result set or causes unexpected behavior.
In RESTful API design, an empty parameter is generally expected to be treated as if the parameter was omitted entirely, which should return the default, unfiltered paginated queryset.
This causes friction for headless Wagtail implementations. When a frontend application binds UI controls (like search inputs or dropdown filters) directly to API requests or URL state, clearing the input often results in requests with empty parameters like ?search=&type=. Instead of reverting to the standard list of items, the frontend displays a "No results found" state. This forces developers to write boilerplate code to explicitly clean and strip empty parameters from their frontend state before making fetch calls.
Proposed solution
Update the core API filter logic to universally ignore empty query parameters across all filter backends (SearchFilter, FieldsFilter, ChildOfFilter, etc.).
This could be handled gracefully in the base API view or filter classes by checking if a parameter's value is an empty string (or whitespace) and ignoring it before the specific filter logic is applied.
Additional context
- Better DX: Frontend developers will no longer need to write conditional logic to sanitize query parameters.
- Consistency: This aligns Wagtail's API behavior with standard REST conventions, treating empty values identically to omitted values.
- Performance: Bypassing database lookups or search backends for empty queries avoids unnecessary processing overhead.
I am planning to work on a PR for this.
Steps to reproduce
- Start a Wagtail project with the v2 API enabled and ensure there are a few published pages available.
- Make a standard GET request to the base pages endpoint:
GET /api/v2/pages/Observe the response returns a populated list of pages. - Make the same request, but append an empty
searchparameter:GET /api/v2/pages/?search=Observe the response incorrectly returns an empty items array. - Make the same request using an empty
typeparameter (or any other field filter):GET /api/v2/pages/?type=Observe the response similarly returns an empty result set instead of ignoring the parameter.
Expected behavior: When a query parameter is present but empty, the API should ignore it and return the default, unfiltered paginated list, rather than attempting to filter by an empty string.
Technical details
- Python version: 3.14.
- Django version: 6.1.
- Wagtail version: 7.4.2.
Working on this
If you would like to contribute to this issue, follow these steps:
- Confirm that the issue is reproducible, either on a fresh Wagtail project or the bakerydemo.
- Once confirmed, view our contributing guidelines, add a comment to the issue once you're ready to start.
Source: wagtail/wagtail