Skip to content

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

Fix generating slug for product create mutation (#14378) (#14402)

SzymJ7eb5b8910e7b
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index 9c4643b6f5..67c57addb7 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -111,6 +111,11 @@ def generate_unique_slug(
"""
slug = slugify(unidecode(slugable_value))
+ # in case when slugable_value contains only not allowed in slug characters, slugify
+ # function will return empty string, so we need to provide some default value
+ if slug == "":
+ slug = "-"
+
ModelClass = instance.__class__
search_field = f"{slug_field_name}__iregex"
diff --git a/saleor/graphql/product/tests/mutations/test_product_create.py b/saleor/graphql/product/tests/mutations/test_product_create.py
index bb67937680..f01edbc852 100644
--- a/saleor/graphql/product/tests/mutations/test_product_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_create.py
@@ -194,6 +194,59 @@ def test_create_product(
)
+def test_create_product_without_slug_and_not_allowed_characters_for_slug_in_name(
+ staff_api_client,
+ product_type,
+ permission_manage_products,
+):
+ # given
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "name": "->>",
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ CREATE_PRODUCT_MUTATION, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ assert data["errors"] == []
+ assert data["product"]["slug"] == "-"
+
+
+def test_create_second_product_without_slug_and_not_allowed_characters_for_slug_in_name(
+ staff_api_client, product_type, permission_manage_products, product
+):
+ # given
+ product.slug = "-"
+ product.save(update_fields=["slug"])
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "name": "->>",
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ CREATE_PRODUCT_MUTATION, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ assert data["errors"] == []
+ assert data["product"]["slug"] == "--2"
+
+
def test_create_product_use_tax_class_from_product_type(
staff_api_client,
product_type,