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

Commit

Permalink
Fix the encoding/decoding of Enum types.
Browse files Browse the repository at this point in the history
This is an update from tbeadle/enum branch with changes that tomchriste
suggested.
  • Loading branch information
jriggins committed May 23, 2017
1 parent dbec97a commit 3aaf883
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions coreapi/codecs/corejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ def encode_schema_to_corejson(schema):
'description': schema.description
}
if isinstance(schema, coreschema.Enum):
retval['extra'] = {'enum': schema.enum}
retval['enum'] = schema.enum
return retval


def decode_schema_from_corejson(data):
type_id = _get_string(data, '_type')
title = _get_string(data, 'title')
description = _get_string(data, 'description')
extra = _get_dict(data, 'extra')

extra = {}
if type_id == 'enum':
extra['enum'] = _get_list(data, 'enum')

schema_cls = TYPE_ID_TO_SCHEMA_CLASS.get(type_id, coreschema.Anything)
return schema_cls(title=title, description=description, **extra)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_document_to_primitive(doc):
'_type': 'enum',
'title': '',
'description': '',
'extra': {'enum': ['a', 'b', 'c']},
'enum': ['a', 'b', 'c'],
},
},
]},
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_primitive_to_document(doc):
'_type': 'enum',
'title': '',
'description': '',
'extra': {'enum': ['a', 'b', 'c']},
'enum': ['a', 'b', 'c'],
},
},
],
Expand Down

0 comments on commit 3aaf883

Please sign in to comment.