Skip to content

Commit

Permalink
fix: defend from empty filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tadams42 committed Jul 22, 2023
1 parent 469d76c commit 78189e2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/pypi-publish.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see:
# For more information see:
# - https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
# - https://docs.pypi.org/trusted-publishers/using-a-publisher/

Expand All @@ -23,15 +23,21 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3

- name: Set up 🐍
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
cache: 'pip'
cache-dependency-path: pyproject.toml

- name: 📦 Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
- name: 🏗 Build package 🚧
run: python -m build
- name: Publish package distributions to PyPI

- name: 🚚 Publish package distributions to PyPI 🚀
uses: pypa/gh-action-pypi-publish@release/v1
11 changes: 7 additions & 4 deletions src/flask_rest_jsonapi_next/data_layers/filtering/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ def create_filters(model, filter_info, resource):
:param Resource resource: the resource
"""
filters = []

for filter_ in filter_info:
filters.append(Node(model, filter_, resource, resource.schema).resolve())
resolved = Node(model, filter_, resource, resource.schema).resolve()
if resolved is not None:
filters.append(resolved)

return filters

Expand Down Expand Up @@ -61,17 +64,17 @@ def resolve(self):
else:
return getattr(self.column, self.operator)(value)

if "or" in self.filter_:
if "or" in self.filter_ and self.filter_["or"]:
return or_(
Node(self.model, filt, self.resource, self.schema).resolve()
for filt in self.filter_["or"]
)
if "and" in self.filter_:
if "and" in self.filter_ and self.filter_["and"]:
return and_(
Node(self.model, filt, self.resource, self.schema).resolve()
for filt in self.filter_["and"]
)
if "not" in self.filter_:
if "not" in self.filter_ and self.filter_["not"]:
return not_(
Node(
self.model, self.filter_["not"], self.resource, self.schema
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlalchemy_filtering_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ def test_Node(person_model, person_schema):
n.related_model
with pytest.raises(InvalidFilters):
n.related_schema


def test_Node_empty_filter(person_model, person_schema):
for filt in [
{"and": []},
{"or": []},
{"not": []},
]:
resolved = Node(person_model, filt, None, person_schema).resolve()
assert resolved is None

0 comments on commit 78189e2

Please sign in to comment.