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

Commit 5ae907e

Browse files
authored
Merge pull request #135 from jriggins/enum
Fix the encoding/decoding of Enum types.
2 parents 131f94b + 13fa37f commit 5ae907e

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

coreapi/codecs/corejson.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,27 @@
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['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+
51+
kwargs = {}
52+
if type_id == 'enum':
53+
kwargs['enum'] = _get_list(data, 'enum')
54+
4755
schema_cls = TYPE_ID_TO_SCHEMA_CLASS.get(type_id, coreschema.Anything)
48-
return schema_cls(title=title, description=description)
56+
return schema_cls(title=title, description=description, **kwargs)
4957

5058

5159
# 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+
'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+
'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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy