From 5ae44cc085087a25381439bddd657ffdae0c674b Mon Sep 17 00:00:00 2001 From: Daniel Jilg Date: Mon, 17 Jul 2017 11:50:03 +0200 Subject: [PATCH] Add better handling of Arrays and Objects The _get_parameter method now handles arrays and objects slighlty better. Before, it would simply output type `string` for arrays, whereas now it tries to inspect objects and arrays in order to output type information for them. This is not recursive, so it goes only one level deep. --- openapi_codec/encode.py | 42 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/openapi_codec/encode.py b/openapi_codec/encode.py index 13b3ef4..77a3f81 100644 --- a/openapi_codec/encode.py +++ b/openapi_codec/encode.py @@ -128,6 +128,26 @@ def _get_field_type(field): }.get(field.schema.__class__, 'string') +def _get_property_type(the_schema): + return { + coreschema.String: 'string', + coreschema.Integer: 'integer', + coreschema.Number: 'number', + coreschema.Boolean: 'boolean', + coreschema.Array: 'array', + coreschema.Object: 'object', + }.get(the_schema.__class__, 'string') + + +def _field_properties_for_fields(field__schema_properties): + field_properties = {} + for k in field__schema_properties: + field_properties[k] = { + 'type': _get_property_type(field__schema_properties[k]) + } + return field_properties + + def _get_parameters(link, encoding): """ Generates Swagger Parameter Item object. @@ -161,8 +181,28 @@ def _get_parameters(link, encoding): 'description': field_description, 'type': field_type, } + if field_type == 'object': + field_properties = _field_properties_for_fields(field.schema.properties) + schema_property = { + 'description': field_description, + 'type': field_type, + 'properties': field_properties + } if field_type == 'array': - schema_property['items'] = {'type': 'string'} + if hasattr(field.schema.items, 'properties'): + # the array contains object instances, we need to inspect their properties + field_properties = _field_properties_for_fields( + field.schema.items.properties + ) + schema_property['items'] = { + 'type': 'object', + 'properties': field_properties + } + else: + # the array contains primitive types + schema_property['items'] = { + 'type': _get_property_type(field.schema.items), + } properties[field.name] = schema_property if field.required: required.append(field.name)