Skip to content

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

Convert snake_case field names to camelCase

Adam Janik0554e2710515
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index 1b46d73dcc..a5f91cc356 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -13,6 +13,7 @@ from ..account.types import User
from ..file_upload.types import Upload
from ..utils import get_node, get_nodes
from .types.common import Error
+from .utils import snake_to_camel_case
registry = get_global_registry()
@@ -199,6 +200,7 @@ class ModelMutation(BaseMutation):
if field in cls._meta.exclude:
continue
for message in message_dict[field]:
+ field = snake_to_camel_case(field)
cls.add_error(errors, field, message)
return errors
diff --git a/saleor/graphql/core/utils.py b/saleor/graphql/core/utils.py
index 5a1702ab5b..ceef852950 100644
--- a/saleor/graphql/core/utils.py
+++ b/saleor/graphql/core/utils.py
@@ -4,3 +4,11 @@ def clean_seo_fields(data):
if seo_fields:
data['seo_title'] = seo_fields.get('title')
data['seo_description'] = seo_fields.get('description')
+
+
+def snake_to_camel_case(name):
+ """Convert snake_case variable name to camelCase."""
+ if isinstance(name, str):
+ splitted = name.split('_')
+ return splitted[0] + "".join(map(str.capitalize, splitted[1:]))
+ return name
diff --git a/tests/api/test_core.py b/tests/api/test_core.py
index 1ab58ee949..4e7bd768b1 100644
--- a/tests/api/test_core.py
+++ b/tests/api/test_core.py
@@ -9,7 +9,7 @@ from django_prices_vatlayer.models import VAT
from tests.utils import get_graphql_content
from saleor.core.permissions import MODELS_PERMISSIONS
-from saleor.graphql.core.utils import clean_seo_fields
+from saleor.graphql.core.utils import clean_seo_fields, snake_to_camel_case
from saleor.graphql.product import types as product_types
from saleor.graphql.utils import get_database_id
from .utils import assert_no_permission
@@ -246,3 +246,8 @@ def test_get_database_id(product):
node_id = graphene.Node.to_global_id('Product', product.pk)
pk = get_database_id(info, node_id, product_types.Product)
assert int(pk) == product.pk
+
+
+def test_snake_to_camel_case():
+ assert snake_to_camel_case('test_camel_case') == 'testCamelCase'
+ assert snake_to_camel_case('testCamel_case') == 'testCamelCase'