apipie-rails 0.9.3 → 1.0.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 (96) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rubocop.yml +2 -2
  3. data/.rubocop.yml +23 -14
  4. data/.rubocop_todo.yml +103 -487
  5. data/CHANGELOG.md +20 -3
  6. data/README.rst +12 -12
  7. data/app/controllers/apipie/apipies_controller.rb +6 -6
  8. data/app/helpers/apipie_helper.rb +1 -1
  9. data/lib/apipie/apipie_module.rb +5 -5
  10. data/lib/apipie/application.rb +81 -55
  11. data/lib/apipie/configuration.rb +19 -26
  12. data/lib/apipie/dsl_definition.rb +8 -9
  13. data/lib/apipie/error_description.rb +1 -1
  14. data/lib/apipie/errors.rb +2 -16
  15. data/lib/apipie/extractor/collector.rb +3 -3
  16. data/lib/apipie/extractor/recorder.rb +1 -1
  17. data/lib/apipie/extractor.rb +2 -2
  18. data/lib/apipie/generator/config.rb +12 -0
  19. data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
  20. data/lib/apipie/generator/swagger/config.rb +78 -0
  21. data/lib/apipie/generator/swagger/context.rb +12 -1
  22. data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
  23. data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +86 -0
  24. data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
  25. data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
  26. data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
  27. data/lib/apipie/generator/swagger/method_description/response_service.rb +58 -0
  28. data/lib/apipie/generator/swagger/method_description.rb +2 -0
  29. data/lib/apipie/generator/swagger/operation_id.rb +2 -2
  30. data/lib/apipie/generator/swagger/param_description/builder.rb +4 -4
  31. data/lib/apipie/generator/swagger/param_description/composite.rb +9 -1
  32. data/lib/apipie/generator/swagger/param_description/in.rb +1 -1
  33. data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
  34. data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
  35. data/lib/apipie/generator/swagger/param_description/type.rb +9 -2
  36. data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
  37. data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
  38. data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
  39. data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
  40. data/lib/apipie/generator/swagger/schema.rb +63 -0
  41. data/lib/apipie/generator/swagger/type_extractor.rb +0 -19
  42. data/lib/apipie/generator/swagger/warning.rb +3 -6
  43. data/lib/apipie/generator/swagger/warning_writer.rb +7 -1
  44. data/lib/apipie/helpers.rb +3 -3
  45. data/lib/apipie/method_description.rb +5 -3
  46. data/lib/apipie/param_description.rb +4 -2
  47. data/lib/apipie/resource_description.rb +11 -8
  48. data/lib/apipie/response_description.rb +1 -1
  49. data/lib/apipie/response_description_adapter.rb +3 -3
  50. data/lib/apipie/routing.rb +1 -1
  51. data/lib/apipie/rspec/response_validation_helper.rb +1 -1
  52. data/lib/apipie/swagger_generator.rb +27 -551
  53. data/lib/apipie/validator.rb +9 -5
  54. data/lib/apipie/version.rb +1 -1
  55. data/lib/apipie-rails.rb +17 -0
  56. data/lib/tasks/apipie.rake +25 -20
  57. data/spec/controllers/api/v2/nested/resources_controller_spec.rb +2 -2
  58. data/spec/controllers/pets_controller_spec.rb +10 -16
  59. data/spec/controllers/users_controller_spec.rb +2 -2
  60. data/spec/dummy/app/controllers/pets_controller.rb +4 -4
  61. data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
  62. data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
  63. data/spec/dummy/app/controllers/users_controller.rb +5 -5
  64. data/spec/dummy/config.ru +1 -1
  65. data/spec/lib/apipie/apipies_controller_spec.rb +4 -0
  66. data/spec/lib/apipie/application_spec.rb +25 -15
  67. data/spec/lib/apipie/configuration_spec.rb +15 -0
  68. data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
  69. data/spec/lib/apipie/generator/swagger/context_spec.rb +23 -2
  70. data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +106 -0
  71. data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
  72. data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +1 -1
  73. data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +2 -2
  74. data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +7 -7
  75. data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
  76. data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
  77. data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
  78. data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
  79. data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
  80. data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +0 -43
  81. data/spec/lib/apipie/generator/swagger/warning_spec.rb +1 -1
  82. data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +19 -7
  83. data/spec/lib/apipie/method_description_spec.rb +101 -66
  84. data/spec/lib/apipie/no_documented_method_spec.rb +17 -0
  85. data/spec/lib/apipie/param_description_spec.rb +209 -49
  86. data/spec/lib/apipie/param_group_spec.rb +1 -0
  87. data/spec/lib/apipie/resource_description_spec.rb +71 -28
  88. data/spec/lib/apipie/response_does_not_match_swagger_schema_spec.rb +35 -0
  89. data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
  90. data/spec/lib/apipie/validator_spec.rb +47 -11
  91. data/spec/lib/rake_spec.rb +1 -1
  92. data/spec/lib/swagger/rake_swagger_spec.rb +6 -6
  93. data/spec/lib/swagger/swagger_dsl_spec.rb +17 -11
  94. data/spec/lib/validators/array_validator_spec.rb +1 -1
  95. data/spec/spec_helper.rb +2 -2
  96. metadata +31 -3
