Skip to content

Commit 4d3a72a

Browse files
committed
Properly handle empty enums
1 parent 566d037 commit 4d3a72a

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Current
99
- Fix missing changelog inprevious release
1010
- Ensure definitions with both `$ref` and description (or other property) output is valid (using `allOf`)
1111
- Added initial specifications schemas and validation support
12+
- Ensure empty enums are not serialized (to have a valid specification)
1213

1314
0.12.0 (2018-09-27)
1415
-------------------

flask_restplus/fields.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ def format(self, value):
379379
def schema(self):
380380
enum = self._v('enum')
381381
schema = super(String, self).schema()
382-
schema.update(enum=enum)
382+
if enum:
383+
schema.update(enum=enum)
383384
if enum and schema['example'] is None:
384385
schema['example'] = enum[0]
385386
return schema

tests/test_fields.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,23 @@ def test_with_enum(self):
195195
assert not field.required
196196
assert field.__schema__ == {'type': 'string', 'enum': enum, 'example': enum[0]}
197197

198+
def test_with_empty_enum(self):
199+
field = fields.String(enum=[])
200+
assert not field.required
201+
assert field.__schema__ == {'type': 'string'}
202+
198203
def test_with_callable_enum(self):
199204
enum = lambda: ['A', 'B', 'C'] # noqa
200205
field = fields.String(enum=enum)
201206
assert not field.required
202207
assert field.__schema__ == {'type': 'string', 'enum': ['A', 'B', 'C'], 'example': 'A'}
203208

209+
def test_with_empty_callable_enum(self):
210+
enum = lambda: [] # noqa
211+
field = fields.String(enum=enum)
212+
assert not field.required
213+
assert field.__schema__ == {'type': 'string'}
214+
204215
def test_with_default(self):
205216
field = fields.String(default='aaa')
206217
assert field.__schema__ == {'type': 'string', 'default': 'aaa'}

0 commit comments

Comments
 (0)