Skip to content
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.3.2'
__version__ = '1.3.3'


class OpenAPICodec(BaseCodec):
Expand Down
48 changes: 46 additions & 2 deletions openapi_codec/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,43 @@ def _get_field_type(field):
coreschema.Object: 'object',
}.get(field.schema.__class__, 'string')

def _get_schema_field_type(property):
return {
coreschema.String: 'string',
coreschema.Integer: 'integer',
coreschema.Number: 'number',
coreschema.Boolean: 'boolean',
coreschema.Array: 'array',
coreschema.Object: 'object',
}.get(property.__class__, 'string')

def _get_nested_object_fields(field):
try:
field_properties = field.schema.properties
except AttributeError:
field_properties = field.properties

properties = {}
for key, property in field_properties.items():
field_description = _get_field_description(property)
field_type = _get_schema_field_type(property)
properties[key] = {
'type': field_type,
'description': field_description
}
if field_type == 'object':
properties[key]['properties'] = _get_nested_object_fields(property)
if field_type == 'array':
array_type = _get_schema_field_type(property.items)
array_properties = {
'type': array_type,
}
if array_type == 'object':
array_properties['properties'] = _get_nested_object_fields(property.items)
properties[key]['items'] = array_properties
return properties



def _get_parameters(link, encoding):
"""
Expand Down Expand Up @@ -156,13 +193,20 @@ def _get_parameters(link, encoding):
else:
# Expand coreapi fields with location='form' into a single swagger
# parameter, with a schema containing multiple properties.

schema_property = {
'description': field_description,
'type': field_type,
}
if field_type == 'object':
schema_property['properties'] = _get_nested_object_fields(field)
if field_type == 'array':
schema_property['items'] = {'type': 'string'}
array_type = _get_schema_field_type(field.schema.items)
array_properties = {
'type': array_type,
}
if array_type == 'object':
array_properties['properties'] = _get_nested_object_fields(field.schema.items)
schema_property['items'] = array_properties
properties[field.name] = schema_property
if field.required:
required.append(field.name)
Expand Down