From 234daf0446c07780b476e8143734a0ffa383dd24 Mon Sep 17 00:00:00 2001 From: Anna Shamray Date: Mon, 9 Dec 2024 17:15:07 +0100 Subject: [PATCH] :sparkles: [#472] support commas in values for 'data_attrs' query param --- src/objects/api/v2/filters.py | 5 +++++ src/objects/api/validators.py | 7 +++++++ src/objects/tests/v2/test_filters.py | 14 ++++++-------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/objects/api/v2/filters.py b/src/objects/api/v2/filters.py index 442dfa18..ad8c5c03 100644 --- a/src/objects/api/v2/filters.py +++ b/src/objects/api/v2/filters.py @@ -92,9 +92,14 @@ class Meta: form = ObjectRecordFilterForm def filter_data_attrs(self, queryset, name, value: str): + # temporarily replace escaped comma + comma_replace = "*COMMA*" + value = value.replace("\\,", comma_replace) parts = value.split(",") for value_part in parts: + # replace to comma back after splitting + value_part = value_part.replace(comma_replace, ",") variable, operator, str_value = value_part.rsplit("__", 2) real_value = string_to_value(str_value) diff --git a/src/objects/api/validators.py b/src/objects/api/validators.py index 11b86567..2518e0ce 100644 --- a/src/objects/api/validators.py +++ b/src/objects/api/validators.py @@ -70,9 +70,16 @@ def __call__(self, new_value, serializer_field): def validate_data_attrs(value: str): code = "invalid-data-attrs-query" + + # temporarily replace escaped comma + comma_replace = "*COMMA*" + value = value.replace("\\,", comma_replace) + parts = value.split(",") for value_part in parts: + # replace to comma back after splitting + value_part = value_part.replace(comma_replace, ",") try: variable, operator, val = value_part.rsplit("__", 2) except ValueError as exc: diff --git a/src/objects/tests/v2/test_filters.py b/src/objects/tests/v2/test_filters.py index 339adbae..9c4e8474 100644 --- a/src/objects/tests/v2/test_filters.py +++ b/src/objects/tests/v2/test_filters.py @@ -363,17 +363,16 @@ def test_filter_icontains_string_with_comma(self): """ regression test for https://github.com/maykinmedia/objects-api/issues/472 """ - record = ObjectRecordFactory.create( + ObjectRecordFactory.create( data={"name": "Something important"}, object__object_type=self.object_type ) - ObjectRecordFactory.create( + record = ObjectRecordFactory.create( data={"name": "Advies, support en kennis om te weten"}, object__object_type=self.object_type, ) - ObjectRecordFactory.create(data={}, object__object_type=self.object_type) response = self.client.get( - self.url, {"data_attrs": "name__icontains__Advies, support en kennis"} + self.url, {"data_attrs": "name__icontains__Advies\\, support en kennis"} ) self.assertEqual(response.status_code, status.HTTP_200_OK) @@ -390,19 +389,18 @@ def test_filter_two_icontains_with_comma(self): """ regression test for https://github.com/maykinmedia/objects-api/issues/472 """ - record = ObjectRecordFactory.create( + ObjectRecordFactory.create( data={"name": "Something important"}, object__object_type=self.object_type ) - ObjectRecordFactory.create( + record = ObjectRecordFactory.create( data={"name": "Advies, support en kennis om te weten"}, object__object_type=self.object_type, ) - ObjectRecordFactory.create(data={}, object__object_type=self.object_type) response = self.client.get( self.url, { - "data_attrs": "name__icontains__Advies, support en kennis,name__icontains__om" + "data_attrs": "name__icontains__Advies\\, support en kennis,name__icontains__om" }, )