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

Support encoding & decoding format #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions openapi_codec/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def _parse_document(data, base_url=None):
name=field_name,
location='form',
required=is_required,
schema=coreschema.String(description=field_description)
schema=coreschema.String(description=field_description, format=field_format)
)
for field_name, is_required, field_description in expanded
for field_name, is_required, field_description, field_format in expanded
if not any([field.name == field_name for field in fields])
]
fields += expanded_fields
Expand Down Expand Up @@ -188,7 +188,7 @@ def _expand_schema(schema):
schema_required = _get_list(schema, 'required')
if ((schema_type == ['object']) or (schema_type == 'object')) and schema_properties:
return [
(key, key in schema_required, schema_properties[key].get('description'))
(key, key in schema_required, schema_properties[key].get('description'), schema_properties[key].get('format'))
for key in schema_properties.keys()
]
return None
Expand Down
11 changes: 11 additions & 0 deletions openapi_codec/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ def _get_field_type(field):
}.get(field.schema.__class__, 'string')


def _get_field_format(field):
return field.schema and getattr(field.schema, 'format', None)


def _get_parameters(link, encoding):
"""
Generates Swagger Parameter Item object.
Expand All @@ -140,6 +144,7 @@ def _get_parameters(link, encoding):
location = get_location(link, field)
field_description = _get_field_description(field)
field_type = _get_field_type(field)
field_format = _get_field_format(field)
if location == 'form':
if encoding in ('multipart/form-data', 'application/x-www-form-urlencoded'):
# 'formData' in swagger MUST be one of these media types.
Expand All @@ -152,6 +157,8 @@ def _get_parameters(link, encoding):
}
if field_type == 'array':
parameter['items'] = {'type': 'string'}
if field_format:
parameter['format'] = field_format
parameters.append(parameter)
else:
# Expand coreapi fields with location='form' into a single swagger
Expand Down Expand Up @@ -179,6 +186,8 @@ def _get_parameters(link, encoding):
'description': field_description,
'schema': schema
}
if field_format:
parameter['format'] = field_format
parameters.append(parameter)
else:
parameter = {
Expand All @@ -190,6 +199,8 @@ def _get_parameters(link, encoding):
}
if field_type == 'array':
parameter['items'] = {'type': 'string'}
if field_format:
parameter['format'] = field_format
parameters.append(parameter)

if properties:
Expand Down
45 changes: 45 additions & 0 deletions tests/test_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,48 @@ def test_expected_fields(self):
'type': 'string' # Everything is a string for now.
}
self.assertEquals(self.swagger[0], expected)


class TestFormat(TestCase):
def setUp(self):
self.field = coreapi.Field(
name='published_before',
required=True,
location='query',
schema=coreschema.String(description='Filter by published date.', format='date')
)
self.swagger = _get_parameters(coreapi.Link(fields=[self.field]), encoding='')

def test_expected_fields(self):
self.assertEquals(len(self.swagger), 1)
expected = {
'name': self.field.name,
'required': self.field.required,
'in': 'query',
'description': self.field.schema.description,
'type': 'string',
'format': 'date'
}
self.assertEquals(self.swagger[0], expected)


class TestIntegerField(TestCase):
def setUp(self):
self.field = coreapi.Field(
name='page',
required=True,
location='query',
schema=coreschema.Integer(description='A page number.')
)
self.swagger = _get_parameters(coreapi.Link(fields=[self.field]), encoding='')

def test_expected_fields(self):
self.assertEquals(len(self.swagger), 1)
expected = {
'name': self.field.name,
'required': self.field.required,
'in': 'query',
'description': self.field.schema.description,
'type': 'integer'
}
self.assertEquals(self.swagger[0], expected)