apipie-rails 0.9.2 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rubocop-challenger.yml +1 -1
  3. data/.github/workflows/rubocop.yml +20 -0
  4. data/.rubocop.yml +21 -0
  5. data/.rubocop_todo.yml +222 -610
  6. data/CHANGELOG.md +16 -1
  7. data/README.rst +20 -0
  8. data/apipie-rails.gemspec +4 -0
  9. data/app/controllers/apipie/apipies_controller.rb +2 -2
  10. data/app/helpers/apipie_helper.rb +1 -1
  11. data/app/views/apipie/apipies/_deprecation.html.erb +16 -0
  12. data/app/views/apipie/apipies/_params.html.erb +7 -1
  13. data/config/locales/en.yml +7 -0
  14. data/lib/apipie/apipie_module.rb +2 -2
  15. data/lib/apipie/application.rb +16 -8
  16. data/lib/apipie/configuration.rb +13 -2
  17. data/lib/apipie/dsl_definition.rb +6 -6
  18. data/lib/apipie/error_description.rb +1 -1
  19. data/lib/apipie/errors.rb +2 -16
  20. data/lib/apipie/extractor/collector.rb +1 -1
  21. data/lib/apipie/extractor/recorder.rb +2 -2
  22. data/lib/apipie/extractor.rb +2 -2
  23. data/lib/apipie/generator/swagger/operation_id.rb +1 -1
  24. data/lib/apipie/helpers.rb +3 -3
  25. data/lib/apipie/markup.rb +9 -8
  26. data/lib/apipie/method_description.rb +1 -1
  27. data/lib/apipie/param_description/deprecation.rb +24 -0
  28. data/lib/apipie/param_description.rb +38 -11
  29. data/lib/apipie/resource_description.rb +10 -7
  30. data/lib/apipie/response_description.rb +1 -1
  31. data/lib/apipie/response_description_adapter.rb +3 -3
  32. data/lib/apipie/routing.rb +1 -1
  33. data/lib/apipie/rspec/response_validation_helper.rb +1 -1
  34. data/lib/apipie/swagger_generator.rb +6 -6
  35. data/lib/apipie/validator.rb +9 -10
  36. data/lib/apipie/version.rb +1 -1
  37. data/lib/apipie-rails.rb +1 -0
  38. data/lib/tasks/apipie.rake +11 -10
  39. data/spec/controllers/users_controller_spec.rb +8 -1
  40. data/spec/dummy/config.ru +1 -1
  41. data/spec/{controllers → lib/apipie}/apipies_controller_spec.rb +6 -2
  42. data/spec/lib/apipie/application_spec.rb +53 -0
  43. data/spec/lib/apipie/configuration_spec.rb +23 -0
  44. data/spec/lib/apipie/extractor/recorder_spec.rb +40 -0
  45. data/spec/lib/{generator → apipie/generator}/swagger/context_spec.rb +1 -0
  46. data/spec/lib/{method_description_spec.rb → apipie/method_description_spec.rb} +4 -4
  47. data/spec/lib/apipie/no_documented_method_spec.rb +17 -0
  48. data/spec/lib/apipie/param_description/deprecation_spec.rb +31 -0
  49. data/spec/lib/{param_description_spec.rb → apipie/param_description_spec.rb} +81 -1
  50. data/spec/lib/apipie/resource_description_spec.rb +91 -0
  51. data/spec/lib/apipie/response_does_not_match_swagger_schema_spec.rb +35 -0
  52. data/spec/lib/rake_spec.rb +1 -1
  53. data/spec/lib/swagger/rake_swagger_spec.rb +2 -2
  54. data/spec/lib/swagger/swagger_dsl_spec.rb +11 -5
  55. data/spec/spec_helper.rb +3 -3
  56. metadata +79 -29
  57. data/spec/lib/application_spec.rb +0 -49
  58. data/spec/lib/resource_description_spec.rb +0 -48
  59. /data/spec/{lib/swagger/response_validation_spec.rb → controllers/pets_controller_spec.rb} +0 -0
  60. /data/spec/lib/{extractor → apipie/extractor/recorder}/middleware_spec.rb +0 -0
  61. /data/spec/lib/{extractor → apipie/extractor}/writer_spec.rb +0 -0
  62. /data/spec/lib/{extractor → apipie}/extractor_spec.rb +0 -0
  63. /data/spec/lib/{file_handler_spec.rb → apipie/file_handler_spec.rb} +0 -0
  64. /data/spec/lib/{generator → apipie/generator}/swagger/operation_id_spec.rb +0 -0
  65. /data/spec/lib/{generator → apipie/generator}/swagger/param_description/builder_spec.rb +0 -0
  66. /data/spec/lib/{generator → apipie/generator}/swagger/param_description/composite_spec.rb +0 -0
  67. /data/spec/lib/{generator → apipie/generator}/swagger/param_description/description_spec.rb +0 -0
  68. /data/spec/lib/{generator → apipie/generator}/swagger/param_description/in_spec.rb +0 -0
  69. /data/spec/lib/{generator → apipie/generator}/swagger/param_description/name_spec.rb +0 -0
  70. /data/spec/lib/{generator → apipie/generator}/swagger/param_description/type_spec.rb +0 -0
  71. /data/spec/lib/{generator → apipie/generator}/swagger/param_description_spec.rb +0 -0
  72. /data/spec/lib/{generator → apipie/generator}/swagger/type_extractor_spec.rb +0 -0
  73. /data/spec/lib/{generator → apipie/generator}/swagger/warning_spec.rb +0 -0
  74. /data/spec/lib/{generator → apipie/generator}/swagger/warning_writer_spec.rb +0 -0
  75. /data/spec/lib/{method_description → apipie/method_description}/apis_service_spec.rb +0 -0
  76. /data/spec/lib/{param_group_spec.rb → apipie/param_group_spec.rb} +0 -0
  77. /data/spec/lib/{validator_spec.rb → apipie/validator_spec.rb} +0 -0
  78. /data/spec/{controllers → test_engine}/memes_controller_spec.rb +0 -0
