Skip to content

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

Fix for unnecesary attributes validation while updating simple product (#6300)

Grzegorz Derdak85253013ffdf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e1ca1a0a14..794d0fbaa0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -75,6 +75,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix filtering products by multiple attributes - #6215 by @GrzegorzDerdak
- Add attributes validation while creating/updating a product's variant - #6269 by @GrzegorzDerdak
- Add metadata to page model - #6292 by @dominik-zeglen
+- Fix for unnecesary attributes validation while updating simple product - #6300 by @GrzegorzDerdak
## 2.10.2
diff --git a/saleor/graphql/core/tests/test_core.py b/saleor/graphql/core/tests/test_core.py
index d825383317..b5785aa800 100644
--- a/saleor/graphql/core/tests/test_core.py
+++ b/saleor/graphql/core/tests/test_core.py
@@ -229,6 +229,54 @@ def test_mutation_positive_decimal_input_without_arguments(
assert data["errors"] == []
+def test_mutation_update_product_with_default_variant(
+ product_with_default_variant, staff_api_client, permission_manage_products
+):
+ # given
+ query = """
+ mutation ProductVariantUpdate(
+ $id: ID!,
+ $sku: String,
+ ) {
+ productVariantUpdate(
+ id: $id,
+ input: {
+ sku: $sku,
+ }
+ ) {
+ errors {
+ field
+ message
+ }
+ productVariant {
+ sku
+ }
+ }
+ }
+ """
+ variant = product_with_default_variant.variants.first()
+ variant_id = variant.pk
+
+ assert product_with_default_variant.product_type.has_variants is False
+
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_id),
+ "sku": "Updated product SKU",
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantUpdate"]
+
+ assert data["errors"] == []
+ assert data["productVariant"]["sku"] == "Updated product SKU"
+
+
def test_filter_input():
class CreatedEnum(graphene.Enum):
WEEK = "week"
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 1362fbfab5..8ca9a60e68 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1281,7 +1281,7 @@ class ProductVariantCreate(ModelMutation):
if stocks:
cls.check_for_duplicates_in_stocks(stocks)
- if instance.product_id is not None:
+ if instance.pk:
# If the variant is getting updated,
# simply retrieve the associated product type
product_type = instance.product.product_type
@@ -1294,18 +1294,22 @@ class ProductVariantCreate(ModelMutation):
cleaned_input["product"]
)
- # Attributes are provided as list of `AttributeValueInput` objects.
- # We need to transform them into the format they're stored in the
- # `Product` model, which is HStore field that maps attribute's PK to
- # the value's PK.
- attributes = cleaned_input.get("attributes", [])
- try:
- cls.validate_duplicated_attribute_values(
- attributes, used_attribute_values, instance
- )
- cleaned_input["attributes"] = cls.clean_attributes(attributes, product_type)
- except ValidationError as exc:
- raise ValidationError({"attributes": exc})
+ # Run the validation only if product type is configurable
+ if product_type.has_variants:
+ # Attributes are provided as list of `AttributeValueInput` objects.
+ # We need to transform them into the format they're stored in the
+ # `Product` model, which is HStore field that maps attribute's PK to
+ # the value's PK.
+ attributes = cleaned_input.get("attributes", [])
+ try:
+ cls.validate_duplicated_attribute_values(
+ attributes, used_attribute_values, instance
+ )
+ cleaned_input["attributes"] = cls.clean_attributes(
+ attributes, product_type
+ )
+ except ValidationError as exc:
+ raise ValidationError({"attributes": exc})
return cleaned_input