Skip to content

Commit

Permalink
Handle parentheses search queries (#1828)
Browse files Browse the repository at this point in the history
* escape parantheses in query

* reformat code

* add changelog

* Update news/1828.bugfix

Co-authored-by: Steve Piercy <[email protected]>

* Update src/plone/restapi/search/handler.py

Co-authored-by: Steve Piercy <[email protected]>

* add tests for searching with parantheses

* format code

* run black to format code

* Update news/1828.bugfix

---------

Co-authored-by: Steve Piercy <[email protected]>
Co-authored-by: David Glick <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent dcdb41b commit b9ca1f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/1828.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`@search` service: Remove parentheses from search query. @tedw87
11 changes: 10 additions & 1 deletion src/plone/restapi/search/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def _constrain_query_by_path(self, query):
path = "/".join(self.context.getPhysicalPath())
query["path"]["query"] = path

def quote_chars(self, query):
# Remove parentheses from the query
return query.replace("(", " ").replace(")", " ").strip()

def search(self, query=None):
if query is None:
query = {}
Expand All @@ -93,14 +97,19 @@ def search(self, query=None):
if use_site_search_settings:
query = self.filter_query(query)

if "SearchableText" in query:
# Sanitize SearchableText by removing parentheses
query["SearchableText"] = self.quote_chars(query["SearchableText"])
if not query["SearchableText"] or query["SearchableText"] == "*":
return []

self._constrain_query_by_path(query)
query = self._parse_query(query)

lazy_resultset = self.catalog.searchResults(**query)
results = getMultiAdapter((lazy_resultset, self.request), ISerializeToJson)(
fullobjects=fullobjects
)

return results

def filter_types(self, types):
Expand Down
23 changes: 23 additions & 0 deletions src/plone/restapi/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ def test_search_on_context_constrains_query_by_path(self):
set(result_paths(response.json())),
)

def test_search_with_parentheses(self):
query = {"SearchableText": "("}
response = self.api_session.get("/@search", params=query)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json(), [], "Expected no items for query with only parentheses"
)

query = {"SearchableText": ")"}
response = self.api_session.get("/@search", params=query)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json(), [], "Expected no items for query with only parentheses"
)

query = {"SearchableText": "lorem(ipsum)"}
response = self.api_session.get("/@search", params=query)
self.assertEqual(response.status_code, 200)
items = [item["title"] for item in response.json().get("items", [])]
self.assertIn(
"Lorem Ipsum", items, "Expected 'Lorem Ipsum' to be found in search results"
)

def test_search_in_vhm(self):
# Install a Virtual Host Monster
if "virtual_hosting" not in self.app.objectIds():
Expand Down

0 comments on commit b9ca1f9

Please sign in to comment.