Skip to content

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

Fix product create when no product type provided

Michał Jeloneka0ded82bf02a
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 7d5d6d8c76..c539402f5c 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -351,7 +351,7 @@ class ProductCreate(ModelMutation):
See the documentation for `has_variants` field for details:
http://docs.getsaleor.com/en/latest/architecture/products.html#product-types
"""
- if not product_type.has_variants:
+ if product_type and not product_type.has_variants:
input_sku = cleaned_input.get('sku')
if not input_sku:
cls.add_error(errors, 'sku', 'This field cannot be blank.')
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 85a8bb8d3c..16e68f9df8 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -710,6 +710,29 @@ def test_create_product_without_variants_sku_duplication(
assert data['errors'][0]['message'] == 'Product with this SKU already exists.'
+def test_product_create_without_product_type(
+ staff_api_client, permission_manage_products):
+ query = """
+ mutation createProduct($name: String!) {
+ productCreate(input: {name: $name, productType: ""}) {
+ product {
+ id
+ }
+ errors {
+ message
+ field
+ }
+ }
+ }
+ """
+
+ response = staff_api_client.post_graphql(
+ query, {'name': 'Product'}, permissions=[permission_manage_products])
+ errors = get_graphql_content(response)['data']['productCreate']['errors']
+
+ assert errors[0]['field'] == 'productType'
+ assert errors[0]['message'] == 'This field cannot be null.'
+
def test_update_product(
staff_api_client, category, non_default_category, product,
permission_manage_products):