Skip to content

Commit

Permalink
Ignore field metadata with non-string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Ghensi authored and lafrech committed Aug 29, 2020
1 parent f075149 commit e4704b8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -64,3 +64,4 @@ Contributors (chronological)
- Fedor Fominykh `@fedorfo <https://github.com/fedorfo>`_
- Colin Bounouar `@Colin-b <https://github.com/Colin-b>`_
- David Bishop `@teancom <https://github.com/teancom>`_
- Andrea Ghensi `@sanzoghenzo <https://github.com/sanzoghenzo>`_
1 change: 1 addition & 0 deletions src/apispec/ext/marshmallow/field_converter.py
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions tests/test_ext_marshmallow_field.py
Expand Up @@ -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"}

0 comments on commit e4704b8

Please sign in to comment.