Skip to content

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

Raise error for empty key in metadata

IKarbowiake7e6d86d6415
diff --git a/saleor/core/error_codes.py b/saleor/core/error_codes.py
index 220d5ced76..28790bf9cb 100644
--- a/saleor/core/error_codes.py
+++ b/saleor/core/error_codes.py
@@ -15,6 +15,7 @@ class MetadataErrorCode(Enum):
GRAPHQL_ERROR = "graphql_error"
INVALID = "invalid"
NOT_FOUND = "not_found"
+ REQUIRED = "required"
class TranslationErrorCode(Enum):
diff --git a/saleor/graphql/core/utils/error_codes.py b/saleor/graphql/core/utils/error_codes.py
index 33f44a427e..b9d3e64e7a 100644
--- a/saleor/graphql/core/utils/error_codes.py
+++ b/saleor/graphql/core/utils/error_codes.py
@@ -3,7 +3,7 @@ from enum import Enum
from ....account.error_codes import AccountErrorCode, PermissionGroupErrorCode
from ....app.error_codes import AppErrorCode
from ....checkout.error_codes import CheckoutErrorCode
-from ....core.error_codes import ShopErrorCode
+from ....core.error_codes import MetadataErrorCode, ShopErrorCode, TranslationErrorCode
from ....csv.error_codes import ExportErrorCode
from ....discount.error_codes import DiscountErrorCode
from ....giftcard.error_codes import GiftCardErrorCode
@@ -53,12 +53,14 @@ SALEOR_ERROR_CODE_ENUMS = [
GiftCardErrorCode,
InvoiceErrorCode,
MenuErrorCode,
+ MetadataErrorCode,
OrderErrorCode,
PaymentErrorCode,
PermissionGroupErrorCode,
ProductErrorCode,
ShippingErrorCode,
ShopErrorCode,
+ TranslationErrorCode,
]
saleor_error_codes = []
diff --git a/saleor/graphql/meta/mutations.py b/saleor/graphql/meta/mutations.py
index 1fefd051ca..6ce087c56e 100644
--- a/saleor/graphql/meta/mutations.py
+++ b/saleor/graphql/meta/mutations.py
@@ -1,3 +1,5 @@
+from typing import List
+
import graphene
from django.core.exceptions import ValidationError
@@ -120,11 +122,25 @@ class UpdateMetadata(BaseMetadataMutation):
required=True,
)
+ @classmethod
+ def clean_input(cls, metadata_list: List[dict]):
+ # raise an error when any of the key is empty
+ if not all([data["key"].strip() for data in metadata_list]):
+ raise ValidationError(
+ {
+ "input": ValidationError(
+ "Metadata key cannot be empty.",
+ code=MetadataErrorCode.REQUIRED.value,
+ )
+ }
+ )
+
@classmethod
def perform_mutation(cls, root, info, **data):
instance = cls.get_instance(info, **data)
if instance:
metadata_list = data.pop("input")
+ cls.clean_input(metadata_list)
items = {data.key: data.value for data in metadata_list}
instance.store_value_in_metadata(items=items)
instance.save(update_fields=["metadata"])
diff --git a/saleor/graphql/meta/tests/test_meta_mutations.py b/saleor/graphql/meta/tests/test_meta_mutations.py
index 8742841f54..21a92290e2 100644
--- a/saleor/graphql/meta/tests/test_meta_mutations.py
+++ b/saleor/graphql/meta/tests/test_meta_mutations.py
@@ -3,6 +3,7 @@ import uuid
from unittest.mock import patch
import graphene
+import pytest
from ....core.error_codes import MetadataErrorCode
from ....core.models import ModelWithMetadata
@@ -27,6 +28,7 @@ mutation UpdatePublicMetadata($id: ID!, $input: [MetadataInput!]!) {
metadataErrors{
field
code
+ message
}
item {
metadata{
@@ -203,6 +205,35 @@ def test_add_public_metadata_for_staff_as_app_no_permission(
assert_no_permission(response)
+ "input", [{"key": " ", "value": "test"}, {"key": " ", "value": ""}],
+)
+def test_staff_update_metadata_empty_key(
+ input, staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_staff,
+ admin_id,
+ "User",
+ input["key"],
+ input["value"],
+ )
+
+ # then
+ data = response["data"]["updateMetadata"]
+ errors = data["metadataErrors"]
+
+ assert not data["item"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == MetadataErrorCode.REQUIRED.name
+ assert errors[0]["field"] == "input"
+
+
def test_add_public_metadata_for_myself_as_customer(user_api_client):
# given
customer = user_api_client.user
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 42ada73dc8..31cd137821 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2532,6 +2532,7 @@ enum MetadataErrorCode {
GRAPHQL_ERROR
INVALID
NOT_FOUND
+ REQUIRED
}
input MetadataInput {