Skip to content

Commit

Permalink
Merge pull request #26 from cloudblue/bugfix/LITE-20832
Browse files Browse the repository at this point in the history
LITE-20832 Malformed RQL select query is correctly handled
  • Loading branch information
maxipavlovic authored Nov 4, 2021
2 parents 0852140 + cddf474 commit 8f2210d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
6 changes: 2 additions & 4 deletions dj_rql/drf/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def get_query(cls, filter_instance, request, view):
else:
return cls.get_rql_query(filter_instance, request, query_string)
except Exception:
raise RQLFilterParsingError(details={
'error': 'Bad filter query.',
})
raise RQLFilterParsingError()

@classmethod
def modify_initial_query(cls, filter_instance, request, query_string):
Expand Down Expand Up @@ -251,7 +249,7 @@ def _add_quotes_to_value(cls, value):

@staticmethod
def _conversion_error():
raise RQLFilterParsingError(details={'error': 'Bad filter query.'})
raise RQLFilterParsingError()

@classmethod
def _get_filters_similar_to_old_syntax(cls, filter_instance):
Expand Down
4 changes: 4 additions & 0 deletions dj_rql/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class RQLFilterParsingError(RQLFilterError):
""" Parsing errors are raised only at query parsing time. """
MESSAGE = 'RQL Parsing error.'

def __init__(self, **kwargs):
kwargs.setdefault('details', {'error': 'Bad filter query.'})
super().__init__(**kwargs)


class RQLFilterLookupError(RQLFilterError):
""" Lookup error is raised when provided lookup is not supported by the associated filter. """
Expand Down
6 changes: 5 additions & 1 deletion dj_rql/filter_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ def apply_filters(self, query, request=None, view=None):
qs = rql_transformer.transform(rql_ast)
except LarkError as e:
# Lark reraises it's errors, but the original ones are needed
raise e.orig_exc
original_error = e.orig_exc
if not isinstance(original_error, (AssertionError, LarkError)):
raise original_error

raise RQLFilterParsingError()

qs = self._apply_ordering(qs, rql_transformer.ordering_filters)
select_filters = rql_transformer.select_filters
Expand Down
4 changes: 1 addition & 3 deletions dj_rql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def parse_query(self, query):
rql_ast = RQLParser.parse(query)
return rql_ast
except LarkError:
raise RQLFilterParsingError(details={
'error': 'Bad filter query.',
})
raise RQLFilterParsingError()


RQLParser = RQLLarkParser(RQL_GRAMMAR, parser='lalr', start='start')
11 changes: 9 additions & 2 deletions tests/test_filter_cls/test_apply_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def apply_filters(query):
return list(q)


def test_parsing_error():
bad_query = 'q='
@pytest.mark.parametrize('bad_query', ['q=', '(select(stats.attributes))&select(stats.attributes)'])
def test_parsing_error(bad_query):
with pytest.raises(RQLFilterParsingError) as e:
apply_filters(bad_query)

assert e.value.details['error'] == 'Bad filter query.'


Expand Down Expand Up @@ -176,6 +177,12 @@ def test_null_with_in_or():
assert apply_filters('or(title=eq={0},eq(title,{1}))'.format(title, RQL_NULL)) == books


@pytest.mark.django_db
def test_non_select_for_select_field():
with pytest.raises(RQLFilterLookupError):
apply_filters('select_author=abc')


@pytest.mark.django_db
def test_null_on_foreign_key_pk():
publisher = Publisher.objects.create()
Expand Down

0 comments on commit 8f2210d

Please sign in to comment.