@@ -20,7 +20,7 @@ namespace :apipie do
20
20
  task :static, [:version] => :environment do |t, args|
21
21
  with_loaded_documentation do
22
22
  args.with_defaults(:version => Apipie.configuration.default_version)
23
- out = ENV["OUT"] || File.join(::Rails.root, Apipie.configuration.doc_path, 'apidoc')
23
+ out = ENV["OUT"] || File.join(Rails.root, Apipie.configuration.doc_path, 'apidoc')
24
24
  subdir = File.basename(out)
25
25
  copy_jscss(out)
26
26
  Apipie.configuration.version_in_url = false
@@ -44,7 +44,7 @@ namespace :apipie do
44
44
  task :static_json, [:version] => :environment do |t, args|
45
45
  with_loaded_documentation do
46
46
  args.with_defaults(:version => Apipie.configuration.default_version)
47
- out = ENV["OUT"] || File.join(::Rails.root, Apipie.configuration.doc_path, 'apidoc')
47
+ out = ENV["OUT"] || File.join(Rails.root, Apipie.configuration.doc_path, 'apidoc')
48
48
  ([nil] + Apipie.configuration.languages).each do |lang|
49
49
  doc = Apipie.to_json(args[:version], nil, nil, lang)
50
50
  generate_json_page(out, doc, lang)
@@ -55,7 +55,7 @@ namespace :apipie do
55
55
  desc "Generate static swagger json"
56
56
  task :static_swagger_json, [:version, :swagger_content_type_input, :filename_suffix] => :environment do |t, args|
57
57
  with_loaded_documentation do
58
- out = ENV["OUT"] || File.join(::Rails.root, Apipie.configuration.doc_path, 'apidoc')
58
+ out = ENV["OUT"] || File.join(Rails.root, Apipie.configuration.doc_path, 'apidoc')
59
59
  generate_swagger_using_args(args, out)
60
60
  end
61
61
  end
@@ -68,7 +68,7 @@ namespace :apipie do
68
68
  desc "Did swagger output change since the last execution of this task?"
69
69
  task :did_swagger_change, [:version, :swagger_content_type_input, :filename_suffix] => :environment do |t, args|
70
70
  with_loaded_documentation do
71
- out = ENV["OUT_REF"] || File.join(::Rails.root, Apipie.configuration.doc_path, 'apidoc_ref')
71
+ out = ENV["OUT_REF"] || File.join(Rails.root, Apipie.configuration.doc_path, 'apidoc_ref')
72
72
  paths = generate_swagger_using_args(args, out)
