grape-swagger 1.4.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +6 -0
  3. data/.github/workflows/ci.yml +45 -0
  4. data/.rubocop.yml +8 -1
  5. data/.rubocop_todo.yml +31 -11
  6. data/CHANGELOG.md +41 -1
  7. data/CONTRIBUTING.md +1 -1
  8. data/Gemfile +6 -5
  9. data/README.md +73 -8
  10. data/UPGRADING.md +11 -0
  11. data/example/config.ru +2 -2
  12. data/grape-swagger.gemspec +3 -2
  13. data/lib/grape-swagger/doc_methods/build_model_definition.rb +1 -20
  14. data/lib/grape-swagger/doc_methods/move_params.rb +7 -8
  15. data/lib/grape-swagger/doc_methods/parse_params.rb +3 -1
  16. data/lib/grape-swagger/endpoint.rb +49 -6
  17. data/lib/grape-swagger/rake/oapi_tasks.rb +37 -17
  18. data/lib/grape-swagger/version.rb +1 -1
  19. data/lib/grape-swagger.rb +2 -2
  20. data/spec/issues/579_align_put_post_parameters_spec.rb +5 -5
  21. data/spec/issues/677_consumes_produces_add_swagger_documentation_options_spec.rb +100 -0
  22. data/spec/issues/751_deeply_nested_objects_spec.rb +2 -2
  23. data/spec/issues/847_route_param_options_spec.rb +37 -0
  24. data/spec/lib/move_params_spec.rb +55 -41
  25. data/spec/lib/oapi_tasks_spec.rb +17 -10
  26. data/spec/spec_helper.rb +0 -4
  27. data/spec/swagger_v2/api_documentation_spec.rb +1 -15
  28. data/spec/swagger_v2/api_swagger_v2_additional_properties_spec.rb +1 -1
  29. data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +7 -7
  30. data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +7 -7
  31. data/spec/swagger_v2/api_swagger_v2_response_with_models_spec.rb +4 -2
  32. data/spec/swagger_v2/param_values_spec.rb +1 -1
  33. metadata +10 -114
  34. data/.github/workflows/rubocop.yml +0 -26
  35. data/.github/workflows/ruby.yml +0 -30
  36. data/.ruby-gemset +0 -1
@@ -5,7 +5,7 @@ require 'active_support/core_ext/string/inflections'
5
5
  require 'grape-swagger/endpoint/params_parser'
6
6
 
7
7
  module Grape
8
- class Endpoint
8
+ class Endpoint # rubocop:disable Metrics/ClassLength
9
9
  def content_types_for(target_class)
10
10
  content_types = (target_class.content_types || {}).values
11
11
 
