Skip to content

Commit

Permalink
Fix param handling in querystringsearch GET (#1622)
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Apr 17, 2023
1 parent a6934da commit 53be761
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions news/1621.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bugs in handling parameters when the `@querystringsearch` endpoint is called with the GET method. @davisagli
15 changes: 8 additions & 7 deletions src/plone/restapi/services/querystringsearch/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from urllib import parse
from zope.component import getMultiAdapter

import json


zcatalog_version = get_distribution("Products.ZCatalog").version
if parse_version(zcatalog_version) >= parse_version("5.1"):
Expand All @@ -24,7 +22,8 @@ def __init__(self, context, request):
self.context = context
self.request = request

def __call__(self, data):
def __call__(self):
data = json_body(self.request)
query = data.get("query", None)
b_start = int(data.get("b_start", 0))
b_size = int(data.get("b_size", 25))
Expand Down Expand Up @@ -73,14 +72,16 @@ class QuerystringSearchPost(Service):

def reply(self):
querystring_search = QuerystringSearch(self.context, self.request)
return querystring_search(data=json_body(self.request))
return querystring_search()


class QuerystringSearchGet(Service):
"""Returns the querystring search results given a p.a.querystring data."""

def reply(self):
# We need to copy the JSON query parameters from the querystring
# into the request body, because that's where other code expects to find them
self.request["BODY"] = parse.unquote(self.request.form.get("query", "{}"))

querystring_search = QuerystringSearch(self.context, self.request)
return querystring_search(
data=json.loads(parse.unquote(self.request.form.get("query", "{}")))
)
return querystring_search()
7 changes: 5 additions & 2 deletions src/plone/restapi/tests/test_services_querystringsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ def test_querystringsearch_basic(self):
self.assertNotIn("effective", response.json()["items"][0])

def test_querystringsearch_basic_get(self):
self.portal.invokeFactory("Document", "doc2", title="Test Document 2")
transaction.commit()

response = self.api_session.get(
"/@querystring-search?query=%7B%22query%22%3A%5B%7B%22i%22%3A%22portal_type%22%2C%22o%22%3A%20%22plone.app.querystring.operation.selection.any%22%2C%22v%22%3A%5B%22Document%22%5D%7D%5D%7D"
"/@querystring-search?query=%7B%22query%22%3A%20%5B%7B%22i%22%3A%20%22portal_type%22%2C%20%22o%22%3A%20%22plone.app.querystring.operation.selection.any%22%2C%20%22v%22%3A%20%5B%22Document%22%5D%7D%5D%2C%20%22b_size%22%3A%201%7D"
)

self.assertEqual(response.status_code, 200)
self.assertIn("items", response.json())
self.assertIn("items_total", response.json())
self.assertEqual(response.json()["items_total"], 1)
self.assertEqual(response.json()["items_total"], 2)
self.assertEqual(len(response.json()["items"]), 1)
self.assertNotIn("effective", response.json()["items"][0])

Expand Down

0 comments on commit 53be761

Please sign in to comment.