73
73
  paths.each {|path|
74
74
  existing_files_in_dir = Pathname(out).children(true)
@@ -123,17 +123,17 @@ namespace :apipie do
123
123
  # Default output dir ('public/apipie_cache') can be changed with OUT=/some/dir
124
124
  desc "Generate cache to avoid production dependencies on markup languages"
125
125
  task :cache => :environment do
126
- puts "#{Time.now} | Started"
126
+ puts "#{Time.zone.now} | Started"
127
127
  cache_part = ENV['cache_part']
128
128
  generate_index = (cache_part == 'resources' ? false : true)
129
129
  generate_resources = (cache_part == 'index' ? false : true)
130
130
  with_loaded_documentation do
131
- puts "#{Time.now} | Documents loaded..."
131
+ puts "#{Time.zone.now} | Documents loaded..."
132
132
  ([nil] + Apipie.configuration.languages).each do |lang|
133
133
  I18n.locale = lang || Apipie.configuration.default_locale
134
- puts "#{Time.now} | Processing docs for #{lang}"
134
+ puts "#{Time.zone.now} | Processing docs for #{lang}"
135
135
  cache_dir = ENV["OUT"] || Apipie.configuration.cache_dir
136
- subdir = Apipie.configuration.doc_base_url.sub(/\A\//,"")
136
+ subdir = Apipie.configuration.doc_base_url.sub(%r{\A/},"")
137
137
  subdir_levels = subdir.split('/').length
138
138
  subdir_traversal_prefix = '../' * subdir_levels
139
139
  file_base = File.join(cache_dir, Apipie.configuration.doc_base_url)
@@ -159,7 +159,7 @@ namespace :apipie do
159
159
  end
160
160
  end
161
161
  end
162
- puts "#{Time.now} | Finished"
162
+ puts "#{Time.zone.now} | Finished"
163
163
  end
164
164
 
165
165
  # Attempt to use the Rails application views, otherwise default to built in views
@@ -195,10 +195,15 @@ namespace :apipie do
195
195
  end
196
196
 
197
197
  def generate_swagger_using_args(args, out)
198
- args.with_defaults(:version => Apipie.configuration.default_version,
199
- :swagger_content_type_input => Apipie.configuration.swagger_content_type_input || :form_data,
200
- :filename_suffix => nil)
201
- Apipie.configuration.swagger_content_type_input = args[:swagger_content_type_input].to_sym
198
+ args.with_defaults(
199
+ version: Apipie.configuration.default_version,
200
+ swagger_content_type_input: Apipie.configuration.generator.swagger.content_type_input,
201
+ filename_suffix: nil
202
+ )
203
+
204
+ Apipie.configuration.generator.swagger.content_type_input =
205
+ args[:swagger_content_type_input].to_sym
206
+
202
207
  count = 0
203
208
 
204
209
  sfx = args[:filename_suffix] || "_#{args[:swagger_content_type_input]}"
@@ -221,7 +226,7 @@ namespace :apipie do
221
226
  File.open("#{file_base}/#{filename}", 'w') { |file| file.write(JSON.pretty_generate(doc)) }
222
227
  end
223
228
 
224
- def generate_swagger_json_page(file_base, doc, sfx="", lang = nil)
229
+ def generate_swagger_json_page(file_base, doc, sfx = "", lang = nil)
225
230
  FileUtils.mkdir_p(file_base) unless File.exist?(file_base)
226
231
 
227
232
  path = Pathname.new("#{file_base}/schema_swagger#{sfx}#{lang_ext(lang)}.json")
@@ -254,11 +259,11 @@ namespace :apipie do
254
259
  end
255
260
 
256
261
  def generate_resource_pages(version, file_base, doc, include_json = false, lang = nil)
257
- doc[:docs][:resources].each do |resource_name, _|
258
- resource_file_base = File.join(file_base, resource_name.to_s)
262
+ doc[:docs][:resources].each do |resource_id, _|
263
+ resource_file_base = File.join(file_base, resource_id.to_s)
259
264
  FileUtils.mkdir_p(File.dirname(resource_file_base)) unless File.exist?(File.dirname(resource_file_base))
260
265
 
261
- doc = Apipie.to_json(version, resource_name, nil, lang)
266
+ doc = Apipie.to_json(version, resource_id, nil, lang)
262
267
  doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
263
268
  render_page("#{resource_file_base}#{lang_ext(lang)}.html", "resource", {:doc => doc[:docs],
264
269
  :resource => doc[:docs][:resources].first, :language => lang, :languages => Apipie.configuration.languages})
@@ -267,12 +272,12 @@ namespace :apipie do
267
272
  end
268
273
 
269
274
  def generate_method_pages(version, file_base, doc, include_json = false, lang = nil)
270
- doc[:docs][:resources].each do |resource_name, resource_params|
275
+ doc[:docs][:resources].each do |resource_id, resource_params|
271
276
  resource_params[:methods].each do |method|
272
- method_file_base = File.join(file_base, resource_name.to_s, method[:name].to_s)
277
+ method_file_base = File.join(file_base, resource_id.to_s, method[:name].to_s)
273
278
  FileUtils.mkdir_p(File.dirname(method_file_base)) unless File.exist?(File.dirname(method_file_base))
274
279
 
275
- doc = Apipie.to_json(version, resource_name, method[:name], lang)
280
+ doc = Apipie.to_json(version, resource_id, method[:name], lang)
276
281
  doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
277
282
  render_page("#{method_file_base}#{lang_ext(lang)}.html", "method", {:doc => doc[:docs],
278
283
  :resource => doc[:docs][:resources].first,
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Api::V2::Nested::ResourcesController do
4
- describe "resource id" do
5
- subject { Apipie.get_resource_name(Api::V2::Nested::ResourcesController) }
4
+ describe '.get_resource_id' do
5
+ subject { Apipie.get_resource_id(Api::V2::Nested::ResourcesController) }
6
6
 
7
7
  it "should have resource_id set" do
8
8
  expect(subject).to eq("resource")
@@ -5,10 +5,6 @@ require 'apipie/rspec/response_validation_helper'
5
5
  require "json-schema"
6
6
 
7
7
  RSpec.describe PetsController, :type => :controller do
8
- before :each do
9
- Apipie.configuration.swagger_allow_additional_properties_in_response = false
10
- end
11
-
12
8
  it "does not raise error when rendered output matches the described response" do
13
9
  response = get :return_and_validate_expected_response, format: :json
14
10
  expect(response).to match_declared_responses
@@ -39,7 +35,7 @@ RSpec.describe PetsController, :type => :controller do
39
35
  expect(response).not_to match_declared_responses
40
36
  end
41
37
 
42
- it "raises error when a response has an extra property and 'swagger_allow_additional_properties_in_response' is false" do
38
+ it "raises error when a response has an extra property and 'swagger.allow_additional_properties_in_response' is false" do
43
39
  response = get :return_and_validate_extra_property, format: :json
44
40
  expect(response).not_to match_declared_responses
45
41
  end
@@ -50,26 +46,28 @@ RSpec.describe PetsController, :type => :controller do
50
46
  expect(response).not_to match_declared_responses
51
47
  end
52
48
 
53
- it "does not raise error when a response has an extra property and 'swagger_allow_additional_properties_in_response' is true" do
54
- Apipie.configuration.swagger_allow_additional_properties_in_response = true
55
- response = get :return_and_validate_extra_property, format: :json
56
- expect(response).to match_declared_responses
49
+ context "when allow_additional_properties_in_response is true'" do
50
+ before do
51
+ Apipie.configuration.generator.swagger.allow_additional_properties_in_response = true
52
+ end
53
+
54
+ it "does not raise error when a response has an extra property" do
55
+ response = get :return_and_validate_extra_property, format: :json
56
+ expect(response).to match_declared_responses
57
+ end
57
58
  end
58
59
 
59
60
  it "does not raise error when a response has an extra field and 'additional_properties' is specified in the response" do
60
- Apipie.configuration.swagger_allow_additional_properties_in_response = false
61
61
  response = get :return_and_validate_allowed_extra_property, format: :json
62
62
  expect(response).to match_declared_responses
63
63
  end
64
64
 
65
65
  it "raises error when a response sub-object has an extra field and 'additional_properties' is not specified on it, but specified on the top level of the response" do
66
- Apipie.configuration.swagger_allow_additional_properties_in_response = false
67
66
  response = get :sub_object_invalid_extra_property, format: :json
68
67
  expect(response).not_to match_declared_responses
69
68
  end
70
69
 
71
70
  it "does not raise error when a response sub-object has an extra field and 'additional_properties' is specified on it" do
72
- Apipie.configuration.swagger_allow_additional_properties_in_response = false
73
71
  response = get :sub_object_allowed_extra_property, format: :json
74
72
  expect(response).to match_declared_responses
75
73
  end
@@ -86,7 +84,6 @@ RSpec.describe PetsController, :type => :controller do
86
84
 
87
85
  end
88
86
 
89
-
90
87
  describe "with array field" do
91
88
  it "no error for valid response" do
92
89
  response = get :returns_response_with_valid_array, format: :json
@@ -98,7 +95,4 @@ RSpec.describe PetsController, :type => :controller do
98
95
  expect(response).not_to match_declared_responses
99
96
  end
100
97
  end
101
-
102
-
103
-
104
98
  end
@@ -412,7 +412,7 @@ describe UsersController do
412
412
  expect(b.resource._id).to eq('users')
413
413
 
414
414
  expect(b.apis.count).to eq(1)
415
- expect(b.formats).to eq(['json', 'jsonp'])
415
+ expect(b.formats).to eq(%w[json jsonp])
416
416
  api = b.apis.first
417
417
  expect(api.short_description).to eq("Show user profile")
418
418
  expect(api.path).to eq("/users/:id")
@@ -708,7 +708,7 @@ EOS2
708
708
  param = a.params[:array_param]
709
709
  expect(param.desc).to eq("\n<p>array validator</p>\n")
710
710
  expect(param.validator.class).to be(Apipie::Validator::EnumValidator)
711
- expect(param.validator.instance_variable_get("@array")).to eq(["100", "one", "two", "1", "2"])
711
+ expect(param.validator.instance_variable_get("@array")).to eq(%w[100 one two 1 2])
712
712
 
713
713
  param = a.params[:proc_param]
714
714
  expect(param.desc).to eq("\n<p>proc validator</p>\n")
@@ -24,7 +24,7 @@ class PetsController < ApplicationController
24
24
  api :GET, "/pets/:id/as_properties", "Get a pet record"
25
25
  returns :code => 200 do
26
26
  property :pet_name, String, :desc => "Name of pet", :required => false
27
- property :animal_type, ['dog','cat','iguana','kangaroo'], :desc => "Type of pet" # required by default, because this is a 'property'
27
+ property :animal_type, %w[dog cat iguana kangaroo], :desc => "Type of pet" # required by default, because this is a 'property'
28
28
  end
29
29
  returns :code => 404 do
30
30
  property :another_error_message, String, :desc => "Overriding the response description from the Pets resource"
@@ -40,7 +40,7 @@ class PetsController < ApplicationController
40
40
  #-----------------------------------------------------------
41
41
  def_param_group :pet do
42
42
  property :pet_name, String, :desc => "Name of pet", :required => false
43
- property :animal_type, ['dog','cat','iguana','kangaroo'], :desc => "Type of pet" # required by default, because this is a 'property'
43
+ property :animal_type, %w[dog cat iguana kangaroo], :desc => "Type of pet" # required by default, because this is a 'property'
44
44
  end
45
45
 
46
46
  api :GET, "/pets/:id/as_param_group_of_properties", "Get a pet record"
@@ -70,7 +70,7 @@ class PetsController < ApplicationController
70
70
  param :pet_id, Integer, :desc => "id of pet", :required => true
71
71
  param :pet_name, String, :desc => "Name of pet", :required => false, :only_in => :response
72
72
  param :partial_match_allowed, [true, false], :desc => "Partial match allowed?", :required => false, :only_in => :request
73
- property :animal_type, ['dog','cat','iguana','kangaroo'], :desc => "Type of pet" # this is implicitly :only_in => :response
73
+ property :animal_type, %w[dog cat iguana kangaroo], :desc => "Type of pet" # this is implicitly :only_in => :response
74
74
  end
75
75
 
76
76
  api :GET, "/pets/pet_by_id", "Get a pet record with the pet id in the body of the request"
@@ -142,7 +142,7 @@ class PetsController < ApplicationController
142
142
  end
143
143
  returns :code => 204 do
144
144
  property :int_array, :array_of => Integer
145
- property :enum_array, :array_of => ['v1','v2','v3']
145
+ property :enum_array, :array_of => %w[v1 v2 v3]
146
146
  end
147
147
  returns :code => :unprocessable_entity, :desc => "Fleas were discovered on the pet" do
148
148
  param_group :pet
@@ -14,7 +14,7 @@ class Pet
14
14
  def self.describe_own_properties
15
15
  [
16
16
  Apipie::prop(:pet_name, 'string', {:description => 'Name of pet', :required => false}),
17
- Apipie::prop(:animal_type, 'string', {:description => 'Type of pet', :values => ["dog", "cat", "iguana", "kangaroo"]}),
17
+ Apipie::prop(:animal_type, 'string', {:description => 'Type of pet', :values => %w[dog cat iguana kangaroo]}),
18
18
  Apipie::additional_properties(false)
19
19
  ]
20
20
  end
@@ -27,7 +27,7 @@ class PetWithMeasurements
27
27
  def self.describe_own_properties
28
28
  [
29
29
  Apipie::prop(:pet_name, 'string', {:description => 'Name of pet', :required => false}),
30
- Apipie::prop('animal_type', 'string', {:description => 'Type of pet', :values => ["dog", "cat", "iguana", "kangaroo"]}),
30
+ Apipie::prop('animal_type', 'string', {:description => 'Type of pet', :values => %w[dog cat iguana kangaroo]}),
31
31
  Apipie::prop(:pet_measurements, 'object', {}, [
32
32
  Apipie::prop(:weight, 'number', {:description => "Weight in pounds" }),
33
33
  Apipie::prop(:height, 'number', {:description => "Height in inches" }),
@@ -31,7 +31,7 @@ class TwitterExampleController < ApplicationController
31
31
 
32
32
  api :GET, '/twitter_example/profile_image/:screen_name', 'Access the profile image in various sizes for the user with the indicated screen_name.'
33
33
  param :screen_name, String, :required => true, :desc => 'The screen name of the user for whom to return results for. Helpful for disambiguating when a valid screen name is also a user ID.'
34
- param :size, ['bigger','normal','mini','original'], :desc => <<-EOS
34
+ param :size, %w[bigger normal mini original], :desc => <<-EOS
35
35
  Specifies the size of image to fetch. Not specifying a size will give the default, normal size of 48px by 48px. Valid options include:
36
36
 
37
37
  * bigger - 73px by 73px
@@ -166,7 +166,7 @@ class TwitterExampleController < ApplicationController
166
166
  param :user_id, Integer, :desc => 'The ID of the user for whom to return results for. Helpful for disambiguating when a valid user ID is also a valid screen name.'
167
167
  param :screen_name, String, :desc => 'The screen name of the user for whom to return results for. Helpful for disambiguating when a valid screen name is also a user ID.'
168
168
  param :include_entities, String
169
- param :skip_status, ['t','true','1'],
169
+ param :skip_status, %w[t true 1],
170
170
  :description => 'When set to either true, t or 1 statuses will not be included in the returned user objects.'
171
171
 
172
172
  description "Look at examples."
@@ -170,14 +170,14 @@ class UsersController < ApplicationController
170
170
  eos
171
171
  api :GET, "/users/:id", "Show user profile"
172
172
  show false
173
- formats ['json', 'jsonp']
173
+ formats %w[json jsonp]
174
174
  error 401, "Unauthorized"
175
175
  error :code => 404, :description => "Not Found"
176
176
  param :id, Integer, :desc => "user id", :required => true
177
177
  param :session, String, :desc => "user is logged in", :required => true, :missing_message => lambda { "session_parameter_is_required" }
178
178
  param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
179
179
  param :regexp2, /\b[A-Z0-9._%+-=]+@[A-Z0-9.-]+.[A-Z]{2,}\b/i, :desc => "email regexp"
180
- param :array_param, ["100", "one", "two", "1", "2"], :desc => "array validator"
180
+ param :array_param, %w[100 one two 1 2], :desc => "array validator"
181
181
  param :boolean_param, [true, false], :desc => "array validator with boolean"
182
182
  param :proc_param, lambda { |val|
183
183
  val == "param value" ? true : "The only good value is 'param value'."
@@ -211,7 +211,7 @@ class UsersController < ApplicationController
211
211
  def_param_group :user do
212
212
  param :user, Hash, :desc => "User info", :required => true, :action_aware => true do
213
213
  param_group :credentials
214
- param :membership, ["standard","premium"], :desc => "User membership", :allow_nil => false
214
+ param :membership, %w[standard premium], :desc => "User membership", :allow_nil => false
215
215
  end
216
216
  end
217
217
 
@@ -269,13 +269,13 @@ class UsersController < ApplicationController
269
269
  end
270
270
 
271
271
  api :GET, '/users/by_department', 'show users from a specific department'
272
- param :department, ["finance", "operations", "sales", "marketing", "HR"], required: false, default_value: "sales"
272
+ param :department, %w[finance operations sales marketing HR], required: false, default_value: "sales"
273
273
  def get_by_department
274
274
  render :plain => 'nothing to see here'
275
275
  end
276
276
 
277
277
  api :GET, '/users/in_departments', 'show users from specific departments'
278
- param :departments, Array, in: ["finance", "operations", "sales", "marketing", "HR"], default_value: ['sales']
278
+ param :departments, Array, in: %w[finance operations sales marketing HR], default_value: ['sales']
279
279
  def get_in_departments
280
280
  render :plain => 'nothing to see here'
281
281
  end
data/spec/dummy/config.ru CHANGED
@@ -1,4 +1,4 @@
1
1
  # This file is used by Rack-based servers to start the application.
2
2
 
3
- require ::File.expand_path('config/environment', __dir__)
3
+ require File.expand_path('config/environment', __dir__)
4
4
  run Dummy::Application
@@ -209,6 +209,7 @@ describe Apipie::ApipiesController, type: :controller do
209
209
  get :index
210
210
  expect(test).to eq(true)
211
211
  end
212
+
212
213
  it "remove all resources" do
213
214
  Apipie.configuration.authorize = Proc.new do |&args|
214
215
  false
@@ -216,6 +217,7 @@ describe Apipie::ApipiesController, type: :controller do
216
217
  get :index
217
218
  expect(assigns(:doc)[:resources]).to eq({})
218
219
  end
220
+
219
221
  it "remove all methods" do
220
222
  Apipie.configuration.authorize = Proc.new do |controller, method, doc|
221
223
  !method
@@ -225,6 +227,7 @@ describe Apipie::ApipiesController, type: :controller do
225
227
  expect(assigns(:doc)[:resources]["twitter_example"][:methods]).to eq([])
226
228
  expect(assigns(:doc)[:resources]["users"][:methods]).to eq([])
227
229
  end
230
+
228
231
  it "remove specific method" do
229
232
  Apipie.configuration.authorize = nil
230
233
  get :index
@@ -241,6 +244,7 @@ describe Apipie::ApipiesController, type: :controller do
241
244
  expect(assigns(:doc)[:resources]["users"][:methods].size).to eq(users_methods - 1)
242
245
  expect(assigns(:doc)[:resources]["twitter_example"][:methods].size).to eq(twitter_example_methods)
243
246
  end
247
+
244
248
  it "does not allow access to swagger when authorization is set" do
245
249
  get :index, :params => { :format => "json", :type => "swagger"}
246
250
 
@@ -16,37 +16,47 @@ describe Apipie::Application do
16
16
 
17
17
  end
18
18
 
19
- describe "get_resource_name" do
20
- subject {Apipie.get_resource_name(Api::V2::Nested::ArchitecturesController)}
19
+ shared_examples 'resource id' do
20
+ let(:resource_class) { Api::V2::Nested::ArchitecturesController }
21
+ let(:base_url) { '/some-api' }
22
+
23
+ before { allow(described_class).to receive(:get_base_url).and_return(base_url) }
21
24
 
22
25
  context "with namespaced_resources enabled" do
23
26
  before { Apipie.configuration.namespaced_resources = true }
24
- context "with a defined base url" do
25
-
26
- it "should not overwrite the parent resource" do
27
- is_expected.not_to eq(Apipie.get_resource_name(Api::V2::ArchitecturesController))
28
- end
27
+ after { Apipie.configuration.namespaced_resources = false }
29
28
 
29
+ it "returns the namespaces" do
30
+ is_expected.to eq('api-v2-nested-architectures')
30
31
  end
31
32
 
32
33
  context "with an undefined base url" do
33
- before {allow(Apipie.app).to receive(:get_base_url).and_return(nil)}
34
+ let(:base_url) { nil }
34
35
 
35
36
  it "should not raise an error" do
36
- expect { Apipie.get_resource_name(Api::V2::ArchitecturesController) }.
37
- not_to raise_error
37
+ expect { method_call }.not_to raise_error
38
38
  end
39
39
  end
40
-
41
- after { Apipie.configuration.namespaced_resources = false }
42
40
  end
43
41
 
44
- context "with namespaced_resources enabled" do
42
+ context "with namespaced_resources disabled" do
45
43
  before { Apipie.configuration.namespaced_resources = false }
46
44
 
47
- it "should overwrite the the parent" do
48
- is_expected.to eq(Apipie.get_resource_name(Api::V2::ArchitecturesController))
45
+ it "returns the controller name" do
46
+ is_expected.to eq('architectures')
49
47
  end
50
48
  end
51
49
  end
50
+
51
+ describe '.get_resource_id' do
52
+ subject(:method_call) { Apipie.get_resource_id(resource_class) }
53
+
54
+ it_behaves_like 'resource id'
55
+ end
56
+
57
+ describe '.get_resource_name' do
58
+ subject(:method_call) { Apipie.get_resource_name(resource_class) }
59
+
60
+ it_behaves_like 'resource id'
61
+ end
52
62
  end
@@ -20,4 +20,19 @@ describe 'Apipie::Configuration' do
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ describe 'generator configuration' do
25
+ let(:generator_config) { configuration.generator }
26
+
27
+ describe '#swagger' do
28
+ subject(:setter) { generator_config.swagger.include_warning_tags = true }
29
+
30
+ it 'assigns the correct value' do
31
+ expect { setter }
32
+ .to change(configuration.generator.swagger, :include_warning_tags?)
33
+ .from(false)
34
+ .to(true)
35
+ end
36
+ end
37
+ end
23
38
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::Config do
4
+ let(:config) { described_class.clone.instance }
5
+
6
+ before { Singleton.__init__(described_class) }
7
+
8
+ describe 'deprecation warnings' do
9
+ context 'when a deprecated method is call' do
10
+ subject(:deprecated_method_call) { config.swagger_include_warning_tags? }
11
+
12
+ it 'returns a deprecations warning' do
13
+ expect { deprecated_method_call }
14
+ .to output(/DEPRECATION WARNING: config.swagger_include_warning_tags is deprecated/)
15
+ .to_stderr
16
+ end
17
+ end
18
+ end
19
+ end
@@ -4,18 +4,23 @@ describe Apipie::Generator::Swagger::Context do
4
4
  let(:allow_null) { true }
5
5
  let(:in_schema) { true }
6
6
  let(:http_method) { 'get' }
7
+ let(:prefix) { nil }
7
8
 
8
- subject do
9
+ let(:context_instance) do
9
10
  described_class.new(
10
11
  allow_null: allow_null,
11
12
  http_method: http_method,
12
13
  in_schema: in_schema,
13
- controller_method: 'show'
14
+ controller_method: 'show',
15
+ prefix: prefix
14
16
  )
15
17
  end
16
18
 
17
19
  describe '#in_schema?' do
20
+ subject { context_instance }
21
+
18
22
  it { is_expected.to be_in_schema }
23
+
19
24
  context 'when in_schema is false' do
20
25
  let(:in_schema) { false }
21
26
 
@@ -24,6 +29,8 @@ describe Apipie::Generator::Swagger::Context do
24
29
  end
25
30
 
26
31
  describe '#allow_null?' do
32
+ subject { context_instance }
33
+
27
34
  it { is_expected.to be_allow_null }
28
35
 
29
36
  context 'when allow_null is false' do
@@ -32,4 +39,18 @@ describe Apipie::Generator::Swagger::Context do
32
39
  it { is_expected.not_to be_allow_null }
33
40
  end
34
41
  end
42
+
43
+ describe '#add_to_prefix!' do
44
+ before { context_instance.add_to_prefix!('some-prefix') }
45
+
46
+ subject { context_instance.prefix }
47
+
48
+ it { is_expected.to eq('some-prefix') }
49
+
50
+ context 'when context has already a prefix' do
51
+ let(:prefix) { 'existing-prefix' }
52
+
53
+ it { is_expected.to eq('existing-prefix[some-prefix]') }
54
+ end
55
+ end
35
56
  end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::MethodDescription::ApiSchemaService do
4
+ let(:path) { '/api' }
5
+ let(:http_method) { 'get' }
6
+ let(:resource_id) { 'users' }
7
+ let(:method_description_description) { nil }
8
+ let(:tags) { [] }
9
+
10
+ let(:dsl_data) do
11
+ ActionController::Base
12
+ .send(:_apipie_dsl_data_init)
13
+ .merge(
14
+ {
15
+ description: method_description_description,
16
+ api_args: [[http_method, path, 'Some api description', { deprecated: true }]],
17
+ tag_list: tags
18
+ }
19
+ )
20
+ end
21
+
22
+ let(:resource_desc) do
23
+ Apipie::ResourceDescription.new(UsersController, resource_id)
24
+ end
25
+
26
+ let(:method_description) do
27
+ Apipie::Generator::Swagger::MethodDescription::Decorator.new(
28
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
29
+ )
30
+ end
31
+
32
+ let(:service) { described_class.new(method_description) }
33
+
34
+ describe '#call' do
35
+ subject(:schema) { service.call }
36
+
37
+ it 'returns the path' do
38
+ expect(schema).to include(path)
39
+ end
40
+
41
+ it 'returns the http method' do
42
+ expect(schema[path]).to include(http_method)
43
+ end
44
+
45
+ it 'returns the correct attributes' do
46
+ expect(schema[path][http_method].keys).to include(
47
+ :tags,
48
+ :consumes,
49
+ :operationId,
50
+ :parameters,
51
+ :responses
52
+ )
53
+ end
54
+ end
55
+
56
+ describe 'tags' do
57
+ subject { service.call[path][http_method][:tags] }
58
+
59
+ it { is_expected.to eq([resource_id]) }
60
+
61
+ context 'when tags are available' do
62
+ let(:tags) { ['Tag 1', 'Tag 2'] }
63
+
64
+ it { is_expected.to include(*tags) }
65
+ end
66
+
67
+ context 'when Apipie.configuration.generator.swagger.include_warning_tags is enabled' do
68
+ before { Apipie.configuration.generator.swagger.include_warning_tags = true }
69
+
70
+ context 'when warnings are issued' do
71
+ before do
72
+ Apipie::Generator::Swagger::Warning.for_code(
73
+ Apipie::Generator::Swagger::Warning::MISSING_METHOD_SUMMARY_CODE,
74
+ 'some_method'
75
+ ).warn_through_writer
76
+ end
77
+
78
+ it { is_expected.to include('warnings issued') }
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'consumes' do
84
+ subject { service.call[path]['get'][:consumes] }
85
+
86
+ it { is_expected.to eq(['application/x-www-form-urlencoded', 'multipart/form-data']) }
87
+
88
+ context 'when content type input is json' do
89
+ before { Apipie.configuration.generator.swagger.content_type_input = :json }
90
+
91
+ it { is_expected.to eq(['application/json']) }
92
+ end
93
+ end
94
+
95
+ describe 'description' do
96
+ subject { service.call[path]['get'][:description] }
97
+
98
+ it { is_expected.to be_blank }
99
+
100
+ context 'when description for method description exists' do
101
+ let(:method_description_description) { 'Some description' }
102
+
103
+ it { is_expected.to eq(method_description_description) }
104
+ end
105
+ end
106
+ end