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

Commit

Permalink
Merge pull request #135 from jriggins/enum
Browse files Browse the repository at this point in the history
Fix the encoding/decoding of Enum types.
  • Loading branch information
tomchristie authored May 23, 2017
2 parents 131f94b + 13fa37f commit 5ae907e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
12 changes: 10 additions & 2 deletions coreapi/codecs/corejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,27 @@

def encode_schema_to_corejson(schema):
type_id = SCHEMA_CLASS_TO_TYPE_ID.get(schema.__class__, 'anything')
return {
retval = {
'_type': type_id,
'title': schema.title,
'description': schema.description
}
if isinstance(schema, coreschema.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')

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

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


# Robust dictionary lookups, that always return an item of the correct
Expand Down
55 changes: 52 additions & 3 deletions tests/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from coreapi.document import Document, Link, Error, Field
from coreapi.exceptions import ParseError, NoCodecAvailable
from coreapi.utils import negotiate_decoder, negotiate_encoder
from coreschema import Enum, String
import pytest


Expand All @@ -21,7 +22,13 @@ def doc():
'integer': 123,
'dict': {'key': 'value'},
'list': [1, 2, 3],
'link': Link(url='http://example.org/', fields=[Field(name='example')]),
'link': Link(
url='http://example.org/',
fields=[
Field(name='noschema'),
Field(name='string_example', schema=String()),
Field(name='enum_example', schema=Enum(['a', 'b', 'c'])),
]),
'nested': {'child': Link(url='http://example.org/123')},
'_type': 'needs escaping'
})
Expand All @@ -40,7 +47,26 @@ def test_document_to_primitive(doc):
'integer': 123,
'dict': {'key': 'value'},
'list': [1, 2, 3],
'link': {'_type': 'link', 'fields': [{'name': 'example'}]},
'link': {'_type': 'link', 'fields': [
{'name': 'noschema'},
{
'name': 'string_example',
'schema': {
'_type': 'string',
'title': '',
'description': '',
},
},
{
'name': 'enum_example',
'schema': {
'_type': 'enum',
'title': '',
'description': '',
'enum': ['a', 'b', 'c'],
},
},
]},
'nested': {'child': {'_type': 'link', 'url': '/123'}},
'__type': 'needs escaping'
}
Expand All @@ -56,7 +82,30 @@ def test_primitive_to_document(doc):
'integer': 123,
'dict': {'key': 'value'},
'list': [1, 2, 3],
'link': {'_type': 'link', 'url': 'http://example.org/', 'fields': [{'name': 'example'}]},
'link': {
'_type': 'link',
'url': 'http://example.org/',
'fields': [
{'name': 'noschema'},
{
'name': 'string_example',
'schema': {
'_type': 'string',
'title': '',
'description': '',
},
},
{
'name': 'enum_example',
'schema': {
'_type': 'enum',
'title': '',
'description': '',
'enum': ['a', 'b', 'c'],
},
},
],
},
'nested': {'child': {'_type': 'link', 'url': 'http://example.org/123'}},
'__type': 'needs escaping'
}
Expand Down

0 comments on commit 5ae907e

Please sign in to comment.