diff --git a/AUTHORS.rst b/AUTHORS.rst index 309c1916..e10eb08f 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -64,3 +64,4 @@ Contributors (chronological) - Fedor Fominykh `@fedorfo `_ - Colin Bounouar `@Colin-b `_ - David Bishop `@teancom `_ +- Andrea Ghensi `@sanzoghenzo `_ diff --git a/src/apispec/ext/marshmallow/field_converter.py b/src/apispec/ext/marshmallow/field_converter.py index 8130ef0b..07a72559 100644 --- a/src/apispec/ext/marshmallow/field_converter.py +++ b/src/apispec/ext/marshmallow/field_converter.py @@ -397,6 +397,7 @@ def metadata2properties(self, field, **kwargs): metadata = { key.replace("_", "-") if key.startswith("x_") else key: value for key, value in field.metadata.items() + if isinstance(key, str) } # Avoid validation error with "Additional properties not allowed" diff --git a/tests/test_ext_marshmallow_field.py b/tests/test_ext_marshmallow_field.py index 701da30d..274f0d9f 100644 --- a/tests/test_ext_marshmallow_field.py +++ b/tests/test_ext_marshmallow_field.py @@ -293,3 +293,13 @@ def custom_string2properties(self, field, **kwargs): assert properties["x-customString"] == ( spec_fixture.openapi.openapi_version == "2.0" ) + + +def test_field2property_with_non_string_metadata_keys(spec_fixture): + class _DesertSentinel: + pass + + field = fields.Boolean(description="A description") + field.metadata[_DesertSentinel()] = "to be ignored" + result = spec_fixture.openapi.field2property(field) + assert result == {"description": "A description", "type": "boolean"}