Skip to content

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

Clean address fields when input is empty string (#10334)

Maciej Korycinskid8dcf723f905
diff --git a/saleor/graphql/account/i18n.py b/saleor/graphql/account/i18n.py
index b90856a1d4..0891a01a1f 100644
--- a/saleor/graphql/account/i18n.py
+++ b/saleor/graphql/account/i18n.py
@@ -83,8 +83,13 @@ class I18nMixin:
errors_dict[field] = errors
else:
address_form.cleaned_data[field] = address_form.data[field]
- if required_check and error.code == "required":
- errors_dict[field] = errors
+ if error.code == "required":
+ field_value = address_form.data.get(field)
+ if required_check:
+ errors_dict[field] = errors
+ elif field_value is not None:
+ address_form.cleaned_data[field] = field_value
+
return errors_dict
@classmethod
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
index 525c434f02..ed1e060dc9 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
@@ -236,6 +236,39 @@ def test_checkout_billing_address_update_with_skip_required_doesnt_raise_error(
assert checkout_with_items.billing_address
+def test_checkout_billing_address_update_with_skip_required_overwrite_address(
+ checkout_with_items, user_api_client, address
+):
+ # given
+ checkout_with_items.billing_address = address
+ checkout_with_items.save()
+
+ variables = {
+ "id": to_global_id_or_none(checkout_with_items),
+ "billingAddress": {
+ "postalCode": "",
+ "city": "",
+ "country": "US",
+ },
+ "validationRules": {"checkRequiredFields": False},
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_BILLING_ADDRESS_UPDATE, variables
+ )
+
+ # then
+ checkout_with_items.refresh_from_db()
+
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutBillingAddressUpdate"]
+ assert not data["errors"]
+
+ assert checkout_with_items.billing_address.city == ""
+ assert checkout_with_items.billing_address.postal_code == ""
+
+
def test_checkout_billing_address_update_with_skip_required_raises_validation_error(
checkout_with_items, user_api_client
):
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
index 3e36090849..be01c35557 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
@@ -446,6 +446,7 @@ def test_checkout_shipping_address_update_exclude_shipping_method(
"address_data",
[
{"country": "PL"}, # missing postalCode, streetAddress
+ {"country": "PL", "postalCode": ""},
{"country": "PL", "postalCode": "53-601"}, # missing streetAddress
{"country": "US"},
{
@@ -478,6 +479,39 @@ def test_checkout_shipping_address_update_with_skip_required_doesnt_raise_error(
assert checkout_with_items.shipping_address
+def test_checkout_shipping_address_update_with_skip_required_overwrite_address(
+ checkout_with_items, user_api_client, address
+):
+ # given
+ checkout_with_items.shipping_address = address
+ checkout_with_items.save()
+
+ variables = {
+ "id": to_global_id_or_none(checkout_with_items),
+ "shippingAddress": {
+ "postalCode": "",
+ "city": "",
+ "country": "US",
+ },
+ "validationRules": {"checkRequiredFields": False},
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+
+ # then
+ checkout_with_items.refresh_from_db()
+
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ assert not data["errors"]
+
+ assert checkout_with_items.shipping_address.city == ""
+ assert checkout_with_items.shipping_address.postal_code == ""
+
+
def test_checkout_shipping_address_update_with_skip_required_raises_validation_error(
checkout_with_items, user_api_client
):