Skip to content

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

Fix : #13694 NoneType in productChannels and removeVariants in productChannelListingUpdate (#13782)

Manoj Gowrae6949d730b34
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8305477022..b3ae15b12f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -49,6 +49,7 @@ in 3.17. Use `PaymentSettings.defaultTransactionFlowStrategy` instead.
- Add filter by slugs to attribute choices - #13761 by @rafiwts
- Add a new `product` field on `AssignedProductAttributeValue`. First part of a simplification of Attribute - Product relation from #12881. by @aniav
- Lazy legacy webhooks payload generation - #13758 by @maarcingebala
+- Fix NoneType in `prodcutChannelsListingUpdate` - #13694 by @Manoj-gowra
# 3.15.0 [Unreleased]
diff --git a/saleor/graphql/channel/mutations/base_channel_listing.py b/saleor/graphql/channel/mutations/base_channel_listing.py
index 2e143a45a9..de022ef3b7 100644
--- a/saleor/graphql/channel/mutations/base_channel_listing.py
+++ b/saleor/graphql/channel/mutations/base_channel_listing.py
@@ -77,7 +77,6 @@ class BaseChannelListingMutation(BaseMutation):
cls.validate_duplicated_channel_values(
remove_channels_ids, "remove_channels", errors, error_code
)
-
if errors:
return {}
channels_to_add: List["models.Channel"] = []
@@ -85,16 +84,18 @@ class BaseChannelListingMutation(BaseMutation):
channels_to_add = cls.get_nodes_or_error(
add_channels_ids, "channel_id", Channel
)
- remove_channels_pks = cls.get_global_ids_or_error(
- remove_channels_ids, Channel, field="remove_channels"
- )
+ if remove_channels_ids:
+ remove_channels_pks = cls.get_global_ids_or_error(
+ remove_channels_ids, Channel, field="remove_channels"
+ )
+ else:
+ remove_channels_pks = []
cleaned_input = {input_source: [], "remove_channels": remove_channels_pks}
for channel_listing, channel in zip(add_channels, channels_to_add):
channel_listing["channel"] = channel
cleaned_input[input_source].append(channel_listing)
-
return cleaned_input
@classmethod
diff --git a/saleor/graphql/channel/tests/mutations/test_base_channel_listing.py b/saleor/graphql/channel/tests/mutations/test_base_channel_listing.py
index ad76096ed2..833ac3350f 100644
--- a/saleor/graphql/channel/tests/mutations/test_base_channel_listing.py
+++ b/saleor/graphql/channel/tests/mutations/test_base_channel_listing.py
@@ -115,6 +115,22 @@ def test_clean_channels_remove_channels(channel_PLN):
assert errors["input"] == []
+def test_clean_channels_remove_channels_is_null(channel_PLN):
+ # given
+ channel_id = None
+ error_code = ShippingErrorCode.DUPLICATED_INPUT_ITEM.value
+ errors = defaultdict(list)
+
+ # when
+ result = BaseChannelListingMutation.clean_channels(
+ None, {"remove_channels": channel_id}, errors, error_code
+ )
+
+ # then
+ assert result == {"add_channels": [], "remove_channels": []}
+ assert errors["input"] == []
+
+
def test_test_clean_channels_with_errors(channel_PLN):
# given
channel_id = graphene.Node.to_global_id("Channel", channel_PLN.id)
diff --git a/saleor/graphql/core/utils/__init__.py b/saleor/graphql/core/utils/__init__.py
index ea31889310..d2772f47f9 100644
--- a/saleor/graphql/core/utils/__init__.py
+++ b/saleor/graphql/core/utils/__init__.py
@@ -37,7 +37,9 @@ def get_duplicates_items(first_list, second_list):
def get_duplicated_values(values):
"""Return set of duplicated values."""
- return {value for value in values if values.count(value) > 1}
+ if values:
+ return {value for value in values if values.count(value) > 1}
+ return {}
@overload
diff --git a/saleor/graphql/product/tests/mutations/test_product_channel_listing_update.py b/saleor/graphql/product/tests/mutations/test_product_channel_listing_update.py
index 718eddee10..e4d7739107 100644
--- a/saleor/graphql/product/tests/mutations/test_product_channel_listing_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_channel_listing_update.py
@@ -1187,6 +1187,38 @@ def test_product_channel_listing_remove_variant_as_staff_user(
assert len(variant.channel_listings.all()) == 1
+def test_product_channel_listing_remove_variant_is_None_as_staff_user(
+ staff_api_client, product, permission_manage_products, channel_USD, channel_PLN
+):
+ # given
+ variant = product.variants.first()
+ ProductVariantChannelListing.objects.create(channel=channel_PLN, variant=variant)
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [{"channelId": channel_id, "removeVariants": None}]
+ },
+ }
+
+ assert len(variant.channel_listings.all()) == 2
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["productChannelListingUpdate"]
+ assert not data["errors"]
+
+ assert len(variant.channel_listings.all()) == 2
+
+
def test_product_channel_listing_remove_variant_as_app(
app_api_client, product, permission_manage_products, channel_USD, channel_PLN
):
@@ -1222,6 +1254,38 @@ def test_product_channel_listing_remove_variant_as_app(
assert len(variant.channel_listings.all()) == 1
+def test_product_channel_listing_remove_variant_is_None_as_app(
+ app_api_client, product, permission_manage_products, channel_USD, channel_PLN
+):
+ # given
+ variant = product.variants.first()
+ ProductVariantChannelListing.objects.create(channel=channel_PLN, variant=variant)
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [{"channelId": channel_id, "removeVariants": None}]
+ },
+ }
+
+ assert len(variant.channel_listings.all()) == 2
+ # when
+ response = app_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["productChannelListingUpdate"]
+ assert not data["errors"]
+
+ assert len(variant.channel_listings.all()) == 2
+
+
def test_product_channel_listing_remove_variant_removes_checkout_lines(
staff_api_client,
product,