@@ -1,6 +1,6 @@
1
1
  module Apipie
2
2
 
3
- def self.prop(name, expected_type, options={}, sub_properties=[])
3
+ def self.prop(name, expected_type, options = {}, sub_properties = [])
4
4
  Apipie::ResponseDescriptionAdapter::PropDesc.new(name, expected_type, options, sub_properties)
5
5
  end
6
6
 
@@ -60,7 +60,7 @@ module Apipie
60
60
  return self.send(key) if self.respond_to?(key.to_s)
61
61
  end
62
62
 
63
- def initialize(expected_type, enum_values=nil, sub_properties=nil)
63
+ def initialize(expected_type, enum_values = nil, sub_properties = nil)
64
64
  @expected_type = expected_type
65
65
  @enum_values = enum_values
66
66
  @is_enum = !!enum_values
@@ -84,7 +84,7 @@ module Apipie
84
84
  #======================================================================
85
85
 
86
86
 
87
- def initialize(name, expected_type, options={}, sub_properties=[])
87
+ def initialize(name, expected_type, options = {}, sub_properties = [])
88
88
  @name = name
89
89
  @required = true
90
90
  @required = false if options[:required] == false
@@ -4,7 +4,7 @@ module Apipie
4
4
  def apipie(options = {})
5
5
  namespace "apipie", :path => Apipie.configuration.doc_base_url do
6
6
  get 'apipie_checksum', :to => "apipies#apipie_checksum", :format => "json"
7
- constraints(:version => /[^\/]+/, :resource => /[^\/]+/, :method => /[^\/]+/) do
7
+ constraints(:version => %r{[^/]+}, :resource => %r{[^/]+}, :method => %r{[^/]+}) do
8
8
  get(options.reverse_merge("(:version)/(:resource)/(:method)" => "apipies#index", :as => :apipie))
