8000 This is a patch for openapi_codec.encode._get_parameters. · core-api/python-openapi-codec@6823898 · GitHub
[go: up one dir, main page]

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

Commit 6823898

Browse files
committed
This is a patch for openapi_codec.encode._get_parameters.
Per the swagger docs (https://swagger.io/docs/specification/adding-examples/): schema: # Request body contents type: object properties: id: type: integer name: type: string example: # <<< _get_parameters was not doing this! id: 10 name: Jessica Smith
1 parent baa97e0 commit 6823898

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

openapi_codec/encode.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,15 @@ def _get_parameters(link, encoding):
134134
"""
135135
parameters = []
136136
properties = {}
137+
examples = {}
137138
required = []
138139

139140
for field in link.fields:
140-
location = get_location(link, field)
141-
field_description = _get_field_description(field)
142-
field_type = _get_field_type(field)
141+
location = openapi_codec.encode.get_location(link, field)
142+
field_description = openapi_codec.encode._get_field_description(field)
143+
field_type = openapi_codec.encode._get_field_type(field)
144+
field_example = getattr(field, 'example', None)
145+
143146
if location == 'form':
144147
if encoding in ('multipart/form-data', 'application/x-www-form-urlencoded'):
145148
# 'formData' in swagger MUST be one of these media types.
@@ -164,6 +167,10 @@ def _get_parameters(link, encoding):
164167
if field_type == 'array':
165168
schema_property['items'] = {'type': 'string'}
166169
properties[field.name] = schema_property
170+
171+
if field_example is not None:
172+
examples[field.name] = field_example
173+
167174
if field.required:
168175
required.append(field.name)
169176
elif location == 'body':
@@ -193,13 +200,16 @@ def _get_parameters(link, encoding):
193200
parameters.append(parameter)
194201

195202
if properties:
203+
schema = {
204+
'type': 'object',
205+
'properties': properties
206+
}
207+
if examples:
208+
schema['example'] = examples
196209
parameter = {
197210
'name': 'data',
198211
'in': 'body',
199-
'schema': {
200-
'type': 'object',
201-
'properties': properties
202-
}
212+
'schema': schema
203213
}
204214
if required:
205215
parameter['schema']['required'] = required

0 commit comments

Comments
 (0)
0