8000 Fix the encoding/decoding of Enum types. · core-api/python-client@dbec97a · 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 dbec97a

Browse files
tbeadlejriggins
authored andcommitted
Fix the encoding/decoding of Enum types.
The 'enum' argument is a required parameter to creating an Enum. Store it in an 'extra' dict that can be passed as **kwargs during its creation.
1 parent 131f94b commit dbec97a

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

coreapi/codecs/corejson.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,23 @@
3333

3434
def encode_schema_to_corejson(schema):
3535
type_id = SCHEMA_CLASS_TO_TYPE_ID.get(schema.__class__, 'anything')
36-
return {
36+
retval = {
3737
'_type': type_id,
3838
'title': schema.title,
3939
'description': schema.description
4040
}
41+
if isinstance(schema, coreschema.Enum):
42+
retval['extra'] = {'enum': schema.enum}
43+
return retval
4144

4245

4346
def decode_schema_from_corejson(data):
4447
type_id = _get_string(data, '_type')
4548
title = _get_string(data, 'title')
4649
description = _get_string(data, 'description')
50+
extra = _get_dict(data, 'extra')
4751
schema_cls = TYPE_ID_TO_SCHEMA_CLASS.get(type_id, coreschema.Anything)
48-
return schema_cls(title=title, description=description)
52+
return schema_cls(title=title, description=description, **extra)
4953

5054

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

tests/test_codecs.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from coreapi.document import Document, Link, Error, Field
55
from coreapi.exceptions import ParseError, NoCodecAvailable
66
from coreapi.utils import negotiate_decoder, negotiate_encoder
7+
from coreschema import Enum, String
78
import pytest
89

910

@@ -21,7 +22,13 @@ def doc():
2122
'integer': 123,
2223
'dict': {'key': 'value'},
2324
'list': [1, 2, 3],
24-
'link': Link(url='http://example.org/', fields=[Field(name='example')]),
25+
'link': Link(
26+
url='http://example.org/',
27+
fields=[
28+
Field(name='noschema'),
29+
Field(name='string_example', schema=String()),
30+
Field(name='enum_example', schema=Enum(['a', 'b', 'c'])),
31+
]),
2532
'nested': {'child': Link(url='http://example.org/123')},
2633
'_type': 'needs escaping'
2734
})
@@ -40,7 +47,26 @@ def test_document_to_primitive(doc):
4047
'integer': 123,
4148
'dict': {'key': 'value'},
4249
'list': [1, 2, 3],
43-
'link': {'_type': 'link', 'fields': [{'name': 'example'}]},
50+
'link': {'_type': 'link', 'fields': [
51+
{'name': 'noschema'},
52+
{
53+
'name': 'string_example',
54+
'schema': {
55+
'_type': 'string',
56+
'title': '',
57+
'description': '',
58+
},
59+
},
60+
{
61+
'name': 'enum_example',
62+
'schema': {
63+
'_type': 'enum',
64+
'title': '',
65+
'description': '',
66+
'extra': {'enum': ['a', 'b', 'c']},
67+
},
68+
},
69+
]},
4470
'nested': {'child': {'_type': 'link', 'url': '/123'}},
4571
'__type': 'needs escaping'
4672
}
@@ -56,7 +82,30 @@ def test_primitive_to_document(doc):
5682
'integer': 123,
5783
'dict': {'key': 'value'},
5884
'list': [1, 2, 3],
59-
'link': {'_type': 'link', 'url': 'http://example.org/', 'fields': [{'name': 'example'}]},
85+
'link': {
86+
'_type': 'link',
87+
'url': 'http://example.org/',
88+
'fields': [
89+
{'name': 'noschema'},
90+
{
91+
'name': 'string_example',
92+
'schema': {
93+
'_type': 'string',
94+
'title': '',
95+
'description': '',
96+
},
97+
},
98+
{
99+
'name': 'enum_example',
100+
'schema': {
101+
'_type': 'enum',
102+
'title': '',
103+
'description': '',
104+
'extra': {'enum': ['a', 'b', 'c']},
105+
},
106+
},
107+
],
108+
},
60109
'nested': {'child': {'_type': 'link', 'url': 'http://example.org/123'}},
61110
'__type': 'needs escaping'
62111
}

0 commit comments

Comments
 (0)
0