1+ import coreschema
12from collections import OrderedDict
23from coreapi .compat import urlparse
34from openapi_codec .utils import get_method , get_encoding , get_location , get_links_from_document
@@ -97,6 +98,20 @@ def _get_operation(operation_id, link, tags):
9798 return operation
9899
99100
101+ def _get_schema_type (schema ):
102+ if schema is None :
103+ return 'string'
104+
105+ return {
106+ coreschema .String : 'string' ,
107+ coreschema .Integer : 'integer' ,
108+ coreschema .Number : 'number' ,
109+ coreschema .Boolean : 'boolean' ,
110+ coreschema .Array : 'array' ,
111+ coreschema .Object : 'object' ,
112+ }.get (schema .__class__ , 'string' )
113+
114+
100115def _get_parameters (link , encoding ):
101116 """
102117 Generates Swagger Parameter Item object.
@@ -107,31 +122,30 @@ def _get_parameters(link, encoding):
107122
108123 for field in link .fields :
109124 location = get_location (link , field )
125+ field_description = field .schema .description if field .schema else ''
126+ field_type = _get_schema_type (field .schema )
110127 if location == 'form' :
111128 if encoding in ('multipart/form-data' , 'application/x-www-form-urlencoded' ):
112129 # 'formData' in swagger MUST be one of these media types.
113130 parameter = {
114131 'name' : field .name ,
115132 'required' : field .required ,
116133 'in' : 'formData' ,
117- 'description' : field . description ,
118- 'type' : field . type or 'string' ,
134+ 'description' : field_description ,
135+ 'type' : field_type ,
119136 }
120- if field . type == 'array' :
137+ if field_type == 'array' :
121138 parameter ['items' ] = {'type' : 'string' }
122139 parameters .append (parameter )
123140 else :
124141 # Expand coreapi fields with location='form' into a single swagger
125142 # parameter, with a schema containing multiple properties.
126- use_type = field .type or 'string'
127- if use_type == 'file' :
128- use_type = 'string'
129143
130144 schema_property = {
131- 'description' : field . description ,
132- 'type' : use_type ,
145+ 'description' : field_description ,
146+ 'type' : field_type ,
133147 }
134- if field . type == 'array' :
148+ if field_type == 'array' :
135149 schema_property ['items' ] = {'type' : 'string' }
136150 properties [field .name ] = schema_property
137151 if field .required :
@@ -146,7 +160,7 @@ def _get_parameters(link, encoding):
146160 'name' : field .name ,
147161 'required' : field .required ,
148162 'in' : location ,
149- 'description' : field . description ,
163+ 'description' : field_description ,
150164 'schema' : schema
151165 }
152166 parameters .append (parameter )
@@ -155,10 +169,10 @@ def _get_parameters(link, encoding):
155169 'name' : field .name ,
156170 'required' : field .required ,
157171 'in' : location ,
158- 'description' : field . description ,
159- 'type' : field . type or 'string' ,
172+ 'description' : field_description ,
173+ 'type' : field_type or 'string' ,
160174 }
161- if field . type == 'array' :
175+ if field_type == 'array' :
162176 parameter ['items' ] = {'type' : 'string' }
163177 parameters .append (parameter )
164178
0 commit comments