Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Tracked by #9331
tomchristie opened this issue Mar 19, 2024 Discussed in #9307 · 3 comments · Fixed by #9338
Labels

Comments

@tomchristie
Copy link
Member

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?

@tomchristie
Copy link
Member Author

Suggest reverting #9017.

@tomchristie tomchristie mentioned this issue Mar 21, 2024
9 tasks
@tomchristie
Copy link
Member Author

Okay, I can't automatically revert #9017 through the GH interface.
If anyone's up for creating the revert PR for that, would be appreciated.

@auvipy
Copy link
Member

auvipy commented Mar 22, 2024

I have to look into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants