From 7cfd559749dd1f4b594e12fd8f9c06197d7f6b4a Mon Sep 17 00:00:00 2001 From: Benjamin Scharf Date: Wed, 26 May 2021 21:13:01 -0500 Subject: [PATCH 1/2] Fix for issue #240 + test. --- flask_restx/fields.py | 2 ++ tests/test_fields.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/flask_restx/fields.py b/flask_restx/fields.py index 031fdd61..ef357df6 100644 --- a/flask_restx/fields.py +++ b/flask_restx/fields.py @@ -481,6 +481,8 @@ class Float(NumberMixin, Raw): def format(self, value): try: + if value is None: + return self.default return float(value) except (ValueError, TypeError) as ve: raise MarshallingError(ve) diff --git a/tests/test_fields.py b/tests/test_fields.py index e54fb6a9..58b463c1 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -333,6 +333,12 @@ def test_with_default(self): assert not field.required assert field.__schema__ == {"type": "number", "default": 0.5} + def test_none_uses_default(self): + field = fields.float(default=0.5) + assert not field.required + assert field.__schema__ == {"type": "number", "default": 0.5} + assert field.format(None) == 0.5 + @pytest.mark.parametrize( "value,expected", [("-3.13", -3.13), (str(-3.13), -3.13), (3, 3.0),] ) From aa3492d680058c2100ba89e81bb9d1261d35fe55 Mon Sep 17 00:00:00 2001 From: Benjamin Scharf Date: Thu, 27 May 2021 10:27:11 -0500 Subject: [PATCH 2/2] Capitalization fix. --- tests/test_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_fields.py b/tests/test_fields.py index 58b463c1..09b7e271 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -334,7 +334,7 @@ def test_with_default(self): assert field.__schema__ == {"type": "number", "default": 0.5} def test_none_uses_default(self): - field = fields.float(default=0.5) + field = fields.Float(default=0.5) assert not field.required assert field.__schema__ == {"type": "number", "default": 0.5} assert field.format(None) == 0.5