9
9
  end
10
10
  end
@@ -112,7 +112,7 @@ class ActionController::Base
112
112
  end
113
113
 
114
114
  module Apipie
115
- def self.print_validation_errors(validation_errors, schema, response, error_object=nil)
115
+ def self.print_validation_errors(validation_errors, schema, response, error_object = nil)
116
116
  Rails.logger.warn(validation_errors.to_s)
117
117
  if Rails.env.test?
118
118
  puts "schema validation errors:"
@@ -33,7 +33,7 @@ module Apipie
33
33
  end
34
34
 
35
35
 
36
- def generate_from_resources(version, resources, method_name, lang, clear_warnings=false)
36
+ def generate_from_resources(version, resources, method_name, lang, clear_warnings = false)
37
37
  init_swagger_vars(version, lang, clear_warnings)
38
38
 
39
39
  @only_method = method_name
@@ -48,7 +48,7 @@ module Apipie
48
48
  # Initialization
49
49
  #--------------------------------------------------------------------------
50
50
 
51
- def init_swagger_vars(version, lang, clear_warnings=false)
51
+ def init_swagger_vars(version, lang, clear_warnings = false)
52
52
 
53
53
  # docs = {
54
54
  # :name => Apipie.configuration.app_name,
@@ -284,7 +284,7 @@ module Apipie
284
284
 
285
285
  def swagger_path(str)
286
286
  str = str.gsub(/:(\w+)/, '{\1}')
287
- str = str.gsub(/\/$/, '')
287
+ str = str.gsub(%r{/$}, '')
288
288
 
289
289
  if str[0] != '/'
290
290
  warn_added_missing_slash(str)
@@ -339,7 +339,7 @@ module Apipie
339
339
  response_schema(adapter, allow_nulls)
340
340
  end
341
341
 
342
- def response_schema(response, allow_nulls=false)
342
+ def response_schema(response, allow_nulls = false)
343
343
  begin
344
344
  # no need to warn about "missing default value for optional param" when processing response definitions
345
345
  prev_value = @disable_default_value_warning
@@ -445,7 +445,7 @@ module Apipie
445
445
  "#/definitions/#{name}"
446
446
  end
447
447
 
448
- def gen_referenced_block_from_params_array(name, params_array, allow_nulls=false)
448
+ def gen_referenced_block_from_params_array(name, params_array, allow_nulls = false)
449
449
  return ref_to(:name) if @definitions.key(:name)
450
450
 
