@@ -11,7 +11,7 @@ class Endpoint
11
11
'byte' => %w( string byte ) ,
12
12
'date' => %w( string date ) ,
13
13
'dateTime' => %w( string date-time )
14
- }
14
+ } . freeze
15
15
16
16
def content_types_for ( target_class )
17
17
content_types = ( target_class . content_types || { } ) . values
@@ -103,7 +103,7 @@ def path_item(routes, options)
103
103
path . gsub! ( /:(\w +)/ , '{\1}' )
104
104
105
105
# set item from path, this could be used for the definitions object
106
- @item = path . gsub ( / \/ \ { (.+?)\} / , '' ) . split ( '/' ) . last . singularize . underscore . camelize || 'Item'
106
+ @item = path . gsub ( %r{/ {(.+?)}} , '' ) . split ( '/' ) . last . singularize . underscore . camelize || 'Item'
107
107
@entity = route . route_entity || route . route_success
108
108
109
109
# ... replacing version params through submitted version
@@ -129,8 +129,7 @@ def method_object(route, options)
129
129
methods [ :description ] = description_object ( route , options [ :markdown ] )
130
130
methods [ :headers ] = route . route_headers if route . route_headers
131
131
132
- mime_types = options [ :format ] ? Grape ::ContentTypes ::CONTENT_TYPES [ options [ :format ] ] : Grape ::ContentTypes ::CONTENT_TYPES [ :json ]
133
- methods [ :produces ] = [ mime_types ]
132
+ methods [ :produces ] = produces_object ( route , options )
134
133
135
134
methods [ :parameters ] = params_object ( route )
136
135
methods [ :responses ] = response_object ( route )
@@ -150,6 +149,17 @@ def description_object(route, markdown)
150
149
description
151
150
end
152
151
152
+ def produces_object ( route , options )
153
+ mime_types = GrapeSwagger ::DocMethods ::Produces . call ( options [ :format ] )
154
+
155
+ route_mime_types = [ :route_formats , :route_content_types , :route_produces ] . map do |producer |
156
+ possible = route . send ( producer )
157
+ GrapeSwagger ::DocMethods ::Produces . call ( possible ) if possible . present?
158
+ end . flatten . compact . uniq
159
+
160
+ route_mime_types . present? ? route_mime_types : mime_types
161
+ end
162
+
153
163
def response_object ( route )
154
164
default_code = default_staus_codes [ route . route_method . downcase . to_sym ]
155
165
default_code [ :model ] = @entity if @entity
@@ -165,16 +175,15 @@ def response_object(route)
165
175
response_model = @item
166
176
response_model = expose_params_from_model ( value [ :model ] ) if value [ :model ]
167
177
178
+ next unless !response_model . start_with? ( 'Swagger_doc' ) &&
179
+ ( ( @definitions [ response_model ] && value [ :code ] . to_s . start_with? ( '2' ) ) || value [ :model ] )
180
+
168
181
# TODO: proof that the definition exist, if model isn't specified
169
- if !response_model . start_with? ( 'Swagger_doc' ) &&
170
- ( ( !!@definitions [ response_model ] && value [ :code ] . to_s . start_with? ( '2' ) ) ||
171
- value [ :model ] )
172
- if route . route_is_array
173
- memo [ value [ :code ] ] [ :schema ] = { 'type' => 'array' , 'items' => { '$ref' => "#/definitions/#{ response_model } " } }
174
- else
175
- memo [ value [ :code ] ] [ :schema ] = { '$ref' => "#/definitions/#{ response_model } " }
176
- end
177
- end
182
+ memo [ value [ :code ] ] [ :schema ] = if route . route_is_array
183
+ { 'type' => 'array' , 'items' => { '$ref' => "#/definitions/#{ response_model } " } }
184
+ else
185
+ { '$ref' => "#/definitions/#{ response_model } " }
186
+ end
178
187
end
179
188
end
180
189
@@ -190,7 +199,7 @@ def default_staus_codes
190
199
191
200
def params_object ( route )
192
201
partition_params ( route ) . map do |param , value |
193
- parse_params ( param , value , route . route_path , route . route_method )
202
+ parse_params ( param , { required : false } . merge ( value ) , route . route_path , route . route_method )
194
203
end
195
204
end
196
205
@@ -230,7 +239,7 @@ def parse_request_params(parameters, required, route_paramter)
230
239
end
231
240
232
241
def parse_response_params ( params )
233
- return if params . empty ?
242
+ return if params . nil ?
234
243
235
244
params . each_with_object ( { } ) do |x , memo |
236
245
x [ 0 ] = x . last [ :as ] if x . last [ :as ]
@@ -247,8 +256,15 @@ def parse_response_params(params)
247
256
def expose_params_from_model ( model )
248
257
model_name = model . name . demodulize . camelize
249
258
250
- # has to be adept, to be ready for grape-entity >0.5.0
251
- parameters = model . exposures ? model . exposures : model . documentation
259
+ # DONE: has to be adept, to be ready for grape-entity >0.5.0
260
+ # TODO: this should only be a temporary hack ;)
261
+ if GrapeEntity ::VERSION =~ /0\. 4\. \d /
262
+ parameters = model . exposures ? model . exposures : model . documentation
263
+ elsif GrapeEntity ::VERSION =~ /0\. 5\. \d /
264
+ parameters = model . root_exposures . each_with_object ( { } ) do |value , memo |
265
+ memo [ value . attribute ] = value . send ( :options )
266
+ end
267
+ end
252
268
properties = parse_response_params ( parameters )
253
269
254
270
@definitions [ model_name ] = { type : 'object' , properties : properties }
@@ -272,7 +288,7 @@ def hidden?(route)
272
288
end
273
289
274
290
def parse_params ( param , value , path , method )
275
- items = { }
291
+ @array_items = { }
276
292
277
293
additional_documentation = value . is_a? ( Hash ) ? value [ :documentation ] : nil
278
294
data_type = data_type ( value )
@@ -282,7 +298,7 @@ def parse_params(param, value, path, method)
282
298
end
283
299
284
300
description = value . is_a? ( Hash ) ? value [ :desc ] || value [ :description ] : nil
285
- required = value . is_a? ( Hash ) ? !! value [ :required ] : false
301
+ required = value . is_a? ( Hash ) ? value [ :required ] : false
286
302
default_value = value . is_a? ( Hash ) ? value [ :default ] : nil
287
303
example = value . is_a? ( Hash ) ? value [ :example ] : nil
288
304
is_array = value . is_a? ( Hash ) ? ( value [ :is_array ] || false ) : false
@@ -291,6 +307,7 @@ def parse_params(param, value, path, method)
291
307
enum_or_range_values = parse_enum_or_range_values ( values )
292
308
293
309
value_type = { value : value , data_type : data_type , path : path }
310
+
294
311
parsed_params = {
295
312
in : param_type ( value_type , param , method , is_array ) ,
296
313
name : name ,
@@ -304,12 +321,10 @@ def parse_params(param, value, path, method)
304
321
parsed_params [ :type ] , parsed_params [ :format ] = PRIMITIVE_MAPPINGS [ data_type ]
305
322
end
306
323
307
- parsed_params [ :items ] = items if items . present?
324
+ parsed_params [ :items ] = @array_items if @array_items . present?
308
325
309
326
parsed_params [ :defaultValue ] = example if example
310
- if default_value && example . blank?
311
- parsed_params [ :defaultValue ] = default_value
312
- end
327
+ parsed_params [ :defaultValue ] = default_value if default_value && example . blank?
313
328
314
329
parsed_params . merge! ( enum_or_range_values ) if enum_or_range_values
315
330
parsed_params
@@ -353,8 +368,9 @@ def param_type(value_type, param, method, is_array)
353
368
value_type [ :value ] [ :documentation ] . key? ( :param_type )
354
369
355
370
if is_array
356
- items = { '$ref' => value_type [ :data_type ] }
357
- data_type = 'array'
371
+ @array_items = { 'type' => value_type [ :data_type ] }
372
+
373
+ 'array'
358
374
end
359
375
else
360
376
case
0 commit comments