8000 + list field · core-api/python-openapi-codec@203f91a · GitHub
[go: up one dir, main page]

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

Commit 203f91a

Browse files
author
zw
committed
+ list field
1 parent 107c4d4 commit 203f91a

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ site/
88
__pycache__
99
.cache
1010
.coverage
11+
.vscode
12+
build

openapi_codec/encode.py

+48Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ def generate_swagger_object(document):
99
Generates root of the Swagger spec.
1010
"""
1111
parsed_url = urlparse.urlparse(document.url)
12-
12+
1313
swagger = OrderedDict()
14-
14+
1515
swagger['swagger'] = '2.0'
1616
swagger['info'] = OrderedDict()
1717
swagger['info']['title'] = document.title
1818
swagger['info']['description'] = document.description
1919
swagger['info']['version'] = '' # Required by the spec
20-
20+
2121
if parsed_url.netloc:
2222
swagger['host'] = parsed_url.netloc
2323
if parsed_url.scheme:
2424
swagger['schemes'] = [parsed_url.scheme]
25-
25+
2626
swagger['paths'] = _get_paths_object(document)
27-
27+
2828
return swagger
2929

3030

@@ -49,45 +49,45 @@ def _get_links(document):
4949
operation_id = keys[0]
5050
tags = []
5151
links.append((operation_id, link, tags))
52-
52+
5353
# Determine if the operation ids each have unique names or not.
5454
operation_ids = [item[0] for item in links]
5555
unique = len(set(operation_ids)) == len(links)
56-
56+
5757
# If the operation ids are not unique, then prefix them with the tag.
5858
if not unique:
5959
return [_add_tag_prefix(item) for item in links]
60-
60+
6161
return links
6262

6363

6464
def _get_paths_object(document):
6565
paths = OrderedDict()
66-
66+
6767
links = _get_links(document)
68-
68+
6969
for operation_id, link, tags in links:
7070
if link.url not in paths:
7171
paths[link.url] = OrderedDict()
72-
72+
7373
method = get_method(link)
7474
operation = _get_operation(operation_id, link, tags)
7575
paths[link.url].update({method: operation})
76-
76+
7777
return paths
7878

7979

8080
def _get_operation(operation_id, link, tags):
8181
encoding = get_encoding(link)
8282
description = link.description.strip()
8383
summary = description.splitlines()[0] if description else None
84-
84+
8585
operation = {
8686
'operationId': operation_id,
8787
'responses': _get_responses(link),
8888
'parameters': _get_parameters(link, encoding)
8989
}
90-
90+
9191
if description:
9292
operation['description'] = description
9393
if summary:
@@ -103,21 +103,21 @@ def _get_field_description(field):
103103
if getattr(field, 'description', None) is not None:
104104
# Deprecated
105105
return field.description
106-
106+
107107
if field.schema is None:
108108
return ''
109-
109+
110110
return field.schema.description
111111

112112

113113
def _get_field_type(field):
114114
if getattr(field, 'type', None) is not None:
115115
# Deprecated
116116
return field.type
117-
117+
118118
if field.schema is None:
119119
return 'string'
120-
120+
121121
return {
122122
coreschema.String: 'string',
123123
coreschema.Integer: 'integer',
@@ -135,11 +135,12 @@ def _get_parameters(link, encoding):
135135
parameters = []
136136
properties = {}
137137
required = []
138-
138+
139139
for field in link.fields:
140140
location = get_location(link, field)
141141
field_description = _get_field_description(field)
142142
field_type = _get_field_type(field)
143+
143144
if location == 'form':
144145
if encoding in ('multipart/form-data', 'application/x-www-form-urlencoded'):
145146
# 'formData' in swagger MUST be one of these media types.
@@ -156,24 +157,47 @@ def _get_parameters(link, encoding):
156157
else:
157158
# Expand coreapi fields with location='form' into a single swagger
158159
# parameter, with a schema containing multiple properties.
159-
160+
160161
schema_property = {
161162
'description': field_description,
162163
'type': field_type,
163164
}
164165
if field_type == 'array':
165166
schema_property['items'] = {'type': 'string'}
166-
167+
167168
# ----------------- zw start ---------------------
168169
if hasattr(field, "schema") and hasattr(field.schema, "properties"):
169-
schema_property['properties'] = {k:vars(v) for k,v in dict(field.schema.properties).items()}
170-
for k,v in schema_property['properties'].items():
170+
schema_property['properties'] = {k: vars(v) for k, v in dict(field.schema.properties).items()}
171+
for k, v in schema_property['properties'].items():
171172
pro = {}
172173
for kk, vv in v.items():
173174
if vv:
174175
pro[kk] = vv
175176
pro['type'] = "string"
176177
schema_property['properties'][k] = pro
178+
if hasattr(field, "schema") and hasattr(field.schema, "items") and hasattr(field.schema.items,
179+
"properties"):
180+
schema_property = {
181+
'type': 'array',
182+
'items': {
183+
'type': 'object',
184+
'properties': {}
185+
}}
186+
schema_property['items']['properties'] = {k: vars(v) for k, v in dict(field.schema.items.properties).items()}
187+
for k, v in schema_property['items']['properties'].items():
188+
pro = {}
189+
pro['type'] = "string"
190+
for kk, vv in v.items():
191+
if vv:
192+
pro[kk] = vv
193+
if kk == 'items':
194+
vv = {
195+
'type': 'array',
196+
'items': {
197+
'type': 'string',
198+
}}
199+
pro = vv
200+
schema_property['items']['properties'][k] = pro
177201
# ----------------- zw end ---------------------
178202

179203
properties[field.name] = schema_property
@@ -204,7 +228,7 @@ def _get_parameters(link, encoding):
204228
if field_type == 'array':
205229
parameter['items'] = {'type': 'string'}
206230
parameters.append(parameter)
207-
231+
208232
if properties:
209233
parameter = {
210234
'name': 'data',

0 commit comments

Comments
 (0)
0