Skip to content

Commit

Permalink
batching int valueerror to badrequests
Browse files Browse the repository at this point in the history
  • Loading branch information
djay committed Jan 14, 2025
1 parent 2ea94ce commit 4fb0e7b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/plone/restapi/batching.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from plone.batching.batch import Batch
from plone.restapi.deserializer import json_body
from plone.restapi.exceptions import DeserializationError
from urllib.parse import parse_qsl
from urllib.parse import urlencode
from zExceptions import BadRequest


DEFAULT_BATCH_SIZE = 25
Expand All @@ -11,13 +13,15 @@ class HypermediaBatch:
def __init__(self, request, results):
self.request = request

self.b_start = int(json_body(self.request).get("b_start", False)) or int(
self.request.form.get("b_start", 0)
)
self.b_size = int(json_body(self.request).get("b_size", False)) or int(
self.request.form.get("b_size", DEFAULT_BATCH_SIZE)
)

try:
self.b_start = int(json_body(self.request).get("b_start", False)) or int(
self.request.form.get("b_start", 0)
)
self.b_size = int(json_body(self.request).get("b_size", False)) or int(
self.request.form.get("b_size", DEFAULT_BATCH_SIZE)
)
except (ValueError, DeserializationError) as e:
raise BadRequest(e)
self.batch = Batch(results, self.b_size, self.b_start)

def __iter__(self):
Expand Down
4 changes: 4 additions & 0 deletions src/plone/restapi/tests/test_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def test_batching_links_omitted_if_resulset_fits_in_single_batch(self):
response = self.api_session.get("/collection?b_size=100")
self.assertNotIn("batching", list(response.json()))

def test_batching_badrequests(self):
response = self.api_session.get("/collection?b_size=php")
self.assertEqual(response.status_code, 400)
self.assertIn("invalid literal for int()", response.json()["message"])

class TestBatchingDXFolders(TestBatchingDXBase):

Expand Down

0 comments on commit 4fb0e7b

Please sign in to comment.