@@ -9,22 +9,22 @@ def generate_swagger_object(document):
9
9
Generates root of the Swagger spec.
10
10
"""
11
11
parsed_url = urlparse .urlparse (document .url )
12
-
12
+
13
13
swagger = OrderedDict ()
14
-
14
+
15
15
swagger ['swagger' ] = '2.0'
16
16
swagger ['info' ] = OrderedDict ()
17
17
swagger ['info' ]['title' ] = document .title
18
18
swagger ['info' ]['description' ] = document .description
19
19
swagger ['info' ]['version' ] = '' # Required by the spec
20
-
20
+
21
21
if parsed_url .netloc :
22
22
swagger ['host' ] = parsed_url .netloc
23
23
if parsed_url .scheme :
24
24
swagger ['schemes' ] = [parsed_url .scheme ]
25
-
25
+
26
26
swagger ['paths' ] = _get_paths_object (document )
27
-
27
+
28
28
return swagger
29
29
30
30
@@ -49,45 +49,45 @@ def _get_links(document):
49
49
operation_id = keys [0 ]
50
50
tags = []
51
51
links .append ((operation_id , link , tags ))
52
-
52
+
53
53
# Determine if the operation ids each have unique names or not.
54
54
operation_ids = [item [0 ] for item in links ]
55
55
unique = len (set (operation_ids )) == len (links )
56
-
56
+
57
57
# If the operation ids are not unique, then prefix them with the tag.
58
58
if not unique :
59
59
return [_add_tag_prefix (item ) for item in links ]
60
-
60
+
61
61
return links
62
62
63
63
64
64
def _get_paths_object (document ):
65
65
paths = OrderedDict ()
66
-
66
+
67
67
links = _get_links (document )
68
-
68
+
69
69
for operation_id , link , tags in links :
70
70
if link .url not in paths :
71
71
paths [link .url ] = OrderedDict ()
72
-
72
+
73
73
method = get_method (link )
74
74
operation = _get_operation (operation_id , link , tags )
75
75
paths [link .url ].update ({method : operation })
76
-
76
+
77
77
return paths
78
78
79
79
80
80
def _get_operation (operation_id , link , tags ):
81
81
encoding = get_encoding (link )
82
82
description = link .description .strip ()
83
83
summary = description .splitlines ()[0 ] if description else None
84
-
84
+
85
85
operation = {
86
86
'operationId' : operation_id ,
87
87
'responses' : _get_responses (link ),
88
88
'parameters' : _get_parameters (link , encoding )
89
89
}
90
-
90
+
91
91
if description :
92
92
operation ['description' ] = description
93
93
if summary :
@@ -103,21 +103,21 @@ def _get_field_description(field):
103
103
if getattr (field , 'description' , None ) is not None :
104
104
# Deprecated
105
105
return field .description
106
-
106
+
107
107
if field .schema is None :
108
108
return ''
109
-
109
+
110
110
return field .schema .description
111
111
112
112
113
113
def _get_field_type (field ):
114
114
if getattr (field , 'type' , None ) is not None :
115
115
# Deprecated
116
116
return field .type
117
-
117
+
118
118
if field .schema is None :
119
119
return 'string'
120
-
120
+
121
121
return {
122
122
coreschema .String : 'string' ,
123
123
coreschema .Integer : 'integer' ,
@@ -135,11 +135,12 @@ def _get_parameters(link, encoding):
135
135
parameters = []
136
136
properties = {}
137
137
required = []
138
-
138
+
139
139
for field in link .fields :
140
140
location = get_location (link , field )
141
141
field_description = _get_field_description (field )
142
142
field_type = _get_field_type (field )
143
+
143
144
if location == 'form' :
144
145
if encoding in ('multipart/form-data' , 'application/x-www-form-urlencoded' ):
145
146
# 'formData' in swagger MUST be one of these media types.
@@ -156,24 +157,47 @@ def _get_parameters(link, encoding):
156
157
else :
157
158
# Expand coreapi fields with location='form' into a single swagger
158
159
# parameter, with a schema containing multiple properties.
159
-
160
+
160
161
schema_property = {
161
162
'description' : field_description ,
162
163
'type' : field_type ,
163
164
}
164
165
if field_type == 'array' :
165
166
schema_property ['items' ] = {'type' : 'string' }
166
-
167
+
167
168
# ----------------- zw start ---------------------
168
169
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 ():
171
172
pro = {}
172
173
for kk , vv in v .items ():
173
174
if vv :
174
175
pro [kk ] = vv
175
176
pro ['type' ] = "string"
176
177
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
177
201
# ----------------- zw end ---------------------
178
202
179
203
properties [field .name ] = schema_property
@@ -204,7 +228,7 @@ def _get_parameters(link, encoding):
204
228
if field_type == 'array' :
205
229
parameter ['items' ] = {'type' : 'string' }
206
230
parameters .append (parameter )
207
-
231
+
208
232
if properties :
209
233
parameter = {
210
234
'name' : 'data' ,
0 commit comments