ruby-swagger 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby-swagger.rb +1 -5
  3. data/lib/ruby-swagger/data/contact.rb +3 -8
  4. data/lib/ruby-swagger/data/definitions.rb +5 -7
  5. data/lib/ruby-swagger/data/document.rb +22 -24
  6. data/lib/ruby-swagger/data/example.rb +3 -5
  7. data/lib/ruby-swagger/data/external_documentation.rb +4 -6
  8. data/lib/ruby-swagger/data/header.rb +2 -4
  9. data/lib/ruby-swagger/data/headers.rb +5 -8
  10. data/lib/ruby-swagger/data/info.rb +6 -8
  11. data/lib/ruby-swagger/data/items.rb +4 -6
  12. data/lib/ruby-swagger/data/license.rb +4 -6
  13. data/lib/ruby-swagger/data/mime.rb +0 -2
  14. data/lib/ruby-swagger/data/operation.rb +7 -9
  15. data/lib/ruby-swagger/data/parameter.rb +4 -10
  16. data/lib/ruby-swagger/data/parameters.rb +4 -6
  17. data/lib/ruby-swagger/data/path.rb +14 -18
  18. data/lib/ruby-swagger/data/paths.rb +5 -7
  19. data/lib/ruby-swagger/data/reference.rb +4 -8
  20. data/lib/ruby-swagger/data/response.rb +1 -3
  21. data/lib/ruby-swagger/data/responses.rb +1 -3
  22. data/lib/ruby-swagger/data/schema.rb +7 -9
  23. data/lib/ruby-swagger/data/scopes.rb +4 -6
  24. data/lib/ruby-swagger/data/security_definitions.rb +5 -7
  25. data/lib/ruby-swagger/data/security_requirement.rb +6 -8
  26. data/lib/ruby-swagger/data/security_scheme.rb +10 -12
  27. data/lib/ruby-swagger/data/tag.rb +3 -5
  28. data/lib/ruby-swagger/data/url.rb +1 -3
  29. data/lib/ruby-swagger/data/xml_object.rb +2 -4
  30. data/lib/ruby-swagger/grape/entity.rb +41 -0
  31. data/lib/ruby-swagger/grape/entity_exposure.rb +96 -0
  32. data/lib/ruby-swagger/grape/entity_nesting_exposure.rb +38 -0
  33. data/lib/ruby-swagger/grape/grape.rb +1 -1
  34. data/lib/ruby-swagger/grape/grape_config.rb +19 -22
  35. data/lib/ruby-swagger/grape/grape_presenter.rb +6 -9
  36. data/lib/ruby-swagger/grape/grape_template.rb +3 -5
  37. data/lib/ruby-swagger/grape/method.rb +79 -71
  38. data/lib/ruby-swagger/grape/param.rb +3 -3
  39. data/lib/ruby-swagger/grape/route_path.rb +1 -3
  40. data/lib/ruby-swagger/grape/routes.rb +6 -9
  41. data/lib/ruby-swagger/grape/type.rb +46 -119
  42. data/lib/ruby-swagger/io/comparable.rb +13 -16
  43. data/lib/ruby-swagger/io/definitions.rb +5 -11
  44. data/lib/ruby-swagger/io/file_system.rb +8 -9
  45. data/lib/ruby-swagger/io/paths.rb +6 -11
  46. data/lib/ruby-swagger/io/security.rb +3 -6
  47. data/lib/ruby-swagger/object.rb +5 -7
  48. data/lib/ruby-swagger/railtie.rb +1 -1
  49. data/lib/ruby-swagger/template.rb +2 -4
  50. data/lib/tasks/swagger.rake +13 -18
  51. metadata +10 -7
@@ -8,7 +8,6 @@ require 'ruby-swagger/data/security_definitions'
8
8
 
9
9
  module Swagger::Grape
10
10
  class Template
11
-
12
11
  def self.generate(base_class)
13
12
  swagger_doc = Swagger::Template.generate
14
13
 
@@ -31,12 +30,12 @@ module Swagger::Grape
31
30
  scheme.tokenUrl = 'https://'
