Skip to content

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

Add validator for required field 'name' in attribute bulk mutations (#16166)

Piotr Zabieglik255d39da978a
diff --git a/saleor/graphql/attribute/mutations/attribute_bulk_create.py b/saleor/graphql/attribute/mutations/attribute_bulk_create.py
index cb992367c6..df310557b0 100644
--- a/saleor/graphql/attribute/mutations/attribute_bulk_create.py
+++ b/saleor/graphql/attribute/mutations/attribute_bulk_create.py
@@ -96,7 +96,11 @@ def clean_values(
slugs_list = list(attribute.values.values_list("slug", flat=True))
duplicated_names = get_duplicated_values(
- [unidecode(value_data.name.lower().strip()) for value_data in values]
+ [
+ unidecode(value_data.name.lower().strip())
+ for value_data in values
+ if value_data.name
+ ]
)
for value_index, value_data in enumerate(values):
@@ -121,6 +125,16 @@ def clean_values(
)
continue
+ if not value_data.name:
+ index_error_map[attribute_index].append(
+ error_class(
+ path=f"{path_prefix}.{value_index}.name",
+ message="The field is required.",
+ code=error_class.code.REQUIRED.value,
+ )
+ )
+ continue
+
if unidecode(value_data.name.lower().strip()) in duplicated_names:
index_error_map[attribute_index].append(
error_class(
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_bulk_update.py b/saleor/graphql/attribute/tests/mutations/test_attribute_bulk_update.py
index 4336cab4e9..a1c8fedd51 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_bulk_update.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_bulk_update.py
@@ -835,3 +835,48 @@ def test_attribute_bulk_update_removes_value_with_invalid_id(
assert errors
assert errors[0]["code"] == AttributeBulkUpdateErrorCode.INVALID.name
assert errors[0]["path"] == "removeValues.0"
+
+
+def test_attribute_bulk_update_add_value_missing_name(
+ staff_api_client,
+ color_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ attr_external_ref = color_attribute.external_reference
+ attributes = [
+ {
+ "externalReference": attr_external_ref,
+ "fields": {
+ "addValues": [
+ {
+ "externalReference": "new_ext_reference",
+ },
+ {
+ "name": "",
+ },
+ ]
+ },
+ }
+ ]
+ staff_api_client.user.user_permissions.add(
+ permission_manage_product_types_and_attributes
+ )
+
+ # when
+ response = staff_api_client.post_graphql(
+ ATTRIBUTE_BULK_UPDATE_MUTATION,
+ {"attributes": attributes},
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["attributeBulkUpdate"]
+ errors = data["results"][0]["errors"]
+ assert data["count"] == 0
+ assert errors
+ assert len(errors) == 2
+ assert errors[0]["path"] == "addValues.0.name"
+ assert errors[0]["code"] == AttributeBulkUpdateErrorCode.REQUIRED.name
+ assert errors[1]["path"] == "addValues.1.name"
+ assert errors[1]["code"] == AttributeBulkUpdateErrorCode.REQUIRED.name