451
451
  schema_obj = Apipie::Generator::Swagger::ParamDescription::Composite.new(
@@ -531,7 +531,7 @@ module Apipie
531
531
  end
532
532
 
533
533
 
534
- def add_params_from_hash(swagger_params_array, param_defs, prefix=nil, default_value_for_in=nil)
534
+ def add_params_from_hash(swagger_params_array, param_defs, prefix = nil, default_value_for_in = nil)
535
535
  param_defs.each do |name, desc|
536
536
  if !prefix.nil?
537
537
  name = "#{prefix}[#{name}]"
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+
2
3
  module Apipie
3
4
 
4
5
  module Validator
@@ -189,7 +190,7 @@ module Apipie
189
190
 
190
191
  # arguments value must be an array
191
192
  class ArrayValidator < Apipie::Validator::BaseValidator
192
- def initialize(param_description, argument, options={})
193
+ def initialize(param_description, argument, options = {})
193
194
  super(param_description)
194
195
  @type = argument
195
196
  @items_type = options[:of]
@@ -344,14 +345,12 @@ module Apipie
344
345
 
345
346
  def validate(value)
346
347
  return false if !value.is_a? Hash
347
- if @hash_params
348
- @hash_params.each do |k, p|
349
- if Apipie.configuration.validate_presence?
350
- raise ParamMissing.new(p) if p.required && !value.key?(k)
351
- end
352
- if Apipie.configuration.validate_value?
353
- p.validate(value[k]) if value.key?(k)
354
- end
348
+ @hash_params&.each do |k, p|
349
+ if Apipie.configuration.validate_presence?
350
+ raise ParamMissing.new(p) if p.required && !value.key?(k)
351
+ end
352
+ if Apipie.configuration.validate_value?
353
+ p.validate(value[k]) if value.key?(k)
355
354
  end
356
355
  end
357
356
  return true
@@ -496,7 +495,7 @@ module Apipie
496
495
 
497
496
  def initialize(param_description, argument, param_group)
498
497
  super(param_description)
499
- @validator = Apipie::Validator:: HashValidator.new(param_description, argument, param_group)
498
+ @validator = Apipie::Validator::HashValidator.new(param_description, argument, param_group)
500
499
  @type = argument
501
500
  end
502
501
 
@@ -1,3 +1,3 @@
1
1
  module Apipie
2
- VERSION = "0.9.2"
2
+ VERSION = "0.9.4"
3
3
  end
data/lib/apipie-rails.rb CHANGED
@@ -12,6 +12,7 @@ require "apipie/configuration"
12
12
  require "apipie/method_description"
13
13
  require "apipie/resource_description"
14
14
  require "apipie/param_description"
15
+ require "apipie/param_description/deprecation"
15
16
  require "apipie/method_description/api"
16
17
  require "apipie/method_description/apis_service"
17
18
  require "apipie/errors"
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+
2
3
  require 'fileutils'
3
4
 
4
5
  namespace :apipie do
@@ -19,7 +20,7 @@ namespace :apipie do
19
20
  task :static, [:version] => :environment do |t, args|
20
21
  with_loaded_documentation do
21
22
  args.with_defaults(:version => Apipie.configuration.default_version)
22
- 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')
23
24
  subdir = File.basename(out)
24
25
  copy_jscss(out)
25
26
  Apipie.configuration.version_in_url = false
@@ -43,7 +44,7 @@ namespace :apipie do
43
44
  task :static_json, [:version] => :environment do |t, args|
44
45
  with_loaded_documentation do
45
46
  args.with_defaults(:version => Apipie.configuration.default_version)
46
- 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')
47
48
  ([nil] + Apipie.configuration.languages).each do |lang|
48
49
  doc = Apipie.to_json(args[:version], nil, nil, lang)
49
50
  generate_json_page(out, doc, lang)
@@ -54,7 +55,7 @@ namespace :apipie do
54
55
  desc "Generate static swagger json"
55
56
  task :static_swagger_json, [:version, :swagger_content_type_input, :filename_suffix] => :environment do |t, args|
56
57
  with_loaded_documentation do
57
- 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')
58
59
  generate_swagger_using_args(args, out)
59
60
  end
60
61
  end
@@ -67,7 +68,7 @@ namespace :apipie do
67
68
  desc "Did swagger output change since the last execution of this task?"
68
69
  task :did_swagger_change, [:version, :swagger_content_type_input, :filename_suffix] => :environment do |t, args|
69
70
  with_loaded_documentation do
70
- 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')
71
72
  paths = generate_swagger_using_args(args, out)
72
73
  paths.each {|path|
73
74
  existing_files_in_dir = Pathname(out).children(true)
@@ -122,17 +123,17 @@ namespace :apipie do
122
123
  # Default output dir ('public/apipie_cache') can be changed with OUT=/some/dir
123
124
  desc "Generate cache to avoid production dependencies on markup languages"
124
125
  task :cache => :environment do
125
- puts "#{Time.now} | Started"
126
+ puts "#{Time.zone.now} | Started"
126
127
  cache_part = ENV['cache_part']
127
128
  generate_index = (cache_part == 'resources' ? false : true)
128
129
  generate_resources = (cache_part == 'index' ? false : true)
129
130
  with_loaded_documentation do
130
- puts "#{Time.now} | Documents loaded..."
131
+ puts "#{Time.zone.now} | Documents loaded..."
131
132
  ([nil] + Apipie.configuration.languages).each do |lang|
132
133
  I18n.locale = lang || Apipie.configuration.default_locale
133
- puts "#{Time.now} | Processing docs for #{lang}"
134
+ puts "#{Time.zone.now} | Processing docs for #{lang}"
134
135
  cache_dir = ENV["OUT"] || Apipie.configuration.cache_dir
135
- subdir = Apipie.configuration.doc_base_url.sub(/\A\//,"")
136
+ subdir = Apipie.configuration.doc_base_url.sub(%r{\A/},"")
136
137
  subdir_levels = subdir.split('/').length
137
138
  subdir_traversal_prefix = '../' * subdir_levels
138
139
  file_base = File.join(cache_dir, Apipie.configuration.doc_base_url)
@@ -158,7 +159,7 @@ namespace :apipie do
158
159
  end
159
160
  end
160
161
  end
161
- puts "#{Time.now} | Finished"
162
+ puts "#{Time.zone.now} | Finished"
162
163
  end
163
164
 
164
165
  # Attempt to use the Rails application views, otherwise default to built in views
@@ -220,7 +221,7 @@ namespace :apipie do
220
221
  File.open("#{file_base}/#{filename}", 'w') { |file| file.write(JSON.pretty_generate(doc)) }
221
222
  end
222
223
 
223
- def generate_swagger_json_page(file_base, doc, sfx="", lang = nil)
224
+ def generate_swagger_json_page(file_base, doc, sfx = "", lang = nil)
224
225
  FileUtils.mkdir_p(file_base) unless File.exist?(file_base)
225
226
 
226
227
  path = Pathname.new("#{file_base}/schema_swagger#{sfx}#{lang_ext(lang)}.json")
@@ -269,6 +269,7 @@ describe UsersController do
269
269
  :allow_blank => false,
270
270
  :metadata => nil,
271
271
  :show => true,
272
+ :deprecated => false,
272
273
  :expected_type => "hash",
273
274
  :validations => [])
274
275
  end
@@ -585,6 +586,7 @@ describe UsersController do
585
586
  :description=>"\n<p>Authorization</p>\n",
586
587
  :name=>"oauth",
587
588
  :show=>true,
589
+ :deprecated=>false,
588
590
  :expected_type=>"string"},
589
591
  {:validator=>"Must be a Hash",
590
592
  :description=>"\n<p>Deprecated parameter not documented</p>\n",
@@ -595,15 +597,17 @@ describe UsersController do
595
597
  :required=>false,
596
598
  :full_name=>"legacy_param",
597
599
  :show=>false,
600
+ :deprecated=>false,
598
601
  :params=>
599
602
  [{:validator=>"Must be a Hash",
600
603
  :description=>"\n<p>Param description for all methods</p>\n",
601
604
  :expected_type=>"hash",
602
605
  :allow_nil=>false,
603
- :allow_blank => false,
606
+ :allow_blank => false,
604
607
  :name=>"resource_param",
605
608
  :required=>false,
606
609
  :full_name=>"resource_param",
610
+ :deprecated=>false,
607
611
  :show=>true,
608
612
  :params=>
609
613
  [{:required=>true,
@@ -613,6 +617,7 @@ describe UsersController do
613
617
  :description=>"\n<p>Username for login</p>\n",
614
618
  :name=>"ausername", :full_name=>"resource_param[ausername]",
615
619
  :show=>true,
620
+ :deprecated=>false,
616
621
  :expected_type=>"string"},
617
622
  {:required=>true,
618
623
  :allow_nil => false,
@@ -621,6 +626,7 @@ describe UsersController do
621
626
  :description=>"\n<p>Password for login</p>\n",
622
627
  :name=>"apassword", :full_name=>"resource_param[apassword]",
623
628
  :show=>true,
629
+ :deprecated=>false,
624
630
  :expected_type=>"string"}
625
631
  ]}
626
632
  ]
@@ -631,6 +637,7 @@ describe UsersController do
631
637
  :description=>"\n<p>Company ID</p>\n",
632
638
  :name=>"id", :full_name=>"id",
633
639
  :show=>true,
640
+ :deprecated=>false,
634
641
  :expected_type=>"numeric"},
635
642
  ],