32
31
  scopes = {}
33
32
  routes.scopes.uniq.each do |scope|
34
- scopes[scope] = ""
33
+ scopes[scope] = ''
35
34
  end
36
35
  scheme.scopes = scopes
37
36
 
38
37
  swagger_doc.securityDefinitions = Swagger::Data::SecurityDefinitions.new
39
- swagger_doc.securityDefinitions.add_param("oauth2", scheme)
38
+ swagger_doc.securityDefinitions.add_param('oauth2', scheme)
40
39
  end
41
40
 
42
41
  swagger_doc
@@ -62,6 +61,5 @@ module Swagger::Grape
62
61
 
63
62
  extract_all_types(new_types, all_types)
64
63
  end
65
-
66
64
  end
67
- end
65
+ end
@@ -3,7 +3,6 @@ require 'ruby-swagger/grape/type'
3
3
 
4
4
  module Swagger::Grape
5
5
  class Method
6
-
7
6
  attr_reader :operation, :types, :scopes
8
7
 
9
8
  def initialize(route_name, route)
@@ -22,40 +21,40 @@ module Swagger::Grape
22
21
 
23
22
  private
24
23
 
25
- #generate the base of the operation
24
+ # generate the base of the operation
26
25
  def new_operation
27
26
  @operation = Swagger::Data::Operation.new
28
27
  @operation.tags = grape_tags
29
28
  @operation.operationId = @route.route_api_name if @route.route_api_name && @route.route_api_name.length > 0
30
29
  @operation.summary = @route.route_description
31
30
  @operation.description = (@route.route_detail && @route.route_detail.length > 0) ? @route.route_detail : @route.route_description
32
- @operation.deprecated = @route.route_deprecated if @route.route_deprecated #grape extension
31
+ @operation.deprecated = @route.route_deprecated if @route.route_deprecated # grape extension
33
32
 
34
33
  @operation
35
34
  end
36
35
 
37
- #extract all the parameters from the method definition (in path, url, body)
36
+ # extract all the parameters from the method definition (in path, url, body)
38
37
  def operation_params
39
38
  extract_params_and_types
40
39
 
41
- @params.each do |param_name, parameter|
40
+ @params.each do |_param_name, parameter|
42
41
  operation.add_parameter(parameter)
43
42
  end
44
43
  end
45
44
 
46
- #extract the data about the response of the method
45
+ # extract the data about the response of the method
47
46
  def operation_responses
48
47
  @operation.responses = Swagger::Data::Responses.new
49
48
 
50
49
  # Include all the possible errors in the response (store the types, they are documented separately)
51
50
  (@route.route_errors || {}).each do |code, response|
52
- error_response = {'description' => response['description'] || response[:description]}
51
+ error_response = { 'description' => response['description'] || response[:description] }
53
52
 
54
- if entity = (response[:entity] || response['entity'])
53
+ if (entity = (response[:entity] || response['entity']))
55
54
  type = Object.const_get entity.to_s
56
55
 
57
56
  error_response['schema'] = {}
58
- error_response['schema']['$ref'] = "#/definitions/#{type.to_s}"
57
+ error_response['schema']['$ref'] = "#/definitions/#{type}"
59
58
 
60
59
  remember_type(type)
61
60
  end
@@ -64,10 +63,10 @@ module Swagger::Grape
64
63
  end
65
64
 
66
65
  if @route.route_response.present? && @route.route_response[:entity].present?
67
- rainbow_response = {'description' => 'Successful result of the operation'}
66
+ rainbow_response = { 'description' => 'Successful result of the operation' }
68
67
 
69
68
  type = Swagger::Grape::Type.new(@route.route_response[:entity].to_s)
70
- current_obj = rainbow_response['schema'] = {}
69
+ rainbow_response['schema'] = {}
71
70
  remember_type(@route.route_response[:entity])
72
71
 
73
72
  # Include any response headers in the documentation of the response
@@ -77,9 +76,9 @@ module Swagger::Grape
77
76
  rainbow_response['headers'] ||= {}
78
77
 
