diff --git a/openapi_codec/__init__.py b/openapi_codec/__init__.py index 49a1c9f..e645c21 100644 --- a/openapi_codec/__init__.py +++ b/openapi_codec/__init__.py @@ -8,7 +8,7 @@ from openapi_codec.decode import _parse_document -__version__ = '1.2.1' +__version__ = '1.3.0' class OpenAPICodec(BaseCodec): diff --git a/openapi_codec/decode.py b/openapi_codec/decode.py index 747263b..679ddb8 100644 --- a/openapi_codec/decode.py +++ b/openapi_codec/decode.py @@ -1,6 +1,7 @@ from coreapi import Document, Link, Field from coreapi.compat import string_types, urlparse from coreapi.exceptions import ParseError +import coreschema def _parse_document(data, base_url=None): @@ -37,22 +38,40 @@ def _parse_document(data, base_url=None): schema = _get_dict(parameter, 'schema', dereference_using=data) expanded = _expand_schema(schema) if expanded is not None: + # TODO: field schemas. expanded_fields = [ - Field(name=field_name, location='form', required=is_required, description=field_description) + Field( + name=field_name, + location='form', + required=is_required, + schema=coreschema.String(description=field_description) + ) for field_name, is_required, field_description in expanded if not any([field.name == field_name for field in fields]) ] fields += expanded_fields else: + # TODO: field schemas. field_description = _get_string(parameter, 'description') - field = Field(name=name, location='body', required=required, description=field_description) + field = Field( + name=name, + location='body', + required=required, + schema=coreschema.String(description=field_description) + ) fields.append(field) else: if location == 'formData': has_form = True location = 'form' field_description = _get_string(parameter, 'description') - field = Field(name=name, location=location, required=required, description=field_description) + # TODO: field schemas. + field = Field( + name=name, + location=location, + required=required, + schema=coreschema.String(description=field_description) + ) fields.append(field) link_consumes = get_strings(_get_list(operation, 'consumes', consumes)) diff --git a/openapi_codec/encode.py b/openapi_codec/encode.py index 981f83f..e455130 100644 --- a/openapi_codec/encode.py +++ b/openapi_codec/encode.py @@ -1,3 +1,4 @@ +import coreschema from collections import OrderedDict from coreapi.compat import urlparse from openapi_codec.utils import get_method, get_encoding, get_location, get_links_from_document @@ -97,6 +98,20 @@ def _get_operation(operation_id, link, tags): return operation +def _get_schema_type(schema): + if schema is None: + return 'string' + + return { + coreschema.String: 'string', + coreschema.Integer: 'integer', + coreschema.Number: 'number', + coreschema.Boolean: 'boolean', + coreschema.Array: 'array', + coreschema.Object: 'object', + }.get(schema.__class__, 'string') + + def _get_parameters(link, encoding): """ Generates Swagger Parameter Item object. @@ -107,6 +122,8 @@ def _get_parameters(link, encoding): for field in link.fields: location = get_location(link, field) + field_description = field.schema.description if field.schema else '' + field_type = _get_schema_type(field.schema) if location == 'form': if encoding in ('multipart/form-data', 'application/x-www-form-urlencoded'): # 'formData' in swagger MUST be one of these media types. @@ -114,24 +131,21 @@ def _get_parameters(link, encoding): 'name': field.name, 'required': field.required, 'in': 'formData', - 'description': field.description, - 'type': field.type or 'string', + 'description': field_description, + 'type': field_type, } - if field.type == 'array': + if field_type == 'array': parameter['items'] = {'type': 'string'} parameters.append(parameter) else: # Expand coreapi fields with location='form' into a single swagger # parameter, with a schema containing multiple properties. - use_type = field.type or 'string' - if use_type == 'file': - use_type = 'string' schema_property = { - 'description': field.description, - 'type': use_type, + 'description': field_description, + 'type': field_type, } - if field.type == 'array': + if field_type == 'array': schema_property['items'] = {'type': 'string'} properties[field.name] = schema_property if field.required: @@ -146,7 +160,7 @@ def _get_parameters(link, encoding): 'name': field.name, 'required': field.required, 'in': location, - 'description': field.description, + 'description': field_description, 'schema': schema } parameters.append(parameter) @@ -155,10 +169,10 @@ def _get_parameters(link, encoding): 'name': field.name, 'required': field.required, 'in': location, - 'description': field.description, - 'type': field.type or 'string', + 'description': field_description, + 'type': field_type or 'string', } - if field.type == 'array': + if field_type == 'array': parameter['items'] = {'type': 'string'} parameters.append(parameter) diff --git a/setup.py b/setup.py index 85cf22d..b411216 100755 --- a/setup.py +++ b/setup.py @@ -61,7 +61,7 @@ def get_package_data(package): author_email='tom@tomchristie.com', packages=get_packages('openapi_codec'), package_data=get_package_data('openapi_codec'), - install_requires=['coreapi>=2.1.0'], + install_requires=['coreapi>=2.2.0'], classifiers=[ 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', diff --git a/tests/test_encode.py b/tests/test_encode.py index 8bd7a72..0ffd883 100644 --- a/tests/test_encode.py +++ b/tests/test_encode.py @@ -1,4 +1,5 @@ import coreapi +import coreschema from openapi_codec.encode import generate_swagger_object, _get_parameters from unittest import TestCase @@ -86,7 +87,7 @@ def setUp(self): name='email', required=True, location='query', - description='A valid email address.' + schema=coreschema.String(description='A valid email address.') ) self.swagger = _get_parameters(coreapi.Link(fields=[self.field]), encoding='') @@ -96,7 +97,7 @@ def test_expected_fields(self): 'name': self.field.name, 'required': self.field.required, 'in': 'query', - 'description': self.field.description, + 'description': self.field.schema.description, 'type': 'string' # Everything is a string for now. } self.assertEquals(self.swagger[0], expected) diff --git a/tests/test_mappings.py b/tests/test_mappings.py index 5b0e58f..87d23c2 100644 --- a/tests/test_mappings.py +++ b/tests/test_mappings.py @@ -1,5 +1,6 @@ from openapi_codec import OpenAPICodec import coreapi +import coreschema codec = OpenAPICodec() @@ -10,15 +11,15 @@ 'simple_link': coreapi.Link('/simple_link/', description='example link'), 'location': { 'query': coreapi.Link('/location/query/', fields=[ - coreapi.Field(name='a', description='example field', required=True), + coreapi.Field(name='a', required=True, schema=coreschema.String(description='example field')), coreapi.Field(name='b') ]), 'form': coreapi.Link('/location/form/', action='post', fields=[ - coreapi.Field(name='a', description='example field', required=True), + coreapi.Field(name='a', required=True, schema=coreschema.String(description='example field')), coreapi.Field(name='b'), ]), 'body': coreapi.Link('/location/body/', action='post', fields=[ - coreapi.Field(name='example', location='body', description='example field') + coreapi.Field(name='example', location='body') ]), 'path': coreapi.Link('/location/path/{id}/', fields=[ coreapi.Field(name='id', location='path', required=True) @@ -69,12 +70,13 @@ def test_mapping(): coreapi.Field( name='a', location='query', - description='example field', + schema=coreschema.String(description='example field'), required=True ), coreapi.Field( name='b', - location='query' + location='query', + schema=coreschema.String() ) ] ) @@ -86,7 +88,8 @@ def test_mapping(): coreapi.Field( name='id', location='path', - required=True + required=True, + schema=coreschema.String() ) ] ) @@ -100,11 +103,12 @@ def test_mapping(): name='a', location='form', required=True, - description='example field' + schema=coreschema.String(description='example field') ), coreapi.Field( name='b', - location='form' + location='form', + schema=coreschema.String() ) ] ) @@ -117,8 +121,9 @@ def test_mapping(): coreapi.Field( name='example', location='body', - description='example field' + schema=coreschema.String() ) + ] ) @@ -130,11 +135,13 @@ def test_mapping(): coreapi.Field( name='a', location='form', - required=True + required=True, + schema=coreschema.String() ), coreapi.Field( name='b', - location='form' + location='form', + schema=coreschema.String() ) ] ) @@ -147,11 +154,13 @@ def test_mapping(): coreapi.Field( name='a', location='form', - required=True + required=True, + schema=coreschema.String() ), coreapi.Field( name='b', - location='form' + location='form', + schema=coreschema.String() ) ] ) @@ -164,7 +173,8 @@ def test_mapping(): coreapi.Field( name='example', location='body', - required=True + required=True, + schema=coreschema.String() ) ] ) @@ -178,7 +188,8 @@ def test_mapping(): fields=[ coreapi.Field( name='example', - location='body' + location='body', + schema=coreschema.String() ) ] ) @@ -190,7 +201,9 @@ def test_mapping(): fields=[ coreapi.Field( name='example', - location='body' + location='body', + + schema=coreschema.String() ) ] )