636
643
  :name => 'two_urls',
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
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'fileutils'
3
3
  require "json-schema"
4
4
 
5
- describe Apipie::ApipiesController do
5
+ describe Apipie::ApipiesController, type: :controller do
6
6
 
7
7
  describe "GET index" do
8
8
 
@@ -159,7 +159,7 @@ describe Apipie::ApipiesController do
159
159
  describe "GET index as swagger" do
160
160
 
161
161
  let(:swagger_schema) do
162
- File.read(File.join(File.dirname(__FILE__),"../lib/swagger/openapi_2_0_schema.json"))
162
+ File.read("#{Rails.root.parent}/lib/swagger/openapi_2_0_schema.json")
163
163
  end
164
164
 
165
165
  it "outputs swagger when format is json and type is swagger" do
@@ -209,6 +209,7 @@ describe Apipie::ApipiesController 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 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 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 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
 
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+
3
+ describe Apipie::Application do
4
+
5
+ describe "api_controllers_paths" do
6
+ before do
7
+ Apipie.configuration.api_controllers_matcher = [File.join(Rails.root, "app", "controllers", "**","*.rb"),
8
+ File.join(Rails.root, "lib", "**","*.rb")]
9
+ end
10
+
11
+ it "should support receiving array as parameter" do
12
+ expect { Apipie.api_controllers_paths}.
13
+ not_to raise_error
14
+ end
15
+
16
+
17
+ end
18
+
19
+ describe '.get_resource_name' do
20
+ subject(:get_resource_name) do
21
+ Apipie.get_resource_name(Api::V2::Nested::ArchitecturesController)
22
+ end
23
+
24
+ let(:base_url) { '/some-api' }
25
+
26
+ before { allow(Apipie.app).to receive(:get_base_url).and_return(base_url) }
27
+
28
+ context "with namespaced_resources enabled" do
29
+ before { Apipie.configuration.namespaced_resources = true }
30
+ after { Apipie.configuration.namespaced_resources = false }
31
+
32
+ it "returns the namespaces" do
33
+ is_expected.to eq('api-v2-nested-architectures')
34
+ end
35
+
36
+ context "with an undefined base url" do
37
+ let(:base_url) { nil }
38
+
39
+ it "should not raise an error" do
40
+ expect { get_resource_name }.not_to raise_error
41
+ end
42
+ end
43
+ end
44
+
45
+ context "with namespaced_resources disabled" do
46
+ before { Apipie.configuration.namespaced_resources = false }
47
+
48
+ it "returns the controller name" do
49
+ is_expected.to eq('architectures')
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Apipie::Configuration' do
6
+ let(:configuration) { Apipie::Configuration.new }
7
+
8
+ describe '#api_action_matcher=' do
9
+ subject(:setter) { configuration.api_action_matcher = matcher }
10
+
11
+ let(:matcher) { proc { |_| :some_action } }
12
+
13
+ it { is_expected.to eq(matcher) }
14
+
15
+ context 'when matcher does not implement .call method' do
16
+ let(:matcher) { 'I do not implement .call' }
17
+
18
+ it 'raises and exception' do
19
+ expect { setter }.to raise_error('Must implement .call method')
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Apipie::Extractor::Recorder' do
6
+ let(:recorder) { Apipie::Extractor::Recorder.new }
7
+
8
+ describe '#analyse_controller' do
9
+ subject do
10
+ recorder.analyse_controller(controller)
11
+ recorder.record[:action]
12
+ end
13
+
14
+ let(:action) { :show }
15
+
16
+ let(:request) do
17
+ request = ActionDispatch::Request.new({})
18
+ request.request_parameters = { action: action }
19
+ request
20
+ end
21
+
22
+ let(:controller) do
23
+ controller = ActionController::Metal.new
24
+ controller.set_request!(request)
25
+ controller
26
+ end
27
+
28
+ it { is_expected.to eq(action) }
29
+
30
+ context 'when a api_action_matcher is configured' do
31
+ let(:matcher_action) { "#{action}_from_new_matcher" }
32
+
33
+ before do
34
+ Apipie.configuration.api_action_matcher = proc { |_| matcher_action }
35
+ end
36
+
37
+ it { is_expected.to eq(matcher_action) }
38
+ end
39
+ end
40
+ end
@@ -16,6 +16,7 @@ describe Apipie::Generator::Swagger::Context do
16
16
 
