From dbec97a44b332c4f8e40019a4c96a06dcf11d7b8 Mon Sep 17 00:00:00 2001 From: Tommy Beadle Date: Wed, 5 Apr 2017 13:06:11 -0400 Subject: [PATCH 1/2] 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. --- coreapi/codecs/corejson.py | 8 ++++-- tests/test_codecs.py | 55 +++++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/coreapi/codecs/corejson.py b/coreapi/codecs/corejson.py index 091e7a3..37d312c 100644 --- a/coreapi/codecs/corejson.py +++ b/coreapi/codecs/corejson.py @@ -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} + 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 diff --git a/tests/test_codecs.py b/tests/test_codecs.py index 8ebbd63..7529aa9 100644 --- a/tests/test_codecs.py +++ b/tests/test_codecs.py @@ -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 @@ -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' }) @@ -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': '', + 'extra': {'enum': ['a', 'b', 'c']}, + }, + }, + ]}, 'nested': {'child': {'_type': 'link', 'url': '/123'}}, '__type': 'needs escaping' } @@ -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': '', + 'extra': {'enum': ['a', 'b', 'c']}, + }, + }, + ], + }, 'nested': {'child': {'_type': 'link', 'url': 'http://example.org/123'}}, '__type': 'needs escaping' } From 13fa37f58d08c87028a4a87149b15b5e833582f1 Mon Sep 17 00:00:00 2001 From: Jesse Riggins Date: Tue, 23 May 2017 10:46:52 -0700 Subject: [PATCH 2/2] Fix the encoding/decoding of Enum types. This is an update from tbeadle/enum branch with changes that tomchriste suggested. --- coreapi/codecs/corejson.py | 10 +++++++--- tests/test_codecs.py | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/coreapi/codecs/corejson.py b/coreapi/codecs/corejson.py index 37d312c..fd88a58 100644 --- a/coreapi/codecs/corejson.py +++ b/coreapi/codecs/corejson.py @@ -39,7 +39,7 @@ def encode_schema_to_corejson(schema): 'description': schema.description } if isinstance(schema, coreschema.Enum): - retval['extra'] = {'enum': schema.enum} + retval['enum'] = schema.enum return retval @@ -47,9 +47,13 @@ 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') + + 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, **extra) + return schema_cls(title=title, description=description, **kwargs) # Robust dictionary lookups, that always return an item of the correct diff --git a/tests/test_codecs.py b/tests/test_codecs.py index 7529aa9..32458cd 100644 --- a/tests/test_codecs.py +++ b/tests/test_codecs.py @@ -63,7 +63,7 @@ def test_document_to_primitive(doc): '_type': 'enum', 'title': '', 'description': '', - 'extra': {'enum': ['a', 'b', 'c']}, + 'enum': ['a', 'b', 'c'], }, }, ]}, @@ -101,7 +101,7 @@ def test_primitive_to_document(doc): '_type': 'enum', 'title': '', 'description': '', - 'extra': {'enum': ['a', 'b', 'c']}, + 'enum': ['a', 'b', 'c'], }, }, ],