Skip to content

Commit 549a05e

Browse files
authored
Merge pull request #802 from marshmallow-code/enum
Support marshmallow.fields.Enum
2 parents 2261de9 + bd0d12f commit 549a05e

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
INSTALL_REQUIRES = "packaging>=21.3"
55

66
EXTRAS_REQUIRE = {
7-
"marshmallow": ["marshmallow>=3.13.0"],
7+
"marshmallow": ["marshmallow>=3.18.0"],
88
"yaml": ["PyYAML>=3.10"],
99
"validation": ["prance[osv]>=0.11", "openapi_spec_validator<0.5"],
1010
"lint": [

src/apispec/ext/marshmallow/field_converter.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def init_attribute_functions(self):
105105
self.field2length,
106106
self.field2pattern,
107107
self.metadata2properties,
108+
self.enum2properties,
108109
self.nested2properties,
109110
self.pluck2properties,
110111
self.list2properties,
@@ -501,6 +502,18 @@ def timedelta2properties(self, field, **kwargs: typing.Any) -> dict:
501502
ret["x-unit"] = field.precision
502503
return ret
503504

505+
def enum2properties(self, field, **kwargs: typing.Any) -> dict:
506+
"""Return a dictionary of properties from :class:`Enum <marshmallow.fields.Enum` fields.
507+
508+
:param Field field: A marshmallow field.
509+
:rtype: dict
510+
"""
511+
ret = {}
512+
if isinstance(field, marshmallow.fields.Enum):
513+
ret = self.field2property(field.field)
514+
ret["enum"] = field.choices_text
515+
return ret
516+
504517

505518
def make_type_list(types):
506519
"""Return a list of types from a type attribute

tests/test_ext_marshmallow_field.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime as dt
22
import re
3+
from enum import Enum
34

45
import pytest
56
from marshmallow import fields, validate
@@ -263,6 +264,32 @@ def test_field_with_multiple_patterns(recwarn, spec_fixture):
263264
assert ret["pattern"] == "winner"
264265

265266

267+
def test_enum_symbol_field(spec_fixture):
268+
class MyEnum(Enum):
269+
one = 1
270+
two = 2
271+
272+
field = fields.Enum(MyEnum)
273+
ret = spec_fixture.openapi.field2property(field)
274+
assert ret["type"] == "string"
275+
assert ret["enum"] == "one, two"
276+
277+
278+
@pytest.mark.parametrize("by_value", [fields.Integer, True])
279+
def test_enum_value_field(spec_fixture, by_value):
280+
class MyEnum(Enum):
281+
one = 1
282+
two = 2
283+
284+
field = fields.Enum(MyEnum, by_value=by_value)
285+
ret = spec_fixture.openapi.field2property(field)
286+
if by_value is True:
287+
assert "type" not in ret
288+
else:
289+
assert ret["type"] == "integer"
290+
assert ret["enum"] == "1, 2"
291+
292+
266293
def test_field2property_nested_spec_metadatas(spec_fixture):
267294
spec_fixture.spec.components.schema("Category", schema=CategorySchema)
268295
category = fields.Nested(

0 commit comments

Comments
 (0)