17
17
  describe '#in_schema?' do
18
18
  it { is_expected.to be_in_schema }
19
+
19
20
  context 'when in_schema is false' do
20
21
  let(:in_schema) { false }
21
22
 
@@ -58,12 +58,12 @@ describe Apipie::MethodDescription do
58
58
 
59
59
  it "should be ordered" do
60
60
  expect(@method.params.keys).to eq([:a, :b, :c])
61
- expect(@method.to_json[:params].map{|h| h[:name]}).to eq(['a', 'b', 'c'])
61
+ expect(@method.to_json[:params].map { |h| h[:name] }).to eq(['a', 'b', 'c'])
62
62
  end
63
63
 
64
64
  it "should be still ordered" do
65
65
  expect(@method.params.keys).to eq([:a, :b, :c])
66
- expect(@method.to_json[:params].map{|h| h[:name]}).to eq(['a', 'b', 'c'])
66
+ expect(@method.to_json[:params].map { |h| h[:name] }).to eq(['a', 'b', 'c'])
67
67
  end
68
68
 
69
69
  end
@@ -80,7 +80,7 @@ describe Apipie::MethodDescription do
80
80
 
81
81
  it "should ignore response-only parameters" do
82
82
  expect(@method.params.keys).to eq([:a, :c])
83
- expect(@method.to_json[:params].map{|h| h[:name]}).to eq(['a', 'c'])
83
+ expect(@method.to_json[:params].map { |h| h[:name] }).to eq(['a', 'c'])
84
84
  end
