Skip to content

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

Change required and allowed fields returning by AddressValidationRules to camel case

Aleksander Spyra8647798c3d0a
diff --git a/saleor/graphql/account/resolvers.py b/saleor/graphql/account/resolvers.py
index 795e3ddd3a..82d61d04f9 100644
--- a/saleor/graphql/account/resolvers.py
+++ b/saleor/graphql/account/resolvers.py
@@ -13,6 +13,7 @@ from ...payment.utils import (
)
from ..utils import filter_by_query_param
from .types import AddressValidationData, ChoiceValue
+from .utils import get_allowed_fields_camel_case, get_required_fields_camel_case
USER_SEARCH_FIELDS = (
"email",
@@ -66,8 +67,8 @@ def resolve_address_validator(
country_name=rules.country_name,
address_format=rules.address_format,
address_latin_format=rules.address_latin_format,
- allowed_fields=rules.allowed_fields,
- required_fields=rules.required_fields,
+ allowed_fields=get_allowed_fields_camel_case(rules.allowed_fields),
+ required_fields=get_required_fields_camel_case(rules.required_fields),
upper_fields=rules.upper_fields,
country_area_type=rules.country_area_type,
country_area_choices=[
diff --git a/saleor/graphql/account/utils.py b/saleor/graphql/account/utils.py
index 2b1a495489..941adb6bb9 100644
--- a/saleor/graphql/account/utils.py
+++ b/saleor/graphql/account/utils.py
@@ -1,4 +1,5 @@
from django.core.exceptions import ValidationError
+from graphene.utils.str_converters import to_camel_case
from ...account import events as account_events
@@ -42,3 +43,24 @@ class StaffDeleteMixin(UserDeleteMixin):
super().clean_instance(info, instance)
if not instance.is_staff:
raise ValidationError({"id": "Cannot delete a non-staff user."})
+
+
+def get_required_fields_camel_case(required_fields: set) -> set:
+ """Return set of AddressValidationRules required fields in camel case."""
+ return {validation_field_to_camel_case(field) for field in required_fields}
+
+
+def validation_field_to_camel_case(name: str) -> str:
+ """Convert name of the field from snake case to camel case."""
+ name = to_camel_case(name)
+ if name == "streetAddress":
+ return "streetAddress1"
+ return name
+
+
+def get_allowed_fields_camel_case(allowed_fields: set) -> set:
+ """Return set of AddressValidationRules allowed fields in camel case."""
+ fields = {validation_field_to_camel_case(field) for field in allowed_fields}
+ if "streetAddress1" in fields:
+ fields.add("streetAddress2")
+ return fields
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 32fbcf6087..863a4e0b9c 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1868,6 +1868,28 @@ def test_address_validator_with_country_area(user_api_client):
assert not data["cityAreaChoices"]
+def test_address_validator_fields_in_camel_case(user_api_client):
+ query = """
+ query getValidator(
+ $country_code: CountryCode!) {
+ addressValidationRules(countryCode: $country_code) {
+ requiredFields
+ allowedFields
+ }
+ }
+ """
+ variables = {"country_code": "PL"}
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["addressValidationRules"]
+ required_fields = data["requiredFields"]
+ allowed_fields = data["allowedFields"]
+ assert "streetAddress1" in required_fields
+ assert "streetAddress2" not in required_fields
+ assert "streetAddress1" in allowed_fields
+ assert "streetAddress2" in allowed_fields
+
+
CUSTOMER_PASSWORD_RESET_MUTATION = """
mutation CustomerPasswordReset($email: String!) {
customerPasswordReset(input: {email: $email}) {