Skip to content

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

Fix error handling for required value attributes in productBulkCreate (#14803) (#14811)

SzymJf296a12625c3
diff --git a/saleor/graphql/product/bulk_mutations/product_bulk_create.py b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
index bd2812fd3a..0f49b69059 100644
--- a/saleor/graphql/product/bulk_mutations/product_bulk_create.py
+++ b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
@@ -298,14 +298,15 @@ class ProductBulkCreate(BaseMutation):
product_index, exc, index_error_map, "attributes"
)
else:
- index_error_map[product_index].append(
- ProductBulkCreateError(
- path="attributes",
- message=exc.message,
- code=exc.code,
+ for error in exc.error_list:
+ index_error_map[product_index].append(
+ ProductBulkCreateError(
+ path="attributes",
+ message=error.message,
+ code=error.code,
+ )
)
- )
- attributes_errors_count += 1
+ attributes_errors_count += 1
return attributes_errors_count
@classmethod
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 18dc756fd5..9080e21aef 100644
--- a/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
@@ -375,6 +375,62 @@ def test_product_bulk_create_with_invalid_attributes(
assert error["message"] == "Invalid ID: invalidID. Expected: Attribute."
+def test_product_bulk_create_without_value_required_attribute(
+ staff_api_client,
+ product_type_with_value_required_attributes,
+ category,
+ description_json,
+ permission_manage_products,
+):
+ # given
+ product_type = product_type_with_value_required_attributes
+ description_json_string = json.dumps(description_json)
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ color_attr = product_type.product_attributes.get(name="Color")
+ color_attr_id = graphene.Node.to_global_id("Attribute", color_attr.id)
+
+ product_name_1 = "test name 1"
+ base_product_slug = "product-test-slug"
+ product_charge_taxes = True
+ product_tax_rate = "STANDARD"
+
+ products = [
+ {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name_1,
+ "slug": f"{base_product_slug}-1",
+ "description": description_json_string,
+ "chargeTaxes": product_charge_taxes,
+ "taxCode": product_tax_rate,
+ "weight": 2,
+ "attributes": [
+ {"id": color_attr_id, "values": ["Green"]},
+ ],
+ }
+ ]
+
+ # 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
+ assert data["count"] == 0
+ assert data["results"][0]["errors"]
+ error = data["results"][0]["errors"][0]
+ assert error["path"] == "attributes"
+ assert (
+ error["message"]
+ == "All attributes flagged as having a value required must be supplied."
+ )
+
+
def test_product_bulk_create_with_media(
staff_api_client,
product_type,
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 3f59625645..519aa969b4 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -2527,6 +2527,26 @@ def product_type(product_type_generator):
return product_type_generator()
+def product_type_with_value_required_attributes(
+ color_attribute, size_attribute, default_tax_class
+):
+ product_type = ProductType.objects.create(
+ name="Default Type",
+ slug="default-type",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=True,
+ is_shipping_required=True,
+ tax_class=default_tax_class,
+ )
+ color_attribute.value_required = True
+ size_attribute.value_required = True
+ Attribute.objects.bulk_update([color_attribute, size_attribute], ["value_required"])
+ product_type.product_attributes.add(color_attribute)
+ product_type.product_attributes.add(size_attribute)
+ return product_type
+
+
@pytest.fixture
def product_type_list():
product_type_1 = ProductType.objects.create(