85
85
  end
86
86
 
@@ -90,7 +90,7 @@ describe Apipie::MethodDescription do
90
90
  @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
91
91
  dsl_data[:returns] = { 200 => [{:param_group => 'pet', :array_of => 'pet'}, nil, nil] }
92
92
 
93
- expect {Apipie::MethodDescription.new(:a, @resource, dsl_data)}.to raise_error(Apipie::ReturnsMultipleDefinitionError)
93
+ expect { Apipie::MethodDescription.new(:a, @resource, dsl_data) }.to raise_error(Apipie::ReturnsMultipleDefinitionError)
94
94
  end
95
95
  end
96
96
 
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::NoDocumentedMethod do
4
+ let(:error) { described_class.new(controller_name, method_name) }
5
+ let(:controller_name) { 'UserController' }
6
+ let(:method_name) { 'index' }
7
+
8
+ describe '#to_s' do
9
+ subject { error.to_s }
10
+
11
+ let(:error_message) do
12
+ "There is no documented method #{controller_name}##{method_name}"
13
+ end
14
+
15
+ it { is_expected.to eq(error_message) }
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe Apipie::ParamDescription::Deprecation do
4
+ let(:info) { nil }
5
+ let(:deprecated_in) { nil }
6
+ let(:sunset_at) { nil }
7
+
8
+ let(:deprecation) do
9
+ described_class.new(
10
+ info: info,
11
+ deprecated_in: deprecated_in,
12
+ sunset_at: sunset_at
13
+ )
14
+ end
15
+
16
+ describe '#to_json' do
17
+ subject { deprecation.to_json }
18
+
19
+ it { is_expected.to eq({ info: nil, deprecated_in: nil, sunset_at: nil }) }
20
+
21
+ context 'when attributes are given' do
22
+ let(:info) { 'info' }
23
+ let(:deprecated_in) { '2.3' }
24
+ let(:sunset_at) { '3.0' }
25
+
26
+ it 'returns the correct attributes' do
27
+ expect(subject).to eq({ info: info, deprecated_in: deprecated_in, sunset_at: sunset_at })
28
+ end
29
+ end
30
+ end
31
+ end
@@ -188,7 +188,9 @@ describe Apipie::ParamDescription do
188
188
  end
