Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix attribute input validation for numeric attributes
IKarbowiak935677c68af7
diff --git a/saleor/graphql/attribute/types.py b/saleor/graphql/attribute/types.pyindex d82b88d5f9..a19d000ec2 100644--- a/saleor/graphql/attribute/types.py+++ b/saleor/graphql/attribute/types.py@@ -201,7 +201,7 @@ class AttributeInput(graphene.InputObjectType):class AttributeValueInput(graphene.InputObjectType):id = graphene.ID(description="ID of the selected attribute.")values = graphene.List(- graphene.String,+ graphene.NonNull(graphene.String),required=False,description=("The value or slug of an attribute to resolve. "diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.pyindex 03a8a948c5..c2fe6283db 100644--- a/saleor/graphql/attribute/utils.py+++ b/saleor/graphql/attribute/utils.py@@ -154,6 +154,8 @@ class AttributeAssignmentMixin:attribute: attribute_models.Attribute,attr_values: AttrValuesInput,):+ if not attr_values.values:+ return tuple()defaults = {"name": attr_values.values[0],}diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.pyindex dae315734c..169a841ff6 100644--- a/saleor/graphql/product/bulk_mutations/products.py+++ b/saleor/graphql/product/bulk_mutations/products.py@@ -158,7 +158,7 @@ class ProductBulkDelete(ModelBulkDeleteMutation):class BulkAttributeValueInput(InputObjectType):id = graphene.ID(description="ID of the selected attribute.")values = graphene.List(- graphene.String,+ graphene.NonNull(graphene.String),required=True,description=("The value or slug of an attribute to resolve. "@@ -169,7 +169,7 @@ class BulkAttributeValueInput(InputObjectType):class ProductVariantBulkCreateInput(ProductVariantInput):attributes = graphene.List(- BulkAttributeValueInput,+ graphene.NonNull(BulkAttributeValueInput),required=True,description="List of attributes specific to this variant.",)diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 1989ca54f0..33d7537aa6 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -733,7 +733,7 @@ class ProductDelete(ModelDeleteMutation):class ProductVariantInput(graphene.InputObjectType):attributes = graphene.List(- AttributeValueInput,+ graphene.NonNull(AttributeValueInput),required=False,description="List of attributes specific to this variant.",)@@ -749,7 +749,7 @@ class ProductVariantInput(graphene.InputObjectType):class ProductVariantCreateInput(ProductVariantInput):attributes = graphene.List(- AttributeValueInput,+ graphene.NonNull(AttributeValueInput),required=True,description="List of attributes specific to this variant.",)diff --git a/saleor/graphql/product/tests/benchmark/test_variant.py b/saleor/graphql/product/tests/benchmark/test_variant.pyindex c8d79954c3..ba4670e507 100644--- a/saleor/graphql/product/tests/benchmark/test_variant.py+++ b/saleor/graphql/product/tests/benchmark/test_variant.py@@ -184,7 +184,7 @@ def test_product_variant_create($productId: ID!,$sku: String,$stocks: [StockInput!],- $attributes: [AttributeValueInput]!,+ $attributes: [AttributeValueInput!]!,$weight: WeightScalar,$trackInventory: Boolean) {@@ -287,7 +287,7 @@ def test_update_product_variant(query = """mutation VariantUpdate($id: ID!- $attributes: [AttributeValueInput]+ $attributes: [AttributeValueInput!]$sku: String$trackInventory: Boolean!) {diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex d31b3c5d26..0edf34802a 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -4473,7 +4473,7 @@ def test_create_product_invalid_product_attributes("taxCode": product_tax_rate,"attributes": [{"id": color_attr_id, "values": [" "]},- {"id": weight_attr_id, "values": [None]},+ {"id": weight_attr_id, "values": [" "]},{"id": size_attr_id,"values": [non_existent_attr_value, color_value_slug],diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.pyindex 028dbd600e..b6db6a94dd 100644--- a/saleor/graphql/product/tests/test_variant.py+++ b/saleor/graphql/product/tests/test_variant.py@@ -326,7 +326,7 @@ CREATE_VARIANT_MUTATION = """$productId: ID!,$sku: String,$stocks: [StockInput!],- $attributes: [AttributeValueInput]!,+ $attributes: [AttributeValueInput!]!,$weight: WeightScalar,$trackInventory: Boolean) {productVariantCreate(@@ -1199,7 +1199,7 @@ def test_create_variant_invalid_variant_attributes("weight": weight,"attributes": [{"id": color_attr_id, "values": [" "]},- {"id": weight_attr_id, "values": [None]},+ {"id": weight_attr_id, "values": [" "]},{"id": size_attr_id, "values": [non_existent_attr_value, size_value_slug]},{"id": rich_text_attr_id, "richText": json.dumps(dummy_editorjs(" "))},],@@ -1298,7 +1298,7 @@ def test_product_variant_update_with_new_attributes(query = """mutation VariantUpdate($id: ID!- $attributes: [AttributeValueInput]+ $attributes: [AttributeValueInput!]$sku: String$trackInventory: Boolean!) {@@ -1376,7 +1376,7 @@ def test_update_product_variant($id: ID!,$sku: String!,$trackInventory: Boolean!,- $attributes: [AttributeValueInput]) {+ $attributes: [AttributeValueInput!]) {productVariantUpdate(id: $id,input: {@@ -1472,7 +1472,7 @@ QUERY_UPDATE_VARIANT_ATTRIBUTES = """mutation updateVariant ($id: ID!,$sku: String,- $attributes: [AttributeValueInput]!) {+ $attributes: [AttributeValueInput!]!) {productVariantUpdate(id: $id,input: {@@ -1634,6 +1634,50 @@ def test_update_variant_with_rich_text_attribute(product_variant_updated.assert_called_once_with(product.variants.last())+@patch("saleor.plugins.manager.PluginsManager.product_variant_updated")+def test_update_variant_with_numeric_attribute(+ product_variant_updated,+ permission_manage_products,+ product,+ product_type,+ staff_api_client,+ numeric_attribute,+ warehouse,+):+ product_type.variant_attributes.add(numeric_attribute)+ query = QUERY_UPDATE_VARIANT_ATTRIBUTES+ variant = product.variants.first()+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)+ sku = "123"+ attr_id = graphene.Node.to_global_id("Attribute", numeric_attribute.id)+ attribute_value = numeric_attribute.values.first()+ variables = {+ "id": variant_id,+ "sku": sku,+ "attributes": [+ {"id": attr_id, "values": []},+ ],+ }+ attribute_value.slug = f"{variant.id}_{numeric_attribute.id}"+ attribute_value.save()+ values_count = numeric_attribute.values.count()+ associate_attribute_values_to_instance(variant, numeric_attribute, attribute_value)++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)["data"]["productVariantUpdate"]+ variant.refresh_from_db()+ data = content["productVariant"]++ assert not content["errors"]+ assert data["sku"] == sku+ assert data["attributes"][-1]["attribute"]["slug"] == numeric_attribute.slug+ assert not data["attributes"][-1]["values"]+ assert numeric_attribute.values.count() == values_count+ product_variant_updated.assert_called_once_with(product.variants.last())++def test_update_product_variant_with_new_attribute(staff_api_client,product_with_variant_with_two_attributes,@@ -2121,7 +2165,6 @@ def test_update_product_variant_change_attribute_values_ordering(([], "Attribute expects a value but none were given.", "REQUIRED"),(["one", "two"], "Attribute must take only one value.", "INVALID"),([" "], "Attribute values cannot be blank.", "REQUIRED"),- ([None], "Attribute values cannot be blank.", "REQUIRED"),),)def test_update_product_variant_requires_values(@@ -2175,7 +2218,7 @@ def test_update_product_variant_with_price_does_not_raise_price_validation_errorstaff_api_client, variant, size_attribute, permission_manage_products):mutation = """- mutation updateVariant ($id: ID!, $attributes: [AttributeValueInput]) {+ mutation updateVariant ($id: ID!, $attributes: [AttributeValueInput!]) {productVariantUpdate(id: $id,input: {diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 650d86dc63..68d382e0c7 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -628,7 +628,7 @@ type AttributeValueDelete {input AttributeValueInput {id: ID- values: [String]+ values: [String!]file: StringcontentType: Stringreferences: [ID!]@@ -669,7 +669,7 @@ type AttributeValueUpdate {input BulkAttributeValueInput {id: ID- values: [String]!+ values: [String!]!}type BulkProductError {@@ -4542,7 +4542,7 @@ type ProductVariantBulkCreate {}input ProductVariantBulkCreateInput {- attributes: [BulkAttributeValueInput]!+ attributes: [BulkAttributeValueInput!]!sku: String!trackInventory: Booleanweight: WeightScalar@@ -4594,7 +4594,7 @@ type ProductVariantCreate {}input ProductVariantCreateInput {- attributes: [AttributeValueInput]!+ attributes: [AttributeValueInput!]!sku: StringtrackInventory: Booleanweight: WeightScalar@@ -4615,7 +4615,7 @@ input ProductVariantFilterInput {}input ProductVariantInput {- attributes: [AttributeValueInput]+ attributes: [AttributeValueInput!]sku: StringtrackInventory: Booleanweight: WeightScalar