Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions coreapi/codecs/corejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@

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['extra'] = {'enum': schema.enum}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could just put this value directly under enum rather than having an extra dictionary.

Thanks for progressing this!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured that using 'extra' would allow it to be used for other types in the future.

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')
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, **extra)


# 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_primative(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': '',
'extra': {'enum': ['a', 'b', 'c']},
},
},
]},
'nested': {'child': {'_type': 'link', 'url': '/123'}},
'__type': 'needs escaping'
}
Expand All @@ -56,7 +82,30 @@ def test_primative_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': '',
'extra': {'enum': ['a', 'b', 'c']},
},
},
],
},
'nested': {'child': {'_type': 'link', 'url': 'http://example.org/123'}},
'__type': 'needs escaping'
}
Expand Down