189
189
  context 'when allow_blank is specified as false' do
190
190
  it "should throw an exception when passed an empty value" do
191
- expect { Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: false).validate('') }.to raise_error(Apipie::ParamInvalid)
191
+ expect do
192
+ Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: false).validate('')
193
+ end.to raise_error(Apipie::ParamInvalid)
192
194
  end
193
195
  end
194
196
  context 'when allow_blank is not specified' do
@@ -428,4 +430,82 @@ describe Apipie::ParamDescription do
428
430
  end
429
431
  end
430
432
 
433
+ describe '#deprecated?' do
434
+ subject do
435
+ Apipie::ParamDescription.new(method_desc, :param, Integer, options)
436
+ end
437
+
438
+ let(:options) { {} }
439
+
440
+ it { is_expected.not_to be_deprecated }
441
+
442
+ context 'when deprecated option is passed' do
443
+ context 'and is true' do
444
+ let(:options) { { deprecated: true } }
445
+
446
+ it { is_expected.to be_deprecated }
447
+ end
448
+
449
+ context 'and is false' do
450
+ let(:options) { { deprecated: false } }
451
+
452
+ it { is_expected.not_to be_deprecated }
453
+ end
454
+
455
+ context 'and is a string' do
456
+ let(:options) { { deprecated: 'Some description' } }
457
+
458
+ it { is_expected.to be_deprecated }
459
+ end
460
+
461
+ context 'and deprecation options are given' do
462
+ let(:options) do
463
+ { deprecated: { in: '2.3', info: 'Something', sunset: '3.0' } }
464
+ end
465
+
466
+ it { is_expected.to be_deprecated }
467
+ end
468
+ end
469
+ end
470
+
471
+ describe '#deprecation' do
472
+ subject { param_description.deprecation }
473
+
474
+ let(:options) { {} }
475
+
476
+ let(:param_description) do
477
+ Apipie::ParamDescription.new(method_desc, :param, Integer, options)
478
+ end
479
+
480
+ it { is_expected.to be_blank }
481
+
482
+ context 'when deprecated option is passed' do
483
+ context 'and is true' do
484
+ let(:options) { { deprecated: true } }
485
+
486
+ it { is_expected.to be_blank }
487
+ end
488
+
489
+ context 'and is false' do
490
+ let(:options) { { deprecated: false } }
491
+
492
+ it { is_expected.to be_blank }
493
+ end
494
+
495
+ context 'and is a string' do
496
+ let(:options) { { deprecated: 'Some description' } }
497
+
498
+ it { is_expected.to be_an_instance_of(Apipie::ParamDescription::Deprecation) }
499
+ end
500
+
501
+ context 'and deprecation options are given' do
502
+ let(:options) do
503
+ { deprecated: { in: '2.3', info: 'Something', sunset: '3.0' } }
504
+ end
505
+
506
+ it { is_expected.to be_an_instance_of(Apipie::ParamDescription::Deprecation) }
507
+ end
508
+ end
509
+ end
510
+
431
511
  end