From 3d61af85e9c9f823f8b01c5533a69374c71b9c99 Mon Sep 17 00:00:00 2001 From: Austin Snoeyink Date: Wed, 25 Aug 2021 10:06:43 -0400 Subject: [PATCH 1/2] allow Decimal type to return None for empty string --- graphene/types/decimal.py | 2 ++ graphene/types/tests/test_decimal.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/graphene/types/decimal.py b/graphene/types/decimal.py index b2acbe7e7..056d199a8 100644 --- a/graphene/types/decimal.py +++ b/graphene/types/decimal.py @@ -28,6 +28,8 @@ def parse_literal(cls, node): @staticmethod def parse_value(value): + if value == "": + return None try: return _Decimal(value) except ValueError: diff --git a/graphene/types/tests/test_decimal.py b/graphene/types/tests/test_decimal.py index 9757e82ca..24aec24a1 100644 --- a/graphene/types/tests/test_decimal.py +++ b/graphene/types/tests/test_decimal.py @@ -6,7 +6,7 @@ class Query(ObjectType): - decimal = Decimal(input=Decimal()) + decimal = Decimal(input=Decimal(required=False)) def resolve_decimal(self, info, input): return input @@ -49,3 +49,10 @@ def test_decimal_string_query_integer(): assert not result.errors assert result.data == {"decimal": str(decimal_value)} assert decimal.Decimal(result.data["decimal"]) == decimal_value + +def test_parse_decimal_empty_string(): + """Parsing an empty string should return None""" + result = schema.execute("""{ decimal(input: \"\") }""") + assert not result.errors + assert result.data == {"decimal": None} + From 9b0aa5d0cd38cb904c64f0c7295f0d0bffd19d8a Mon Sep 17 00:00:00 2001 From: Austin Snoeyink Date: Wed, 25 Aug 2021 10:28:35 -0400 Subject: [PATCH 2/2] remove unnecessary required=False from test --- graphene/types/tests/test_decimal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene/types/tests/test_decimal.py b/graphene/types/tests/test_decimal.py index 24aec24a1..5829db0fe 100644 --- a/graphene/types/tests/test_decimal.py +++ b/graphene/types/tests/test_decimal.py @@ -6,7 +6,7 @@ class Query(ObjectType): - decimal = Decimal(input=Decimal(required=False)) + decimal = Decimal(input=Decimal()) def resolve_decimal(self, info, input): return input