Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.
Merged
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
2 changes: 1 addition & 1 deletion openapi_codec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from openapi_codec.decode import _parse_document


__version__ = '1.2.1'
__version__ = '1.3.0'


class OpenAPICodec(BaseCodec):
Expand Down
25 changes: 22 additions & 3 deletions openapi_codec/decode.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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))
Expand Down
40 changes: 27 additions & 13 deletions openapi_codec/encode.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -107,31 +122,30 @@ 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.
parameter = {
'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:
Expand All @@ -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)
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions tests/test_encode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import coreapi
import coreschema
from openapi_codec.encode import generate_swagger_object, _get_parameters
from unittest import TestCase

Expand Down Expand Up @@ -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='')

Expand All @@ -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)
45 changes: 29 additions & 16 deletions tests/test_mappings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from openapi_codec import OpenAPICodec
import coreapi
import coreschema


codec = OpenAPICodec()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
)
]
)
Expand All @@ -86,7 +88,8 @@ def test_mapping():
coreapi.Field(
name='id',
location='path',
required=True
required=True,
schema=coreschema.String()
)
]
)
Expand All @@ -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()
)
]
)
Expand All @@ -117,8 +121,9 @@ def test_mapping():
coreapi.Field(
name='example',
location='body',
description='example field'
schema=coreschema.String()
)

]
)

Expand All @@ -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()
)
]
)
Expand All @@ -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()
)
]
)
Expand All @@ -164,7 +173,8 @@ def test_mapping():
coreapi.Field(
name='example',
location='body',
required=True
required=True,
schema=coreschema.String()
)
]
)
Expand All @@ -178,7 +188,8 @@ def test_mapping():
fields=[
coreapi.Field(
name='example',
location='body'
location='body',
schema=coreschema.String()
)
]
)
Expand All @@ -190,7 +201,9 @@ def test_mapping():
fields=[
coreapi.Field(
name='example',
location='body'
location='body',

schema=coreschema.String()
)
]
)