Skip to content

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

Fix for updating product variant without providing an attributes (#6353)

Grzegorz Derdak596cb4969c02
diff --git a/saleor/graphql/core/tests/test_core.py b/saleor/graphql/core/tests/test_core.py
index b5785aa800..38b9df71ee 100644
--- a/saleor/graphql/core/tests/test_core.py
+++ b/saleor/graphql/core/tests/test_core.py
@@ -130,21 +130,19 @@ def test_total_count_query(api_client, product):
def test_mutation_positive_decimal_input(
- staff_api_client, variant, size_attribute, stock, permission_manage_products
+ staff_api_client, variant, stock, permission_manage_products
):
query = """
mutation PositiveDecimalInput(
$id: ID!,
$cost: PositiveDecimal,
$price: PositiveDecimal,
- $attributes: [AttributeValueInput],
) {
productVariantUpdate(
id: $id,
input: {
costPrice: $cost,
price: $price,
- attributes: $attributes
}
) {
errors {
@@ -165,12 +163,6 @@ def test_mutation_positive_decimal_input(
"price": 15,
"cost": 12.12,
"quantity": 17,
- "attributes": [
- {
- "id": graphene.Node.to_global_id("Attribute", size_attribute.pk),
- "values": ["S"],
- }
- ],
}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
@@ -181,21 +173,19 @@ def test_mutation_positive_decimal_input(
def test_mutation_positive_decimal_input_without_arguments(
- staff_api_client, variant, size_attribute, permission_manage_products
+ staff_api_client, variant, permission_manage_products
):
query = """
mutation ProductVariantUpdate(
$id: ID!,
$price: PositiveDecimal,
$costPrice: PositiveDecimal,
- $attributes: [AttributeValueInput],
) {
productVariantUpdate(
id: $id,
input: {
costPrice: $costPrice,
price: $price,
- attributes: $attributes,
}
) {
errors {
@@ -214,12 +204,6 @@ def test_mutation_positive_decimal_input_without_arguments(
"id": graphene.Node.to_global_id("ProductVariant", variant.id),
"cost": 12.12,
"price": 15,
- "attributes": [
- {
- "id": graphene.Node.to_global_id("Attribute", size_attribute.pk),
- "values": ["S"],
- }
- ],
}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 8ca9a60e68..6d60a1259b 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1206,11 +1206,6 @@ class ProductVariantCreate(ModelMutation):
def clean_attributes(
cls, attributes: dict, product_type: models.ProductType
) -> T_INPUT_MAP:
- if not attributes:
- raise ValidationError(
- "All attributes must take a value.", ProductErrorCode.REQUIRED.value
- )
-
attributes_qs = product_type.variant_attributes
attributes = AttributeAssignmentMixin.clean_input(
attributes, attributes_qs, is_variant=True
@@ -1300,14 +1295,21 @@ class ProductVariantCreate(ModelMutation):
# 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", [])
+ 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
- )
+ if attributes:
+ cls.validate_duplicated_attribute_values(
+ attributes, used_attribute_values, instance
+ )
+ cleaned_input["attributes"] = cls.clean_attributes(
+ attributes, product_type
+ )
+ elif not instance.pk and not attributes:
+ # if attributes were not provided on creation
+ raise ValidationError(
+ "All attributes must take a value.",
+ ProductErrorCode.REQUIRED.value,
+ )
except ValidationError as exc:
raise ValidationError({"attributes": exc})
diff --git a/saleor/graphql/product/tests/test_product_minimal_variant_price.py b/saleor/graphql/product/tests/test_product_minimal_variant_price.py
index ebe7bf9590..964387c792 100644
--- a/saleor/graphql/product/tests/test_product_minimal_variant_price.py
+++ b/saleor/graphql/product/tests/test_product_minimal_variant_price.py
@@ -78,20 +78,17 @@ def test_product_variant_update_updates_minimal_variant_price(
mock_update_product_minimal_variant_price_task,
staff_api_client,
product,
- size_attribute,
permission_manage_products,
):
query = """
mutation ProductVariantUpdate(
$id: ID!,
$price: PositiveDecimal,
- $attributes: [AttributeValueInput],
) {
productVariantUpdate(
id: $id,
input: {
price: $price,
- attributes: $attributes
}
) {
productVariant {
@@ -106,12 +103,10 @@ def test_product_variant_update_updates_minimal_variant_price(
"""
variant = product.variants.first()
variant_id = to_global_id("ProductVariant", variant.pk)
- attribute_id = to_global_id("Attribute", size_attribute.pk)
price = "1.99"
variables = {
"id": variant_id,
"price": price,
- "attributes": [{"id": attribute_id, "values": ["S"]}],
}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]