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

Commit 8273a9f

Browse files
authored
Merge pull request #1 from grofers/nested-object-array-encoder
Nested object array encoder
2 parents baa97e0 + 3e55317 commit 8273a9f

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

openapi_codec/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from openapi_codec.decode import _parse_document
99

1010

11-
__version__ = '1.3.2'
11+
__version__ = '1.3.3'
1212

1313

1414
class OpenAPICodec(BaseCodec):

openapi_codec/encode.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,43 @@ def _get_field_type(field):
127127
coreschema.Object: 'object',
128128
}.get(field.schema.__class__, 'string')
129129

130+
def _get_schema_field_type(property):
131+
return {
132+
coreschema.String: 'string',
133+
coreschema.Integer: 'integer',
134+
coreschema.Number: 'number',
135+
coreschema.Boolean: 'boolean',
136+
coreschema.Array: 'array',
137+
coreschema.Object: 'object',
138+
}.get(property.__class__, 'string')
139+
140+
def _get_nested_object_fields(field):
141+
try:
142+
field_properties = field.schema.properties
143+
except AttributeError:
144+
field_properties = field.properties
145+
146+
properties = {}
147+
for key, property in field_properties.items():
148+
field_description = _get_field_description(property)
149+
field_type = _get_schema_field_type(property)
150+
properties[key] = {
151+
'type': field_type,
152+
'description': field_description
153+
}
154+
if field_type == 'object':
155+
properties[key]['properties'] = _get_nested_object_fields(property)
156+
if field_type == 'array':
157+
array_type = _get_schema_field_type(property.items)
158+
array_properties = {
159+
'type': array_type,
160+
}
161+
if array_type == 'object':
162+
array_properties['properties'] = _get_nested_object_fields(property.items)
163+
properties[key]['items'] = array_properties
164+
return properties
165+
166+
130167

131168
def _get_parameters(link, encoding):
132169
"""
@@ -156,13 +193,20 @@ def _get_parameters(link, encoding):
156193
else:
157194
# Expand coreapi fields with location='form' into a single swagger
158195
# parameter, with a schema containing multiple properties.
159-
160196
schema_property = {
161197
'description': field_description,
162198
'type': field_type,
163199
}
200+
if field_type == 'object':
201+
schema_property['properties'] = _get_nested_object_fields(field)
164202
if field_type == 'array':
165-
schema_property['items'] = {'type': 'string'}
203+
array_type = _get_schema_field_type(field.schema.items)
204+
array_properties = {
205+
'type': array_type,
206+
}
207+
if array_type == 'object':
208+
array_properties['properties'] = _get_nested_object_fields(field.schema.items)
209+
schema_property['items'] = array_properties
166210
properties[field.name] = schema_property
167211
if field.required:
168212
required.append(field.name)

0 commit comments

Comments
 (0)