8000 Fix the encoding/decoding of Enum types. · core-api/python-client@13fa37f · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 13fa37f

Browse files
committed
Fix the encoding/decoding of Enum types.
This is an update from tbeadle/enum branch with changes that tomchriste suggested.
1 parent dbec97a commit 13fa37f

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

coreapi/codecs/corejson.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,21 @@ def encode_schema_to_corejson(schema):
3939
'description': schema.description
4040
}
4141
if isinstance(schema, coreschema.Enum):
42-
retval['extra'] = {'enum': schema.enum}
42+
retval['enum'] = schema.enum
4343
return retval
4444

4545

4646
def decode_schema_from_corejson(data):
4747
type_id = _get_string(data, '_type')
4848
title = _get_string(data, 'title')
4949
description = _get_string(data, 'description')
50-
extra = _get_dict(data, 'extra')
50+
51+
kwargs = {}
52+
if type_id == 'enum':
53+
kwargs['enum'] = _get_list(data, 'enum')
54+
5155
schema_cls = TYPE_ID_TO_SCHEMA_CLASS.get(type_id, coreschema.Anything)
52-
return schema_cls(title=title, description=description, **extra)
56+
return schema_cls(title=title, description=description, **kwargs)
5357

5458

5559
# Robust dictionary lookups, that always return an item of the correct

tests/test_codecs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_document_to_primitive(doc):
6363
'_type': 'enum',
6464
'title': '',
6565
'description': '',
66-
'extra': {'enum': ['a', 'b', 'c']},
66+
'enum': ['a', 'b', 'c'],
6767
},
6868
},
6969
]},
@@ -101,7 +101,7 @@ def test_primitive_to_document(doc):
101101
'_type': 'enum',
102102
'title': '',
103103
'description': '',
104-
'extra': {'enum': ['a', 'b', 'c']},
104+
'enum': ['a', 'b', 'c'],
105105
},
106106
},
107107
],

0 commit comments

Comments
 (0)
0