Skip to content

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

Fix slug creation (#14523) (#14631)

SzymJbdfb1e65c540
diff --git a/saleor/graphql/product/bulk_mutations/product_bulk_create.py b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
index 3b3de509c9..77de5fc6ba 100644
--- a/saleor/graphql/product/bulk_mutations/product_bulk_create.py
+++ b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
@@ -210,6 +210,12 @@ class ProductBulkCreate(BaseMutation):
def generate_unique_slug(cls, slugable_value, new_slugs):
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 = "-"
+
search_field = "slug__iregex"
pattern = rf"{slug}-\d+$|{slug}$"
lookup = {search_field: pattern}
diff --git a/saleor/graphql/product/tests/mutations/test_product_bulk_create.py b/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
index d609930307..00c0e56a14 100644
--- a/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
@@ -150,6 +150,52 @@ def test_product_bulk_create_with_base_data(
assert args == {product.id for product in products}
+def test_product_bulk_create_with_no_slug_and_name_with_unslugify_characters(
+ staff_api_client,
+ product_type,
+ category,
+ permission_manage_products,
+):
+ # given
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+
+ product_name = "-->"
+
+ products = [
+ {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ },
+ {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ },
+ ]
+
+ # when
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_graphql(
+ PRODUCT_BULK_CREATE_MUTATION,
+ {"products": products},
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productBulkCreate"]
+
+ # then
+ products = Product.objects.all()
+ assert not data["results"][0]["errors"]
+ assert not data["results"][1]["errors"]
+ assert data["count"] == 2
+ assert data["results"][0]["product"]["name"] == product_name
+ assert data["results"][0]["product"]["slug"] == "-"
+ assert data["results"][1]["product"]["name"] == product_name
+ assert data["results"][1]["product"]["slug"] == "--2"
+ assert len(products) == 2
+
+
@patch("saleor.plugins.manager.PluginsManager.product_created")
def test_product_bulk_create_send_product_created_webhook(
created_webhook_mock,