79
78
  rainbow_response['headers'][header_key] = {
80
- 'description'=> header_value['description'] || header_value[:description],
81
- 'type'=> header_value['type'] || header_value[:type],
82
- 'format'=> header_value['format'] || header_value[:format]
79
+ 'description' => header_value['description'] || header_value[:description],
80
+ 'type' => header_value['type'] || header_value[:type],
81
+ 'format' => header_value['format'] || header_value[:format]
83
82
  }
84
83
  end
85
84
  end
@@ -92,14 +91,14 @@ module Swagger::Grape
92
91
  rainbow_response['schema']['type'] = 'object'
93
92
  rainbow_response['schema']['properties'] = {
94
93
  @route.route_response[:root] => {
95
- 'type' => 'array',
96
- 'items' => type.to_swagger
94
+ 'type' => 'array',
95
+ 'items' => type.to_swagger
97
96
  }
98
97
  }
99
98
  else
100
99
  rainbow_response['schema']['type'] = 'object'
101
100
  rainbow_response['schema']['properties'] = {
102
- @route.route_response[:root] => type.to_swagger
101
+ @route.route_response[:root] => type.to_swagger
103
102
  }
104
103
  end
105
104
 
@@ -117,11 +116,11 @@ module Swagger::Grape
117
116
  @operation.responses.add_response('200', Swagger::Data::Response.parse(rainbow_response))
118
117
  end
119
118
 
120
- @operation.responses.add_response('default', Swagger::Data::Response.parse({'description' => 'Unexpected error'}))
119
+ @operation.responses.add_response('default', Swagger::Data::Response.parse({ 'description' => 'Unexpected error' }))
121
120
  end
122
121
 
123
122
  def operation_security
124
- if @route.route_scopes #grape extensions
123
+ if @route.route_scopes # grape extensions
125
124
  security = Swagger::Data::SecurityRequirement.new
126
125
  security.add_requirement('oauth2', @route.route_scopes)
127
126
  @operation.security = [security]
@@ -132,7 +131,7 @@ module Swagger::Grape
132
131
  end
133
132
  end
134
133
 
135
- #extract the tags
134
+ # extract the tags
136
135
  def grape_tags
137
136
  (@route.route_tags && !@route.route_tags.empty?) ? @route.route_tags : [@route_name.split('/')[1]]
138
137
  end
@@ -144,20 +143,20 @@ module Swagger::Grape
144
143
  path_params
145
144
 
146
145
  case @route.route_method.downcase
147
- when 'get'
148
- query_params
149
- when 'delete'
150
- query_params
151
- when 'post'
152
- body_params
153
- when 'put'
154
- body_params
155
- when 'patch'
156
- body_params
157
- when 'head'
158
- raise ArgumentError.new("Don't know how to handle the http verb HEAD for #{@route_name}")
159
- else
160
- raise ArgumentError.new("Don't know how to handle the http verb #{@route.route_method} for #{@route_name}")
146
+ when 'get'
147
+ query_params
148
+ when 'delete'
149
+ query_params
150
+ when 'post'
151
+ body_params
152
+ when 'put'
153
+ body_params
154
+ when 'patch'
155
+ body_params
156
+ when 'head'
157
+ raise ArgumentError.new("Don't know how to handle the http verb HEAD for #{@route_name}")
158
+ else
159
+ raise ArgumentError.new("Don't know how to handle the http verb #{@route.route_method} for #{@route_name}")
161
160
  end
162
161
 
163
162
  @params
@@ -166,14 +165,14 @@ module Swagger::Grape
166
165
  def header_params
167
166
  @params ||= {}
168
167
 
169
- #include all the parameters that are in the headers
168
+ # include all the parameters that are in the headers
170
169
  if @route.route_headers
171
170
  @route.route_headers.each do |header_key, header_value|
172
- @params[header_key] = {'name' => header_key,
173
- 'in' => 'header',
174
- 'required' => (header_value[:required] == true),
175
- 'type' => 'string',
176
- 'description' => header_value[:description]}
171
+ @params[header_key] = { 'name' => header_key,
172
+ 'in' => 'header',
173
+ 'required' => (header_value[:required] == true),
174
+ 'type' => 'string',
175
+ 'description' => header_value[:description] }
177
176
  end
178
177
  end
179
178
 
@@ -181,16 +180,15 @@ module Swagger::Grape
181
180
  end
182
181
 
183
182
  def path_params
184
- #include all the parameters that are in the path
185
-
186
- @route_name.scan(/\{[a-zA-Z0-9\-\_]+\}/).each do |parameter| #scan all parameters in the url
187
- param_name = parameter[1..parameter.length-2]
188
- @params[param_name] = {'name' => param_name,
189
- 'in' => 'path',
190
- 'required' => true,
191
- 'type' => 'string'}
183
+ # include all the parameters that are in the path
184
+
185
+ @route_name.scan(/\{[a-zA-Z0-9\-\_]+\}/).each do |parameter| # scan all parameters in the url
186
+ param_name = parameter[1..parameter.length - 2]
187
+ @params[param_name] = { 'name' => param_name,
188
+ 'in' => 'path',
189
+ 'required' => true,
190
+ 'type' => 'string' }
192
191
  end
193
-
194
192
  end
195
193
 
196
194
  def query_params
@@ -207,15 +205,16 @@ module Swagger::Grape
207
205
  end
208
206
 
209
207
  def body_params
210
- #include all the parameters that are in the content-body
208
+ # include all the parameters that are in the content-body
211
209
  return unless @route.route_params && @route.route_params.length > 0
212
210
 
213
- root_param = Swagger::Data::Parameter.parse({'name' => 'body',
214
- 'in' => 'body',
215
- 'description' => 'the content of the request',
216
- 'schema' => {'type' => 'object', 'properties' => {}}})
211
+ body_name = @operation.operationId ? "#{@operation.operationId}_body" : 'body'
212
+ root_param = Swagger::Data::Parameter.parse({ 'name' => body_name,
213
+ 'in' => 'body',
214
+ 'description' => 'the content of the request',
215
+ 'schema' => { 'type' => 'object', 'properties' => {} } })
217
216
 
218
- #create the params schema
217
+ # create the params schema
219
218
  @route.route_params.each do |parameter|
220
219
  param_name = parameter.first
221
220
  param_value = parameter.last
@@ -224,25 +223,35 @@ module Swagger::Grape
224
223
  next if @params.keys.include?(param_name)
225
224
 
226
225
  if param_name.scan(/[0-9a-zA-Z_]+/).count == 1
227
- #it's a simple parameter, adding it to the properties of the main object
226
+ # it's a simple parameter, adding it to the properties of the main object
228
227
  converted_param = Swagger::Grape::Param.new(param_value)
229
228
  schema.properties[param_name] = converted_param.to_swagger
230
229
  required_parameter(schema, param_name, param_value)
230
+ documented_paramter(schema, param_name, param_value)
231
231
  remember_type(converted_param.type_definition) if converted_param.has_type_definition?
232
232
  else
233
233
  schema_with_subobjects(schema, param_name, parameter.last)
234
234
  end
235
-
236
235
  end
237
236
 
238
- schema= root_param.schema
237
+ schema = root_param.schema
239
238
  @params['body'] = root_param if !schema.properties.nil? && schema.properties.keys.length > 0
240
239
  end
241
240
 
241
+ # Can potentionelly be used for per-param documentation, for now used for example values
242
+ def documented_paramter(schema, name, parameter)
243
+ return if parameter.nil? || parameter[:documentation].nil?
244
+
245
+ unless parameter[:documentation][:example].nil?
246
+ schema['example'] ||= {}
247
+ schema['example'][name] = parameter[:documentation][:example]
248
+ end
249
+ end
250
+
242
251
  def required_parameter(schema, name, parameter)
243
252
  return if parameter.nil? || parameter[:required].nil? || parameter[:required] == false
244
253
 
245
- schema['required'] ||= []
254
+ schema['required'] = [] unless schema['required'].is_a?(Array)
246
255
  schema['required'] << name
247
256
  end
248
257
 
@@ -255,6 +264,7 @@ module Swagger::Grape
255
264
  remember_type(converted_param.type_definition) if converted_param.has_type_definition?
256
265
 
257
266
  required_parameter(append_to, path.last, parameter)
267
+ documented_paramter(append_to, path.last, parameter)
258
268
  end
259
269
 
260
270
  def find_elem_in_schema(root, schema_path)
@@ -265,18 +275,17 @@ module Swagger::Grape
265
275
  return root if root['properties'][next_elem].nil?
266
276
 
267
277
  case root['properties'][next_elem]['type']
268
- when 'array'
269
- #to descend an array this must be an array of objects
270
- root['properties'][next_elem]['items']['type'] = 'object'
271
- root['properties'][next_elem]['items']['properties'] ||= {}
272
-
273
- find_elem_in_schema(root['properties'][next_elem]['items'], schema_path)
274
- when 'object'
275
- find_elem_in_schema(root['properties'][next_elem], schema_path)
276
- else
277
- raise ArgumentError.new("Don't know how to handle the schema path #{schema_path.join('/')}")
278
+ when 'array'
279
+ # to descend an array this must be an array of objects
280
+ root['properties'][next_elem]['items']['type'] = 'object'
281
+ root['properties'][next_elem]['items']['properties'] ||= {}
282
+
283
+ find_elem_in_schema(root['properties'][next_elem]['items'], schema_path)
284
+ when 'object'
285
+ find_elem_in_schema(root['properties'][next_elem], schema_path)
286
+ else
287
+ raise ArgumentError.new("Don't know how to handle the schema path #{schema_path.join('/')}")
278
288
  end
279
-
280
289
  end
281
290
 
282
291
  # Store an object "type" seen on parameters or response types
@@ -290,6 +299,5 @@ module Swagger::Grape
290
299
 
291
300
  @types << type.to_s
292
301
  end
293
-
294
302
  end
295
303
  end
@@ -2,7 +2,6 @@ require 'ruby-swagger/grape/type'
2
2
 
3
3
  module Swagger::Grape
4
4
  class Param
5
-
6
5
  def initialize(param)
7
6
  @param = param
8
7
  end
@@ -11,6 +10,8 @@ module Swagger::Grape
11
10
  swagger_param = {}
12
11
  swagger_param['description'] = @param[:desc] if @param[:desc].present?
13
12
  swagger_param['default'] = @param[:default] if @param[:default].present?
13
+ swagger_param['required'] = @param[:required] if @param.key?(:required)
14
+ swagger_param['enum'] = @param[:values] if @param[:values].present?
14
15
 
15
16
  swagger_param.merge! Swagger::Grape::Type.new(@param[:type]).to_swagger
16
17
 
@@ -28,6 +29,5 @@ module Swagger::Grape
28
29
  def type
29
30
  @param[:type].to_s || 'string'
30
31
  end
31
-
32
32
  end
33
- end
33
+ end
@@ -4,7 +4,6 @@ require 'ruby-swagger/grape/method'
4
4
 
5
5
  module Swagger::Grape
6
6
  class RoutePath
7
-
8
7
  attr_reader :types, :scopes
9
8
 
10
9
  def initialize(route_name)
@@ -32,6 +31,5 @@ module Swagger::Grape
32
31
 
33
32
  path
34
33
  end
35
-
36
34
  end
37
- end
35
+ end
@@ -4,7 +4,6 @@ require 'ruby-swagger/grape/route_path'
4
4
 
5
5
  module Swagger::Grape
6
6
  class Routes
7
-
8
7
  attr_reader :types, :scopes
9
8
 
10
9
  def initialize(routes)
@@ -18,8 +17,7 @@ module Swagger::Grape
18
17
  paths = {}
19
18
 
20
19
  @routes.each do |route|
21
-
22
- next if route.route_hidden == true #implement custom "hidden" extension
20
+ next if route.route_hidden == true # implement custom "hidden" extension
23
21
 
24
22
  swagger_path_name = swagger_path_name(route)
25
23
  paths[swagger_path_name] ||= Swagger::Grape::RoutePath.new(swagger_path_name)