@@ -27,7 +27,8 @@ module Grape
27
27
  object = {
28
28
  info: info_object(options[:info].merge(version: options[:doc_version])),
29
29
  swagger: '2.0',
30
- produces: content_types_for(target_class),
30
+ produces: options[:produces] || content_types_for(target_class),
31
+ consumes: options[:consumes],
31
32
  authorizations: options[:authorizations],
32
33
  securityDefinitions: options[:security_definitions],
33
34
  security: options[:security],
@@ -117,7 +118,7 @@ module Grape
117
118
  method[:summary] = summary_object(route)
118
119
  method[:description] = description_object(route)
119
120
  method[:produces] = produces_object(route, options[:produces] || options[:format])
120
- method[:consumes] = consumes_object(route, options[:format])
121
+ method[:consumes] = consumes_object(route, options[:consumes] || options[:format])
121
122
  method[:parameters] = params_object(route, options, path)
122
123
  method[:security] = security_object(route)
123
124
  method[:responses] = response_object(route, options)
@@ -240,7 +241,8 @@ module Grape
240
241
  if route.http_codes.is_a?(Array) && route.http_codes.any? { |code| success_code?(code) }
241
242
  route.http_codes.clone
242
243
  else
243
- success_codes_from_route(route) + (route.http_codes || route.options[:failure] || [])
244
+ success_codes_from_route(route) + default_code_from_route(route) +
245
+ (route.http_codes || route.options[:failure] || [])
244
246
  end
245
247
  end
246
248
 
@@ -267,6 +269,21 @@ module Grape
267
269
 
268
270
  private
269
271
 
272
+ def default_code_from_route(route)
273
+ entity = route.options[:default_response]
274
+ return [] if entity.nil?
275
+
276
+ default_code = { code: 'default', message: 'Default Response' }
277
+ if entity.is_a?(Hash)
278
+ default_code[:message] = entity[:message] || default_code[:message]
279
+ default_code[:model] = entity[:model] if entity[:model].present?
280
+ else
281
+ default_code[:model] = entity
282
+ end
283
+
284
+ [default_code]
285
+ end
286
+
270
287
  def build_memo_schema(memo, route, value, response_model, options)
271
288
  if memo[value[:code]][:schema] && value[:as]
272
289
  memo[value[:code]][:schema][:properties].merge!(build_reference(route, value, response_model, options))
@@ -294,7 +311,7 @@ module Grape
294
311
  else
295
312
  build_reference_hash(response_model)
296
313
  end
297
- return reference unless value[:code] < 300
314
+ return reference unless value[:code] == 'default' || value[:code] < 300
298
315
 
299
316
  if value.key?(:as) && value.key?(:is_array)
300
317
  reference[value[:as]] = build_reference_array(reference[value[:as]])
@@ -348,13 +365,39 @@ module Grape
348
365
  end
349
366
 
350
367
  def merge_params(route)
368
+ path_params = get_path_params(route.app&.inheritable_setting&.namespace_stackable)
351
369
  param_keys = route.params.keys
370
+
371
+ # Merge path params options into route params
372
+ route_params = route.params
373
+ route_params.each_key do |key|
374
+ path = path_params[key] || {}
375
+ params = route_params[key]
376
+ params = {} unless params.is_a? Hash
377
+ route_params[key] = path.merge(params)
378
+ end
379
+
352
380
  route.params.delete_if { |key| key.is_a?(String) && param_keys.include?(key.to_sym) }.to_a
353
381
  end
354
382
 
383
+ # Iterates over namespaces recursively
384
+ # to build a hash of path params with options, including type
385
+ def get_path_params(stackable_values)
386
+ params = {}
387
+ return param unless stackable_values
388
+ return params unless stackable_values.is_a? Grape::Util::StackableValues
389
+
390
+ stackable_values&.new_values&.dig(:namespace)&.each do |namespace|
391
+ space = namespace.space.to_s.gsub(':', '')
392
+ params[space] = namespace.options || {}
393
+ end
394
+ inherited_params = get_path_params(stackable_values.inherited_values)
395
+ inherited_params.merge(params)
396
+ end
397
+
355
398
  def default_type(params)
356
399
  default_param_type = { required: true, type: 'Integer' }
357
- params.each { |param| param[-1] = param.last == '' ? default_param_type : param.last }
400
+ params.each { |param| param[-1] = param.last.empty? ? default_param_type : param.last }
358
401
  end
359
402
 
360
403
  def expose_params(value)
@@ -46,9 +46,12 @@ module GrapeSwagger
46
46
  resource - if given only for that it would be generated (optional)'
47
47
  task fetch: :environment do
48
48
  # :nocov:
49
- make_request
49
+ urls_for(api_class).each do |url|
50
+ make_request(url)
51
+
52
+ save_to_file? ? File.write(file(url), @oapi) : $stdout.print(@oapi)
53
+ end
50
54
 
51
- save_to_file? ? File.write(file, @oapi) : $stdout.print(@oapi)
52
55
  # :nocov:
53
56
  end
54
57
  end
@@ -64,10 +67,15 @@ module GrapeSwagger
64
67
  ::Rake::Task['oapi:fetch'].invoke
65
68
  exit if error?
66
69
 
67
- output = system "swagger-cli validate #{file}"
70
+ urls_for(api_class).each do |url|
71
+ @output = system "swagger-cli validate #{file(url)}"
72
+
73
+ FileUtils.rm(
74
+ file(url)
75
+ )
76
+ end
68
77
 
69
- $stdout.puts 'install swagger-cli with `npm install swagger-cli -g`' if output.nil?
70
- FileUtils.rm(file)
78
+ $stdout.puts 'install swagger-cli with `npm install swagger-cli -g`' if @output.nil?
71
79
  # :nocov:
72
80
  end
73
81
  end
@@ -75,23 +83,28 @@ module GrapeSwagger
75
83
  # helper methods
76
84
  #
77
85
  # rubocop:disable Style/StringConcatenation
78
- def make_request
79
- get url_for
86
+ def make_request(url)
87
+ get url
80
88
 
81
89
  @oapi = JSON.pretty_generate(
82
- JSON.parse(
83
- last_response.body, symolize_names: true
84
- )
90
+ JSON.parse(last_response.body, symolize_names: true)
85
91
  ) + "\n"
86
92
  end
87
93
  # rubocop:enable Style/StringConcatenation
88
94
 
89
- def url_for
90
- oapi_route = api_class.routes[-2]
91
- path = oapi_route.path.sub(/\(\.\w+\)$/, '').sub(/\(\.:\w+\)$/, '')
92
- path.sub!(':version', oapi_route.version.to_s)
95
+ def urls_for(api_class)
96
+ api_class.routes
97
+ .map(&:path)
98
+ .select { |e| e.include?('doc') }
99
+ .reject { |e| e.include?(':name') }
100
+ .map { |e| format_path(e) }
101
+ .map { |e| [e, ENV.fetch('resource', nil)].join('/').chomp('/') }
102
+ end
93
103
 
94
- [path, ENV['resource']].join('/').chomp('/')
104
+ def format_path(path)
105
+ oapi_route = api_class.routes.select { |e| e.path == path }.first
106
+ path = path.sub(/\(\.\w+\)$/, '').sub(/\(\.:\w+\)$/, '')
107
+ path.sub(':version', oapi_route.version.to_s)
95
108
  end
96
109
 
97
110
  def save_to_file?
@@ -102,8 +115,15 @@ module GrapeSwagger
102
115
  JSON.parse(@oapi).keys.first == 'error'
103
116
  end
104
117
 
105
- def file
106
- name = ENV['store'] == 'true' || ENV['store'].blank? ? 'swagger_doc.json' : ENV['store']
118
+ def file(url)
119
+ api_version = url.split('/').last
120
+
121
+ name = if ENV['store'] == 'true' || ENV['store'].blank?
122
+ "swagger_doc_#{api_version}.json"
123
+ else
124
+ ENV['store'].sub('.json', "_#{api_version}.json")
125
+ end
126
+
107
127
  File.join(Dir.getwd, name)
108
128
  end
109
129
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrapeSwagger
4
- VERSION = '1.4.2'
4
+ VERSION = '1.6.0'
5
5
  end
data/lib/grape-swagger.rb CHANGED
@@ -31,8 +31,8 @@ module SwaggerRouting
31
31
 
32
32
  # want to match emojis … ;)
33
33
  # route_match = route_match
34
- # .match('\/([\p{Alnum}|\p{Emoji}|\-|\_]*?)[\.\/\(]') || route_match.match('\/([\p{Alpha}|\p{Emoji}|\-|\_]*)$')
35
- route_match = route_match.match('\/([\p{Alnum}|\-|\_]*?)[\.\/\(]') || route_match.match('\/([\p{Alpha}|\-|\_]*)$')
34
+ # .match('\/([\p{Alnum}p{Emoji}\-\_]*?)[\.\/\(]') || route_match.match('\/([\p{Alpha}\p{Emoji}\-\_]*)$')
35
+ route_match = route_match.match('\/([\p{Alnum}\-\_]*?)[\.\/\(]') || route_match.match('\/([\p{Alpha}\-\_]*)$')
36
36
  next unless route_match
37
37
 
38
38
  resource = route_match.captures.first
@@ -103,8 +103,8 @@ describe '#579 put / post parameters spec' do
103
103
  [
104
104
  { 'in' => 'path', 'name' => 'guid', 'type' => 'string', 'format' => 'guid', 'required' => true },
105
105
  {
106
- 'name' => 'Issue579ImplicitBodyParameter', 'in' => 'body', 'required' => true, 'schema' => {
107
- '$ref' => '#/definitions/putIssue579ImplicitBodyParameter'
106
+ 'name' => 'putIssue579ImplicitBodyParameterGuid', 'in' => 'body', 'required' => true, 'schema' => {
107
+ '$ref' => '#/definitions/putIssue579ImplicitBodyParameterGuid'
108
108
  }
109
109
  }
110
110
  ]
@@ -130,8 +130,8 @@ describe '#579 put / post parameters spec' do
130
130
  [
131
131
  { 'in' => 'path', 'name' => 'guid', 'type' => 'string', 'format' => 'guid', 'required' => true },
132
132
  {
133
- 'name' => 'Issue579ExplicitBodyParameter', 'in' => 'body', 'required' => true, 'schema' => {
134
- '$ref' => '#/definitions/putIssue579ExplicitBodyParameter'
133
+ 'name' => 'putIssue579ExplicitBodyParameterGuid', 'in' => 'body', 'required' => true, 'schema' => {
134
+ '$ref' => '#/definitions/putIssue579ExplicitBodyParameterGuid'
135
135
  }
136
136
  }
137
137
  ]
@@ -157,7 +157,7 @@ describe '#579 put / post parameters spec' do
157
157
  [
158
158
  { 'in' => 'path', 'name' => 'guid', 'type' => 'string', 'format' => 'guid', 'required' => true },
159
159
  {
160
- 'name' => 'Issue579NamespaceParamGuidBodyParameter', 'in' => 'body', 'required' => true, 'schema' => {
160
+ 'name' => 'putIssue579NamespaceParamGuidBodyParameter', 'in' => 'body', 'required' => true, 'schema' => {
161
161
  '$ref' => '#/definitions/putIssue579NamespaceParamGuidBodyParameter'
162
162
  }
163
163
  }
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe '#677 consumes and produces options are included in add_swagger_documentation options' do
6
+ describe 'no override' do
7
+ let(:app) do
8
+ Class.new(Grape::API) do
9
+ resource :accounts do
10
+ route_param :account_number, type: String do
11
+ resource :records do
12
+ route_param :id do
13
+ post do
14
+ { message: 'hello world' }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ add_swagger_documentation \
22
+ format: :json
23
+ end
24
+ end
25
+
26
+ subject do
27
+ get '/swagger_doc'
28
+ JSON.parse(last_response.body)
29
+ end
30
+
31
+ specify do
32
+ expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['produces']).to eq ['application/json']
33
+ expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['consumes']).to eq ['application/json']
34
+ end
35
+ end
36
+
37
+ describe 'override produces' do
38
+ let(:app) do
39
+ Class.new(Grape::API) do
40
+ resource :accounts do
41
+ route_param :account_number, type: String do
42
+ resource :records do
43
+ route_param :id do
44
+ post do
45
+ { message: 'hello world' }
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ add_swagger_documentation \
53
+ format: :json,
54
+ produces: ['text/plain']
55
+ end
56
+ end
57
+
58
+ subject do
59
+ get '/swagger_doc'
60
+ JSON.parse(last_response.body)
61
+ end
62
+
63
+ specify do
64
+ expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['produces']).to eq ['text/plain']
65
+ expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['consumes']).to eq ['application/json']
66
+ end
67
+ end
68
+
69
+ describe 'override consumes' do
70
+ let(:app) do
71
+ Class.new(Grape::API) do
72
+ resource :accounts do
73
+ route_param :account_number, type: String do
74
+ resource :records do
75
+ route_param :id do
76
+ post do
77
+ { message: 'hello world' }
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ add_swagger_documentation \
85
+ format: :json, \
86
+ consumes: ['application/json', 'application/x-www-form-urlencoded']
87
+ end
88
+ end
89
+
90
+ subject do
91
+ get '/swagger_doc'
92
+ JSON.parse(last_response.body)
93
+ end
94
+
95
+ specify do
96
+ expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['produces']).to eq ['application/json']
97
+ expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['consumes']).to eq ['application/json', 'application/x-www-form-urlencoded']
98
+ end
99
+ end
100
+ end
@@ -75,7 +75,7 @@ describe '751 deeply nested objects' do
75
75
  end
