Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Return empty results when filtering by non-existing attribute (#7025)

Marcin Gębalad2db1b42138d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5f8c5a6b42..1ae770f4bb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -95,6 +95,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add default sorting by rank for search products - #6936 by @d-wysocki
- Fix exporting product description to xlsx - #6959 by @IKarbowiak
- Add `Shop.version` field to query API version - #6980 by @maarcingebala
+- Return empty results when filtering by non-existing attribute - #7025 by @maarcingebala
# 2.11.1
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index aa3c809636..ac2f02293b 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -85,7 +85,10 @@ def filter_products_by_attributes_values(qs, queries: T_PRODUCT_FILTER_QUERIES):
def filter_products_by_attributes(qs, filter_value):
- queries = _clean_product_attributes_filter_input(filter_value)
+ try:
+ queries = _clean_product_attributes_filter_input(filter_value)
+ except ValueError:
+ return Product.objects.none()
return filter_products_by_attributes_values(qs, queries)
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 1402999380..0105be9bfc 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -1529,6 +1529,19 @@ def test_products_query_with_filter_attributes(
assert products[0]["node"]["name"] == second_product.name
+def test_products_query_filter_by_non_existing_attribute(
+ query_products_with_filter, api_client, product_list, channel_USD
+):
+ variables = {
+ "channel": channel_USD.slug,
+ "filter": {"attributes": [{"slug": "i-do-not-exist", "values": ["red"]}]},
+ }
+ response = api_client.post_graphql(query_products_with_filter, variables)
+ content = get_graphql_content(response)
+ products = content["data"]["products"]["edges"]
+ assert len(products) == 0
+
+
def test_products_query_with_filter_product_type(
query_products_with_filter, staff_api_client, product, permission_manage_products
):