Skip to content

3.15 backward compatibility issue with 3.14 - rest_framework.filters.SearchFilter.get_search_terms returns str instead of list #9308

@tomchristie

Description

@tomchristie

Discussed in #9307

Originally posted by freenoth March 19, 2024
in 3.14 the get_search_terms method of rest_framework.filters.SearchFilter class was returning a list of terms
image

so we were able to iterate the search terms directly
image

in 3.15 the same method get_search_terms is returning a string representation
image

and to convert the str to a list an additional function was introduced rest_framework.filters.search_smart_split:
image

and now we have to use this function to iterate the terms explicitly
image

So the changing of return value type (and the result itself, actually) can break some custom classes, that are using the basic functionality. Moreover it's hard to figure out what is wrong, because both list and str are iterable, so the implementation works, but with the wrong results.

Example:

I had a custom Search filter class based on rest_framework.filters.SearchFilter with overrided filter_queryset method, so I used the basic method to get the list of search terms

        search_terms = self.get_search_terms(request)

and a simple iteration by search terms works well

        for search_term in search_terms:

but after upgrading to 3.15, my iteration started working differently, because now get_search_terms is returning a string -> so it works as iteration by string, it's adding every symbol of this string as independent query, instead of one query

for input ?search=asd it builds a query like that

WHERE ((UPPER("resource"."id"::text) LIKE UPPER(%a%) OR UPPER("resource"."name"::text) LIKE UPPER(%a%))
	AND (UPPER("resource"."id"::text) LIKE UPPER(%s%) OR UPPER("resource"."name"::text) LIKE UPPER(%s%)) 
	AND (UPPER("resource"."id"::text) LIKE UPPER(%d%) OR UPPER("resource"."name"::text) LIKE UPPER(%d%)))

instead of a single search query

WHERE (UPPER("resource"."id"::text) LIKE UPPER(%asd%) OR UPPER("resource"."name"::text) LIKE UPPER(%asd%))

so we have to add using a rest_framework.filters.search_smart_split function explicitly everywhere to iterate search terms in an old way

        for search_term in search_smart_split(search_terms):

is there a chance to use search_smart_split inside the rest_framework.filters.SearchFilter.get_search_terms to not to break backward compatibility and method behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions