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

Commit d463f96

Browse files
committed
Support schemas in corejson
1 parent c376e61 commit d463f96

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

coreapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from coreapi.document import Array, Document, Link, Object, Error, Field
55

66

7-
__version__ = '2.2.3'
7+
__version__ = '2.2.4'
88
__all__ = [
99
'Array', 'Document', 'Link', 'Object', 'Error', 'Field',
1010
'Client',

coreapi/codecs/corejson.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,60 @@
55
from coreapi.compat import COMPACT_SEPARATORS, VERBOSE_SEPARATORS
66
from coreapi.document import Document, Link, Array, Object, Error, Field
77
from coreapi.exceptions import ParseError
8+
import coreschema
89
import json
910

1011

12+
# Schema encoding and decoding.
13+
# Just a naive first-pass at this point.
14+
15+
SCHEMA_CLASS_TO_TYPE_ID = {
16+
coreschema.Object: 'object',
17+
coreschema.Array: 'array',
18+
coreschema.Number: 'number',
19+
coreschema.Integer: 'integer',
20+
coreschema.String: 'string',
21+
coreschema.Boolean: 'boolean',
22+
coreschema.Null: 'null',
23+
coreschema.Enum: 'enum',
24+
coreschema.Anything: 'anything'
25+
}
26+
27+
TYPE_ID_TO_SCHEMA_CLASS = {
28+
value: key
29+
for key, value
30+
in SCHEMA_CLASS_TO_TYPE_ID.items()
31+
}
32+
33+
34+
def encode_schema_to_corejson(schema):
35+
type_id = SCHEMA_CLASS_TO_TYPE_ID.get(schema.__class__, 'anything')
36+
return {
37+
'_type': type_id,
38+
'title': schema.title,
39+
'description': schema.description
40+
}
41+
42+
43+
def decode_schema_from_corejson(data):
44+
type_id = _get_string(data, '_type')
45+
title = _get_string(data, 'title')
46+
description = _get_string(data, 'description')
47+
schema_cls = TYPE_ID_TO_SCHEMA_CLASS.get(type_id, coreschema.Anything)
48+
return schema_cls(title=title, description=description)
49+
50+
1151
# Robust dictionary lookups, that always return an item of the correct
1252
# type, using an empty default if an incorrect type exists.
1353
# Useful for liberal parsing of inputs.
1454

55+
def _get_schema(item, key):
56+
schema_data = _get_dict(item, key)
57+
if schema_data:
58+
return decode_schema_from_corejson(schema_data)
59+
return None
60+
61+
1562
def _get_string(item, key):
1663
value = item.get(key)
1764
if isinstance(value, string_types):
@@ -156,6 +203,8 @@ def _document_to_primative(node, base_url=None):
156203
ret['required'] = node.required
157204
if node.location:
158205
ret['location'] = node.location
206+
if node.schema:
207+
ret['schema'] = encode_schema_to_corejson(node.schema)
159208
return ret
160209

161210
elif isinstance(node, Object):
@@ -213,7 +262,7 @@ def _primative_to_document(data, base_url=None):
213262
name=_get_string(item, 'name'),
214263
required=_get_bool(item, 'required'),
215264
location=_get_string(item, 'location'),
216-
schema=item.get('schema', None)
265+
schema=_get_schema(item, 'schema')
217266
)
218267
for item in fields if isinstance(item, dict)
219268
]

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