@@ -41,12 +39,11 @@ module Swagger::Grape
41
39
  grape_path_name = grape_route.route_path
42
40
  grape_prefix = grape_route.route_prefix
43
41
  grape_path_name.gsub!(/^\/#{grape_prefix}/, '') if grape_prefix
44
- grape_path_name.gsub!(/^\/:version/, '') #remove api version - if any
45
- grape_path_name.gsub!(/\(\.:format\)$/, '') #remove api format - if any
46
- grape_path_name.gsub!(/\(\..+\)$/, '') #remove api format - if any
47
- grape_path_name.gsub!(/\/:([a-zA-Z0-9_]+)/, '/{\1}') #convert parameters from :format into {format}
42
+ grape_path_name.gsub!(/^\/:version/, '') # remove api version - if any
43
+ grape_path_name.gsub!(/\(\.:format\)$/, '') # remove api format - if any
44
+ grape_path_name.gsub!(/\(\..+\)$/, '') # remove api format - if any
45
+ grape_path_name.gsub!(/\/:([a-zA-Z0-9_]+)/, '/{\1}') # convert parameters from :format into {format}
48
46
  grape_path_name
49
47
  end
50
-
51
48
  end
52
- end
49
+ end
@@ -1,140 +1,67 @@
1
+ require 'ruby-swagger/grape/entity'
2
+ require 'ruby-swagger/grape/entity_exposure'
3
+ require 'ruby-swagger/grape/entity_nesting_exposure'
4
+
1
5
  module Swagger::Grape
2
6
  class Type
3
-
4
- attr_reader :discovered_types
7
+ attr_reader :discovered_types # needed?
5
8
 
6
9
  def initialize(type)
7
10
  @type = type.to_s || 'String'
11
+ @swagger_type = {}
8
12
  end
9
13
 
10
14
  def to_swagger(with_definition = true)
11
- type_convert(@type.to_s, with_definition)
15
+ translate(@type, with_definition)
12
16
  end
13
17
 
14
18
  def sub_types
15
- type = Object.const_get(@type)
16
- return [] unless type.respond_to?(:exposures)
17
-
18
- types = []
19
-
20
- type.exposures.each do |property, definition|
21
- types << definition[:using] if definition[:using].present?
22
- end
23
-
24
- types.uniq
19
+ Swagger::Grape::Entity.new(@type).sub_types
25
20
  end
26
21
 
27
22
  private
28
23
 
29
- def type_convert(type, with_definition = true)
30
- swagger_type = {}
31
-
24
+ def translate(type, with_definition)
32
25
  case type.downcase
33
- when 'string'
34
- swagger_type['type'] = 'string'
35
- when 'integer'
36
- swagger_type['type'] = 'integer'
37
- when 'array'
38
- swagger_type['type'] = 'array'
39
- swagger_type['items'] = {'type' => 'string'}
40
- when 'hash'
41
- swagger_type['type'] = 'object'
42
- swagger_type['properties'] = {}
43
- when 'boolean'
44
- swagger_type['type'] = 'boolean'
45
- when 'virtus::attribute::boolean'
46
- swagger_type['type'] = 'boolean'
47
- when 'symbol'
48
- swagger_type['type'] = 'string'
49
- when 'float'
50
- swagger_type['type'] = 'number'
51
- swagger_type['format'] = 'float'
52
- when 'rack::multipart::uploadedfile'
53
- swagger_type['type'] = 'string'
54
- STDERR.puts "Warning - I have no idea how to handle the type file. Right now I will consider this a string, but we should probably handle it..."
55
- when 'date'
56
- swagger_type['type'] = 'date'
57
- when 'datetime'
58
- swagger_type['type'] = 'string'
59
- swagger_type['format'] = 'date-time'
26
+ when 'string'
27
+ @swagger_type['type'] = 'string'
28
+ when 'integer'
29
+ @swagger_type['type'] = 'integer'
30
+ when 'array'
31
+ @swagger_type['type'] = 'array'
32
+ @swagger_type['items'] = { 'type' => 'string' }
33
+ when 'hash'
34
+ @swagger_type['type'] = 'object'
35
+ @swagger_type['properties'] = {}
36
+ when 'boolean'
37
+ @swagger_type['type'] = 'boolean'
38
+ when 'virtus::attribute::boolean'
39
+ @swagger_type['type'] = 'boolean'
40
+ when 'symbol'
41
+ @swagger_type['type'] = 'string'
42
+ when 'float'
43
+ @swagger_type['type'] = 'number'
44
+ @swagger_type['format'] = 'float'
45
+ when 'rack::multipart::uploadedfile'
46
+ @swagger_type['type'] = 'string'
47
+ STDERR.puts 'Warning - I have no idea how to handle the type file. Right now I will consider this a string, but we should probably handle it...'
48
+ when 'date'
49
+ @swagger_type['type'] = 'string'
50
+ @swagger_type['format'] = 'date'
51
+ when 'datetime'
52
+ @swagger_type['type'] = 'string'
53
+ @swagger_type['format'] = 'date-time'
54
+ else
55
+
56
+ if with_definition
57
+ # I can just reference the name of the object here
58
+ @swagger_type['type'] = 'object'
59
+ @swagger_type['$ref'] = "#/definitions/#{type}"
60
60
  else
61
- swagger_type['type'] = "object"
62
-
63
- if with_definition
64
- # I can just reference the name of the object here
65
- swagger_type['$ref'] = "#/definitions/#{type}"
66
- else
67
- type = Object.const_get(@type)
68
- # I need to define the full object
69
- raise ArgumentError.new("Don't know how to translate the object #{@type}") unless type.respond_to?(:exposures)
70
-
71
- swagger_type['properties'] = {}
72
-
73
- type.exposures.each do |property, definition|
74
-
75
- cursor = swagger_type
76
- target = property.to_s
77
-
78
- if definition[:nested]
79
- # it's a nested parameter
80
- path = target.split('__')
81
- cursor = find_elem_in_schema(cursor, path.dup)
82
- target = path.last
83
- end
84
-
85
- target = definition[:as].to_s if definition[:as].present?
86
-
87
- cursor['properties'][target] = {}
88
-
89
- if definition[:documentation].present? && definition[:documentation][:type].present?
90
- cursor['properties'][target] = type_convert(definition[:documentation][:type].to_s, true)
91
- end
92
-
93
- if definition[:using].present?
94
- #it's either an object or an array of object
95
- using = type_convert(definition[:using].to_s, true)
96
-
97
- if definition['type'].present? && definition['type'] == 'array'
98
- cursor['properties'][target]['items'] = using
99
- else
100
- cursor['properties'][target] = using
101
- end
102
- end
103
-
104
- cursor['properties'][target]['description'] = definition[:documentation][:desc] if definition[:documentation].present?
105
- cursor['properties'][target]['type'] ||= 'string' #no type defined, assuming it's a string
106
- end
107
- end
108
- end
109
-
110
- swagger_type
111
- end
112
-
113
- def find_elem_in_schema(root, schema_path)
114
- return root if schema_path.nil? || schema_path.empty?
115
-
116
- next_elem = schema_path.shift
117
-
118
- return root if root['properties'][next_elem].nil?
119
-
120
- case root['properties'][next_elem]['type']
121
- when 'array'
122
- #to descend an array this must be an array of objects
123
- root['properties'][next_elem]['items']['type'] = 'object'
124
- root['properties'][next_elem]['items']['properties'] ||= {}
125
-
126
- find_elem_in_schema(root['properties'][next_elem]['items'], schema_path)
127
- when 'object'
128
- find_elem_in_schema(root['properties'][next_elem], schema_path)
129
- else
130
- # I'm discending an object that before I assumed was something else
131
- root['properties'][next_elem]['type'] = 'object'
132
- root['properties'][next_elem]['properties'] ||= {}
133
-
134
- find_elem_in_schema(root['properties'][next_elem], schema_path)
61
+ @swagger_type = Swagger::Grape::Entity.new(type).to_swagger
62
+ end
135
63
  end
136
-
64
+ @swagger_type
137
65
  end
138
-
139
66
  end
140
67
  end