76
76
 
77
77
  describe 'Correctness of vrp Points' do
78
- let(:get_points_response) { subject['definitions']['postVrpSubmit']['properties']['vrp']['properties']['points'] }
78
+ let(:get_points_response) { subject['definitions']['vrp']['properties']['vrp']['properties']['points'] }
79
79
  specify do
80
80
  expect(get_points_response).to eql(
81
81
  'type' => 'array',
@@ -111,7 +111,7 @@ describe '751 deeply nested objects' do
111
111
  end
112
112
 
113
113
  describe 'Correctness of vrp Services' do
114
- let(:get_service_response) { subject['definitions']['postVrpSubmit']['properties']['vrp']['properties']['services'] }
114
+ let(:get_service_response) { subject['definitions']['vrp']['properties']['vrp']['properties']['services'] }
115
115
  specify do
116
116
  expect(get_service_response).to include(
117
117
  'type' => 'array',
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe '#847 route_param type is included in documentation' do
6
+ let(:app) do
7
+ Class.new(Grape::API) do
8
+ resource :accounts do
9
+ route_param :account_number, type: String do
10
+ resource :records do
11
+ route_param :id do
12
+ get do
13
+ { message: 'hello world' }
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ add_swagger_documentation
21
+ end
22
+ end
23
+ let(:parameters) { subject['paths']['/accounts/{account_number}/records/{id}']['get']['parameters'] }
24
+
25
+ subject do
26
+ get '/swagger_doc'
27
+ JSON.parse(last_response.body)
28
+ end
29
+
30
+ specify do
31
+ account_number_param = parameters.find { |param| param['name'] == 'account_number' }
32
+ expect(account_number_param['type']).to eq 'string'
33
+ id_param = parameters.find { |param| param['name'] == 'id' }
34
+ # Default is still integer
35
+ expect(id_param['type']).to eq 'integer'
36
+ end
37
+ end
@@ -103,15 +103,30 @@ describe GrapeSwagger::DocMethods::MoveParams do
103
103
  subject.to_definition(path, params, route, definitions)
104
104
  expect(params).to eql(
105
105
  [
106
- { name: 'InBody', in: 'body', required: true, schema: { '$ref' => '#/definitions/postInBody' } }
106
+ { name: 'postInBody', in: 'body', required: true, schema: { '$ref' => '#/definitions/postInBody' } }
107
107
  ]
108
108
  )
109
109
  expect(subject.definitions['postInBody']).not_to include :description
110
110
  expect(subject.definitions['postInBody']).to eql expected_post_defs
111
111
  end
112
+
113
+ context 'with a nickname' do
114
+ let(:route_options) { { nickname: 'post-body' } }
115
+
116
+ specify do
117
+ subject.to_definition(path, params, route, definitions)
118
+ expect(params).to eql(
119
+ [
120
+ { name: 'post-body', in: 'body', required: true, schema: { '$ref' => '#/definitions/post-body' } }
121
+ ]
122
+ )
123
+ expect(subject.definitions['post-body']).not_to include :description
124
+ expect(subject.definitions['post-body']).to eql expected_post_defs
125
+ end
126
+ end
112
127
  end
113
128
 
114
- describe 'POST' do
129
+ describe 'PUT' do
115
130
  let(:params) { paths['/in_body/{key}'][:put][:parameters] }
116
131
  let(:route) { Grape::Router::Route.new('PUT', path.dup, **route_options) }
117
132
 
@@ -120,12 +135,28 @@ describe GrapeSwagger::DocMethods::MoveParams do
120
135
  expect(params).to eql(
121
136
  [
122
137
  { in: 'path', name: 'key', description: nil, type: 'integer', format: 'int32', required: true },
123
- { name: 'InBody', in: 'body', required: true, schema: { '$ref' => '#/definitions/putInBody' } }
138
+ { name: 'putInBody', in: 'body', required: true, schema: { '$ref' => '#/definitions/putInBody' } }
124
139
  ]
125
140
  )
126
141
  expect(subject.definitions['putInBody']).not_to include :description
127
142
  expect(subject.definitions['putInBody']).to eql expected_put_defs
128
143
  end
144
+
145
+ context 'with a nickname' do
146
+ let(:route_options) { { nickname: 'put-body' } }
147
+
148
+ specify do
149
+ subject.to_definition(path, params, route, definitions)
150
+ expect(params).to eql(
151
+ [
152
+ { in: 'path', name: 'key', description: nil, type: 'integer', format: 'int32', required: true },
153
+ { name: 'put-body', in: 'body', required: true, schema: { '$ref' => '#/definitions/put-body' } }
154
+ ]
155
+ )
156
+ expect(subject.definitions['put-body']).not_to include :description
157
+ expect(subject.definitions['put-body']).to eql expected_put_defs
158
+ end
159
+ end
129
160
  end
130
161
  end
131
162
 
@@ -167,56 +198,39 @@ describe GrapeSwagger::DocMethods::MoveParams do
167
198
  let(:params) { [{ in: 'body', name: 'address[street][name]', description: 'street', type: 'string', required: true }] }
168
199
  before do
169
200
  subject.instance_variable_set(:@definitions, definitions)
170
- subject.send(:build_definition, name, params, verb)
201
+ subject.send(:build_definition, name, params)
171
202
  end
172
203
 
173
- describe 'verb given' do
174
- let(:verb) { 'post' }
175
- let(:name) { 'Foo' }
176
- let(:definitions) { {} }
204
+ let(:name) { 'FooBar' }
205
+ let(:definitions) { {} }
177
206
 
178
- specify do
179
- definition = definitions.to_a.first
180
- expect(definition.first).to eql 'postFoo'
181
- expect(definition.last).to eql(type: 'object', properties: {})
182
- end
183
- end
184
-
185
- describe 'no verb given' do
186
- let(:name) { 'FooBar' }
187
- let(:definitions) { {} }
188
- let(:verb) { nil }
189
-
190
- specify do
191
- definition = definitions.to_a.first
192
- expect(definition.first).to eql 'FooBar'
193
- expect(definition.last).to eql(type: 'object', properties: {})
194
- end
207
+ specify do
208
+ definition = definitions.to_a.first
209
+ expect(definition.first).to eql 'FooBar'
210
+ expect(definition.last).to eql(type: 'object', properties: {})
195
211
  end
196
212
  end
197
213
 
198
214
  describe 'build_body_parameter' do
199
- describe 'name given' do
200
- let(:name) { 'Foo' }
201
- let(:reference) { 'Bar' }
215
+ let(:name) { 'Foo' }
216
+ let(:reference) { 'Bar' }
217
+ let(:expected_param) do
218
+ { name: name, in: 'body', required: true, schema: { '$ref' => "#/definitions/#{name}" } }
219
+ end
220
+ specify do
221
+ parameter = subject.send(:build_body_parameter, name, {})
222
+ expect(parameter).to eql expected_param
223
+ end
224
+
225
+ describe 'body_name option specified' do
226
+ let(:route_options) { { body_name: 'body' } }
202
227
  let(:expected_param) do
203
- { name: name, in: 'body', required: true, schema: { '$ref' => "#/definitions/#{reference}" } }
228
+ { name: route_options[:body_name], in: 'body', required: true, schema: { '$ref' => "#/definitions/#{name}" } }
204
229
  end
205
230
  specify do
206
- parameter = subject.send(:build_body_parameter, reference, name, {})
231
+ parameter = subject.send(:build_body_parameter, name, route_options)
207
232
  expect(parameter).to eql expected_param
208
233
  end
209
-
210
- describe 'body_name option specified' do
211
- let(:route_options) { { body_name: 'body' } }
212
- let(:expected_param) do
213
- { name: route_options[:body_name], in: 'body', required: true, schema: { '$ref' => "#/definitions/#{reference}" } }
214
- end
215
- specify do
216
- parameter = subject.send(:build_body_parameter, reference, name, route_options)
217
- expect(parameter).to eql expected_param
218
- end
219
- end
220
234
  end
221
235
  end
222
236
 
@@ -25,9 +25,12 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
25
25
 
26
26
  subject { described_class.new(Api::Base) }
27
27
 
28
+ let(:api_class) { subject.send(:api_class) }
29
+ let(:docs_url) { subject.send(:urls_for, api_class).first }
30
+
28
31
  describe '.new' do
29
32
  it 'accepts class name as a constant' do
30
- expect(described_class.new(::Api::Base).send(:api_class)).to eq(Api::Base)
33
+ expect(described_class.new(Api::Base).send(:api_class)).to eq(Api::Base)
31
34
  end
32
35
 
33
36
  it 'accepts class name as a string' do
@@ -38,7 +41,7 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
38
41
  describe '#make_request' do
39
42
  describe 'complete documentation' do
40
43
  before do
41
- subject.send(:make_request)
44
+ subject.send(:make_request, docs_url)
42
45
  end
43
46
 
44
47
  describe 'not storing' do
@@ -51,7 +54,7 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
51
54
  end
52
55
 
53
56
  it 'requests doc url' do
54
- expect(subject.send(:url_for)).to eql '/api/swagger_doc'
57
+ expect(docs_url).to eql '/api/swagger_doc'
55
58
  end
56
59
  end
57
60
 
@@ -68,10 +71,14 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
68
71
  describe 'documentation for resource' do
69
72
  before do
70
73
  ENV['resource'] = resource
71
- subject.send(:make_request)
74
+ subject.send(:make_request, docs_url)
72
75
  end
73
76
 
74
- let(:response) { JSON.parse(subject.send(:make_request)) }
77
+ let(:response) do
78
+ JSON.parse(
79
+ subject.send(:make_request, docs_url)
80
+ )
81
+ end
75
82
 
76
83
  after { ENV.delete('resource') }
77
84
 
@@ -83,7 +90,7 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
83
90
  end
84
91
 
85
92
  it 'requests doc url' do
86
- expect(subject.send(:url_for)).to eql "/api/swagger_doc/#{resource}"
93
+ expect(docs_url).to eql "/api/swagger_doc/#{resource}"
87
94
  end
88
95
 
89
96
  it 'has only one resource path' do
@@ -115,7 +122,7 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
115
122
 
116
123
  describe 'call it' do
117
124
  before do
118
- subject.send(:make_request)
125
+ subject.send(:make_request, docs_url)
119
126
  end
120
127
  specify do
121
128
  expect(subject).to respond_to :oapi
@@ -128,7 +135,7 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
128
135
  describe '#file' do
129
136
  describe 'no store given' do
130
137
  it 'returns swagger_doc.json' do
131
- expect(subject.send(:file)).to end_with 'swagger_doc.json'
138
+ expect(subject.send(:file, docs_url)).to end_with 'swagger_doc.json'
132
139
  end
133
140
  end
134
141
 
@@ -139,7 +146,7 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
139
146
  before { ENV['store'] = 'true' }
140
147
 
141
148
  it 'returns swagger_doc.json' do
142
- expect(subject.send(:file)).to end_with 'swagger_doc.json'
149
+ expect(subject.send(:file, docs_url)).to end_with 'swagger_doc.json'
143
150
  end
144
151
  end
145
152
 
@@ -148,7 +155,7 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
148
155
  before { ENV['store'] = name }
149
156
 
150
157
  it 'returns swagger_doc.json' do
151
- expect(subject.send(:file)).to end_with name
158
+ expect(subject.send(:file, docs_url)).to include(name.split('.')[0])
152
159
  end
153
160
  end
154
161
  end
data/spec/spec_helper.rb CHANGED
@@ -5,10 +5,6 @@ if RUBY_ENGINE == 'ruby'
5
5
  require 'coveralls'
6
6
 
7
7
  SimpleCov.formatter = Coveralls::SimpleCov::Formatter
8
- SimpleCov.start do
9
- add_filter 'spec/'
10
- add_filter 'example/'
11
- end
12
8
  Coveralls.wear!
13
9
  end
14
10
 
@@ -21,21 +21,7 @@ describe 'API with additional options' do
21
21
  expect(subject).to eq(
22
22
  [
23
23
  { description: 'Swagger compatible API description' },
24
- {
25
- description: 'Swagger compatible API description for specific API',
26
- params: {
27
- 'locale' => {
28
- desc: 'Locale of API documentation',
29
- required: false,
30
- type: 'Symbol'
31
- },
32
- 'name' => {
33
- desc: 'Resource name of mounted API',
34
- required: true,
35
- type: 'String'
36
- }
37
- }
38
- }
24
+ { description: 'Swagger compatible API description for specific API', params: {} }
39
25
  ]
40
26
  )
41
27
  end