8000 Add better handling of Arrays and Objects · winsmith/python-openapi-codec@5ae44cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ae44cc

Browse files
author
Daniel Jilg
committed
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.
1 parent 1c1c680 commit 5ae44cc

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

openapi_codec/encode.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ def _get_field_type(field):
128128
}.get(field.schema.__class__, 'string')
129129

130130

131+
def _get_property_type(the_schema):
132+
return {
133+
coreschema.String: 'string',
134+
coreschema.Integer: 'integer',
135+
coreschema.Number: 'number',
136+
coreschema.Boolean: 'boolean',
137+
coreschema.Array: 'array',
138+
coreschema.Object: 'object',
139+
}.get(the_schema.__class__, 'string')
140+
141+
142+
def _field_properties_for_fields(field__schema_properties):
143+
field_properties = {}
144+
for k in field__schema_properties:
145+
field_properties[k] = {
146+
'type': _get_property_type(field__schema_properties[k])
147+
}
148+
return field_properties
149+
150+
131151
def _get_parameters(link, encoding):
132152
"""
133153
Generates Swagger Parameter Item object.
@@ -161,8 +181,28 @@ def _get_parameters(link, encoding):
161181
'description': field_description,
162182
'type': field_type,
163183
}
184+
if field_type == 'object':
185+
field_properties = _field_properties_for_fields(field.schema.properties)
186+
schema_property = {
187+
'description': field_description,
188+
'type': field_type,
189+
'properties': field_properties
190+
}
164191
if field_type == 'array':
165-
schema_property['items'] = {'type': 'string'}
192+
if hasattr(field.schema.items, 'properties'):
193+
# the array contains object instances, we need to inspect their properties
194+
field_properties = _field_properties_for_fields(
195+
field.schema.items.properties
196+
)
197+
schema_property['items'] = {
198+
'type': 'object',
199+
'properties': field_properties
200+
}
201+
else:
202+
# the array contains primitive types
203+
schema_property['items'] = {
204+
'type': _get_property_type(field.schema.items),
205+
}
166206
properties[field.name] = schema_property
167207
if field.required:
168208
required.append(field.name)

0 commit comments

Comments
 (0)
0