Skip to content

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

Fix media creation mutations when `alt` field is `null` (#18864)

Przemysław Łada6c886deda1a6
diff --git a/saleor/graphql/product/bulk_mutations/product_bulk_create.py b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
index 25bb323507..b004619053 100644
--- a/saleor/graphql/product/bulk_mutations/product_bulk_create.py
+++ b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
@@ -440,7 +440,9 @@ class ProductBulkCreate(BaseMutation):
for index, media_input in enumerate(media_inputs):
image = media_input.get("image")
media_url = media_input.get("media_url")
- alt = media_input.get("alt")
+ # Replace null alt value with an empty string to satisfy DB constraints
+ alt = media_input.get("alt") or ""
+ media_input["alt"] = alt
if not image and not media_url:
index_error_map[product_index].append(
diff --git a/saleor/graphql/product/mutations/product/product_media_create.py b/saleor/graphql/product/mutations/product/product_media_create.py
index b48910e80a..b45449beb1 100644
--- a/saleor/graphql/product/mutations/product/product_media_create.py
+++ b/saleor/graphql/product/mutations/product/product_media_create.py
@@ -111,7 +111,8 @@ class ProductMediaCreate(BaseMutation):
qs=models.Product.objects.all(),
)
- alt = input.get("alt", "")
+ # Replace null alt value with an empty string to satisfy DB constraints
+ alt = input.get("alt") or ""
media_url = input.get("media_url")
media = None
if img_data := input.get("image"):
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 0417d23ff5..f27cb8bf1b 100644
--- a/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
@@ -3222,3 +3222,84 @@ def test_product_bulk_create_with_media_incorrect_alt(
assert error_2[0]["path"] == "media.0.alt"
assert len(error_2) == 1
assert data["count"] == 0
+
+
+def test_product_bulk_create_with_media_when_alt_is_null(
+ staff_api_client,
+ product_type,
+ category,
+ permission_manage_products,
+ media_root,
+):
+ # given
+ image_file, image_name = create_image()
+ variables = [
+ {
+ "productType": graphene.Node.to_global_id("ProductType", product_type.pk),
+ "category": graphene.Node.to_global_id("Category", category.pk),
+ "name": "test name 1",
+ "media": [
+ {
+ "alt": None,
+ "image": image_name,
+ }
+ ],
+ },
+ ]
+
+ # when
+ body = get_multipart_request_body_with_multiple_files(
+ PRODUCT_BULK_CREATE_MUTATION,
+ {"products": variables},
+ [image_file],
+ {0: ["variables.products.0.media.0.image"]},
+ )
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_multipart(body)
+ content = get_graphql_content(response)["data"]["productBulkCreate"]
+
+ # then
+ assert not content["results"][0]["errors"]
+ assert content["results"][0]["product"]["media"][0]["alt"] == ""
+
+
+@patch("saleor.graphql.product.bulk_mutations.product_bulk_create.HTTPClient")
+def test_product_bulk_create_with_media_url_when_alt_is_null(
+ mock_HTTPClient,
+ staff_api_client,
+ product_type,
+ category,
+ permission_manage_products,
+ media_root,
+):
+ # given
+ image_file, _ = create_image()
+ mock_response = Mock()
+ mock_response.headers.get.return_value = image_file.content_type
+ mock_response.content = image_file.read()
+ mock_HTTPClient.send_request.return_value.__enter__.return_value = mock_response
+ variables = [
+ {
+ "productType": graphene.Node.to_global_id("ProductType", product_type.pk),
+ "category": graphene.Node.to_global_id("Category", category.pk),
+ "name": "test name 1",
+ "media": [
+ {
+ "alt": None,
+ "mediaUrl": "https://saleor.io/image-path",
+ }
+ ],
+ },
+ ]
+
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_BULK_CREATE_MUTATION,
+ variables={"products": variables},
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)["data"]["productBulkCreate"]
+
+ # then
+ assert not content["results"][0]["errors"]
+ assert content["results"][0]["product"]["media"][0]["alt"] == ""
diff --git a/saleor/graphql/product/tests/mutations/test_product_media_create.py b/saleor/graphql/product/tests/mutations/test_product_media_create.py
index 87c39cfeb4..bb3737f837 100644
--- a/saleor/graphql/product/tests/mutations/test_product_media_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_media_create.py
@@ -438,3 +438,57 @@ def test_product_media_create_mutation_alt_character_limit(
errors = content["data"]["productMediaCreate"]["errors"]
assert errors[0]["field"] == "input"
assert errors[0]["code"] == ProductErrorCode.INVALID.name
+
+
+def test_product_media_create_when_alt_is_null(
+ staff_api_client, product, permission_manage_products, media_root
+):
+ # given
+ image_file, image_name = create_image()
+ variables = {
+ "product": graphene.Node.to_global_id("Product", product.id),
+ "alt": None,
+ "image": image_name,
+ }
+
+ # when
+ body = get_multipart_request_body(
+ PRODUCT_MEDIA_CREATE_QUERY, variables, image_file, image_name
+ )
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_multipart(body)
+ content = get_graphql_content(response)["data"]["productMediaCreate"]
+
+ # then
+ assert not content["errors"]
+ assert content["product"]["media"][0]["alt"] == ""
+
+
+@patch("saleor.graphql.product.mutations.product.product_media_create.HTTPClient")
+def test_product_media_create_with_media_url_when_alt_is_null(
+ mock_HTTPClient, staff_api_client, product, permission_manage_products, media_root
+):
+ # given
+ image_file, _ = create_image()
+ mock_response = Mock()
+ mock_response.headers.get.return_value = image_file.content_type
+ mock_response.content = image_file.read()
+ mock_HTTPClient.send_request.return_value.__enter__.return_value = mock_response
+ variables = {
+ "product": graphene.Node.to_global_id("Product", product.id),
+ "mediaUrl": "https://saleor.io/image-path",
+ "alt": None,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_MEDIA_CREATE_QUERY,
+ variables=variables,
+ permissions=[permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)["data"]["productMediaCreate"]
+
+ # then
+ assert not content["errors"]
+ assert content["product"]["media"][0]["alt"] == ""