From a4f37d1c1b307db80d19f398694cd352093a58a1 Mon Sep 17 00:00:00 2001 From: Dorian Hoxha Date: Sun, 1 Oct 2023 16:34:17 +0200 Subject: [PATCH 1/2] Set the `format` of `marshmallow.fields.Float` fields to `float` --- src/apispec/ext/marshmallow/field_converter.py | 2 +- tests/test_ext_marshmallow_field.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/apispec/ext/marshmallow/field_converter.py b/src/apispec/ext/marshmallow/field_converter.py index 2bf8a9de..f1d24d70 100644 --- a/src/apispec/ext/marshmallow/field_converter.py +++ b/src/apispec/ext/marshmallow/field_converter.py @@ -22,7 +22,7 @@ DEFAULT_FIELD_MAPPING: dict[type, tuple[str | None, str | None]] = { marshmallow.fields.Integer: ("integer", None), marshmallow.fields.Number: ("number", None), - marshmallow.fields.Float: ("number", None), + marshmallow.fields.Float: ("number", "float"), marshmallow.fields.Decimal: ("number", None), marshmallow.fields.String: ("string", None), marshmallow.fields.Boolean: ("boolean", None), diff --git a/tests/test_ext_marshmallow_field.py b/tests/test_ext_marshmallow_field.py index 0fb04b69..a7505663 100644 --- a/tests/test_ext_marshmallow_field.py +++ b/tests/test_ext_marshmallow_field.py @@ -66,6 +66,7 @@ def test_formatted_field_translates_to_array(ListClass, spec_fixture): (fields.Date, "date"), (fields.Email, "email"), (fields.URL, "url"), + (fields.Float, "float"), ], ) def test_field2property_formats(FieldClass, expected_format, spec_fixture): From 41572dff54874b8ba4305242d1d6a08d1afc1fde Mon Sep 17 00:00:00 2001 From: Dorian Hoxha Date: Sun, 1 Oct 2023 16:37:21 +0200 Subject: [PATCH 2/2] Test `float` format is propagated to openapi output --- tests/test_ext_marshmallow_openapi.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_ext_marshmallow_openapi.py b/tests/test_ext_marshmallow_openapi.py index ed7fb0ef..e00ba7fa 100644 --- a/tests/test_ext_marshmallow_openapi.py +++ b/tests/test_ext_marshmallow_openapi.py @@ -56,6 +56,7 @@ class UserSchema(Schema): _id = fields.Int() email = fields.Email(metadata={"description": "email address of the user"}) name = fields.Str() + my_float = fields.Float() class Meta: title = "User" @@ -65,6 +66,7 @@ class Meta: assert res["type"] == "object" props = res["properties"] assert props["_id"]["type"] == "integer" + assert props["my_float"]["format"] == "float" assert props["email"]["type"] == "string" assert props["email"]["format"] == "email" assert props["email"]["description"] == "email address of the user"