apipie-rails 0.3.6 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/build.yml +67 -0
  3. data/.github/workflows/rubocop-challenger.yml +28 -0
  4. data/.gitignore +2 -0
  5. data/.rubocop.yml +37 -0
  6. data/.rubocop_todo.yml +1991 -0
  7. data/CHANGELOG.md +246 -2
  8. data/PROPOSAL_FOR_RESPONSE_DESCRIPTIONS.md +244 -0
  9. data/README.rst +646 -25
  10. data/Rakefile +0 -5
  11. data/apipie-rails.gemspec +14 -9
  12. data/app/controllers/apipie/apipies_controller.rb +51 -20
  13. data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +70 -41
  14. data/app/public/apipie/javascripts/bundled/bootstrap.js +1033 -479
  15. data/app/public/apipie/javascripts/bundled/jquery.js +5 -5
  16. data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +9 -12
  17. data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +9 -689
  18. data/app/views/apipie/apipies/_method_detail.erb +21 -0
  19. data/app/views/apipie/apipies/_params.html.erb +4 -2
  20. data/app/views/apipie/apipies/index.html.erb +5 -1
  21. data/app/views/apipie/apipies/resource.html.erb +3 -0
  22. data/app/views/layouts/apipie/apipie.html.erb +1 -1
  23. data/config/locales/en.yml +1 -0
  24. data/config/locales/fr.yml +31 -0
  25. data/config/locales/it.yml +31 -0
  26. data/config/locales/ja.yml +31 -0
  27. data/config/locales/ko.yml +31 -0
  28. data/config/locales/pt-BR.yml +1 -1
  29. data/gemfiles/Gemfile.rails50 +10 -0
  30. data/gemfiles/Gemfile.rails51 +10 -0
  31. data/gemfiles/Gemfile.rails52 +10 -0
  32. data/gemfiles/Gemfile.rails60 +17 -0
  33. data/gemfiles/Gemfile.rails61 +17 -0
  34. data/gemfiles/Gemfile.rails70 +17 -0
  35. data/lib/apipie/apipie_module.rb +22 -4
  36. data/lib/apipie/application.rb +54 -25
  37. data/lib/apipie/configuration.rb +26 -4
  38. data/lib/apipie/core_ext/route.rb +9 -0
  39. data/lib/apipie/dsl_definition.rb +168 -16
  40. data/lib/apipie/error_description.rb +9 -2
  41. data/lib/apipie/errors.rb +34 -0
  42. data/lib/apipie/extractor/collector.rb +4 -0
  43. data/lib/apipie/extractor/recorder.rb +14 -12
  44. data/lib/apipie/extractor/writer.rb +86 -58
  45. data/lib/apipie/extractor.rb +5 -5
  46. data/lib/apipie/generator/generator.rb +2 -0
  47. data/lib/apipie/generator/swagger/swagger.rb +2 -0
  48. data/lib/apipie/generator/swagger/type.rb +16 -0
  49. data/lib/apipie/generator/swagger/type_extractor.rb +70 -0
  50. data/lib/apipie/generator/swagger/warning.rb +77 -0
  51. data/lib/apipie/generator/swagger/warning_writer.rb +48 -0
  52. data/lib/apipie/markup.rb +14 -11
  53. data/lib/apipie/method_description/api.rb +12 -0
  54. data/lib/apipie/method_description/apis_service.rb +82 -0
  55. data/lib/apipie/method_description.rb +51 -49
  56. data/lib/apipie/param_description.rb +63 -5
  57. data/lib/apipie/resource_description.rb +11 -4
  58. data/lib/apipie/response_description.rb +131 -0
  59. data/lib/apipie/response_description_adapter.rb +200 -0
  60. data/lib/apipie/routes_formatter.rb +1 -1
  61. data/lib/apipie/rspec/response_validation_helper.rb +194 -0
  62. data/lib/apipie/static_dispatcher.rb +5 -2
  63. data/lib/apipie/swagger_generator.rb +717 -0
  64. data/lib/apipie/tag_list_description.rb +11 -0
  65. data/lib/apipie/validator.rb +83 -9
  66. data/lib/apipie/version.rb +1 -1
  67. data/lib/apipie-rails.rb +15 -4
  68. data/lib/generators/apipie/install/install_generator.rb +1 -1
  69. data/lib/generators/apipie/views_generator.rb +1 -1
  70. data/lib/tasks/apipie.rake +115 -15
  71. data/rel-eng/gem_release.ipynb +398 -0
  72. data/spec/controllers/apipies_controller_spec.rb +79 -14
  73. data/spec/controllers/concerns_controller_spec.rb +2 -2
  74. data/spec/controllers/extended_controller_spec.rb +14 -0
  75. data/spec/controllers/included_param_group_controller_spec.rb +13 -0
  76. data/spec/controllers/memes_controller_spec.rb +10 -0
  77. data/spec/controllers/users_controller_spec.rb +139 -76
  78. data/spec/dummy/Rakefile +1 -1
  79. data/spec/dummy/app/controllers/application_controller.rb +5 -1
  80. data/spec/dummy/app/controllers/concerns_controller.rb +1 -1
  81. data/spec/dummy/app/controllers/extended_controller.rb +14 -0
  82. data/spec/dummy/app/controllers/extending_concern.rb +10 -0
  83. data/spec/dummy/app/controllers/included_param_group_controller.rb +19 -0
  84. data/spec/dummy/app/controllers/overridden_concerns_controller.rb +2 -2
  85. data/spec/dummy/app/controllers/pets_controller.rb +408 -0
  86. data/spec/dummy/app/controllers/pets_using_auto_views_controller.rb +73 -0
  87. data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +95 -0
  88. data/spec/dummy/app/controllers/{concerns/sample_controller.rb → sample_controller.rb} +5 -7
  89. data/spec/dummy/app/controllers/tagged_cats_controller.rb +32 -0
  90. data/spec/dummy/app/controllers/tagged_dogs_controller.rb +15 -0
  91. data/spec/dummy/app/controllers/twitter_example_controller.rb +5 -0
  92. data/spec/dummy/app/controllers/users_controller.rb +26 -12
  93. data/spec/dummy/app/helpers/random_param_group.rb +8 -0
  94. data/spec/dummy/components/test_engine/Gemfile +6 -0
  95. data/spec/dummy/components/test_engine/app/controllers/test_engine/application_controller.rb +4 -0
  96. data/spec/dummy/components/test_engine/app/controllers/test_engine/memes_controller.rb +37 -0
  97. data/spec/dummy/components/test_engine/config/routes.rb +3 -0
  98. data/spec/dummy/components/test_engine/db/.gitkeep +0 -0
  99. data/spec/dummy/components/test_engine/lib/test_engine.rb +7 -0
  100. data/spec/dummy/components/test_engine/test_engine.gemspec +11 -0
  101. data/spec/dummy/config/application.rb +6 -4
  102. data/spec/dummy/config/boot.rb +2 -2
  103. data/spec/dummy/config/environment.rb +1 -1
  104. data/spec/dummy/config/environments/development.rb +3 -3
  105. data/spec/dummy/config/environments/production.rb +3 -3
  106. data/spec/dummy/config/environments/test.rb +3 -5
  107. data/spec/dummy/config/initializers/apipie.rb +5 -3
  108. data/spec/dummy/config/routes.rb +25 -1
  109. data/spec/dummy/config.ru +1 -1
  110. data/spec/dummy/script/rails +2 -2
  111. data/spec/lib/application_spec.rb +1 -1
  112. data/spec/lib/extractor/writer_spec.rb +37 -7
  113. data/spec/lib/file_handler_spec.rb +25 -0
  114. data/spec/lib/generator/swagger/type_extractor_spec.rb +61 -0
  115. data/spec/lib/generator/swagger/warning_spec.rb +51 -0
  116. data/spec/lib/generator/swagger/warning_writer_spec.rb +59 -0
  117. data/spec/lib/method_description/apis_service_spec.rb +60 -0
  118. data/spec/lib/method_description_spec.rb +34 -0
  119. data/spec/lib/param_description_spec.rb +90 -4
  120. data/spec/lib/rake_spec.rb +2 -4
  121. data/spec/lib/swagger/openapi_2_0_schema.json +1607 -0
  122. data/spec/lib/swagger/rake_swagger_spec.rb +154 -0
  123. data/spec/lib/swagger/response_validation_spec.rb +104 -0
  124. data/spec/lib/swagger/swagger_dsl_spec.rb +658 -0
  125. data/spec/lib/validator_spec.rb +59 -1
  126. data/spec/lib/validators/array_validator_spec.rb +28 -8
  127. data/spec/spec_helper.rb +49 -3
  128. data/spec/support/custom_bool_validator.rb +17 -0
  129. metadata +104 -99
  130. data/.travis.yml +0 -12
  131. data/Gemfile +0 -7
  132. data/Gemfile.rails32 +0 -6
  133. data/Gemfile.rails40 +0 -5
  134. data/Gemfile.rails41 +0 -5
  135. data/Gemfile.rails42 +0 -5
  136. data/lib/apipie/client/generator.rb +0 -135
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::WarningWriter do
4
+ let(:writer) { described_class.clone.instance }
5
+
6
+ let(:warning) do
7
+ Apipie::Generator::Swagger::Warning.for_code(
8
+ Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE,
9
+ 'SampleController#action',
10
+ { parameter: 'some-param' }
11
+ )
12
+ end
13
+
14
+ before do
15
+ Apipie.configuration.swagger_suppress_warnings = false
16
+ Singleton.__init__(described_class)
17
+ end
18
+
19
+ describe '#warn' do
20
+ subject { writer.warn(warning) }
21
+
22
+ it 'outputs the warning' do
23
+ expect { subject }.to output(warning.warning_message).to_stderr
24
+ end
25
+
26
+ context 'when Apipie.configuration.swagger_suppress_warnings is true' do
27
+ before { Apipie.configuration.swagger_suppress_warnings = true }
28
+
29
+ it { is_expected.to be_falsey }
30
+ end
31
+
32
+ context 'when Apipie.configuration.swagger_suppress_warnings includes warning code' do
33
+ before do
34
+ Apipie.configuration.swagger_suppress_warnings =
35
+ Array(Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE)
36
+ end
37
+
38
+ it { is_expected.to be_falsey }
39
+ end
40
+
41
+ context 'when a warning already been logged' do
42
+ before { writer.warn(warning) }
43
+
44
+ it { is_expected.to be_falsey }
45
+ end
46
+ end
47
+
48
+ describe '#issued_warnings?' do
49
+ subject { writer.issued_warnings? }
50
+
51
+ it { is_expected.to be_falsey }
52
+
53
+ context 'when a warning already been logged' do
54
+ before { writer.warn(warning) }
55
+
56
+ it { is_expected.to be_truthy }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::MethodDescription::ApisService do
4
+ let(:resource) {}
5
+ let(:controller_action) {}
6
+ let(:api_args) { [] }
7
+ let(:dsl) { { api_args: api_args } }
8
+ let(:service) { described_class.new(resource, controller_action, dsl) }
9
+
10
+ describe '#call' do
11
+ subject { service.call }
12
+
13
+ it { is_expected.to eq(api_args) }
14
+
15
+ context 'when api_from_routes is given' do
16
+ let(:controller) { UsersController }
17
+ let(:controller_action) { :show }
18
+ let(:resource) { Apipie::ResourceDescription.new(controller, 'dummy') }
19
+ let(:short_description) { 'Short description' }
20
+
21
+ let(:dsl) do
22
+ super().merge({
23
+ api_from_routes: {
24
+ desc: short_description,
25
+ options: {}
26
+ }
27
+ })
28
+ end
29
+
30
+ it 'returns an array of Apipie::MethodDescription::Api' do
31
+ expect(subject).to all(be_an_instance_of(Apipie::MethodDescription::Api))
32
+ expect(subject.count).to eq(1)
33
+ end
34
+
35
+ context 'Apipie::MethodDescription::Api' do
36
+ subject { service.call.first }
37
+
38
+ it 'has the correct values' do
39
+ expect(subject.short_description).to eq(short_description)
40
+ expect(subject.path).to eq('/api/users/:id')
41
+ expect(subject.from_routes).to eq(true)
42
+ expect(subject.options).to eq({ from_routes: true })
43
+ end
44
+
45
+ context "when it's from concern" do
46
+ let(:controller) { ConcernsController }
47
+ let(:controller_action) { :custom }
48
+ let(:dsl) { super().merge(from_concern: true ) }
49
+
50
+ it 'has the correct values' do
51
+ expect(subject.short_description).to eq(short_description)
52
+ expect(subject.path).to eq('/api/concern_resources/custom')
53
+ expect(subject.from_routes).to eq(true)
54
+ expect(subject.options).to eq({ from_routes: true })
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -36,6 +36,13 @@ describe Apipie::MethodDescription do
36
36
  method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
37
37
  expect(method.method_apis_to_json.first[:deprecated]).to eq(true)
38
38
  end
39
+
40
+ it "should return the deprecated flag if resource is deprecated" do
41
+ @resource.instance_variable_set("@_deprecated", true)
42
+ dsl_data[:api_args] = [[:GET, "/foo/bar", "description", {}]]
43
+ method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
44
+ expect(method.method_apis_to_json.first[:deprecated]).to eq(true)
45
+ end
39
46
  end
40
47
 
41
48
  describe "params descriptions" do
@@ -61,4 +68,31 @@ describe Apipie::MethodDescription do
61
68
 
62
69
  end
63
70
 
71
+ describe "response-only properties" do
72
+ before(:each) do
73
+ @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
74
+ dsl_data[:params] = [[:a, String, nil, {:only_in => :request}, nil],
75
+ [:b, String, nil, {:only_in => :response}, nil],
76
+ [:c, String, nil, {}, nil]]
77
+ @method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
78
+ @resource.add_method_description @method
79
+ end
80
+
81
+ it "should ignore response-only parameters" do
82
+ expect(@method.params.keys).to eq([:a, :c])
83
+ expect(@method.to_json[:params].map{|h| h[:name]}).to eq(['a', 'c'])
84
+ end
85
+ end
86
+
87
+
88
+ describe "'returns' properties" do
89
+ it "should raise an error if both :param_group and :array_of are specified in 'returns'" do
90
+ @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
91
+ dsl_data[:returns] = { 200 => [{:param_group => 'pet', :array_of => 'pet'}, nil, nil] }
92
+
93
+ expect {Apipie::MethodDescription.new(:a, @resource, dsl_data)}.to raise_error(Apipie::ReturnsMultipleDefinitionError)
94
+ end
95
+ end
96
+
97
+
64
98
  end
@@ -113,6 +113,92 @@ describe Apipie::ParamDescription do
113
113
 
114
114
  end
115
115
 
116
+ describe 'validate' do
117
+ context 'when allow_blank is ignored, as it was before 0.7.0' do
118
+ before do
119
+ Apipie.configuration.ignore_allow_blank_false = true
120
+ end
121
+
122
+ context 'when the parameter is a boolean' do
123
+ it "should not throw an exception when passed false" do
124
+ expect { Apipie::ParamDescription.new(method_desc, :param, :boolean).validate(false) }.to_not raise_error
125
+ end
126
+
127
+ it "should throw an exception when passed an empty value" do
128
+ expect { Apipie::ParamDescription.new(method_desc, :param, :boolean).validate('') }.to raise_error(Apipie::ParamInvalid)
129
+ end
130
+ end
131
+
132
+ context 'when the parameter is a string' do
133
+ context 'when allow_blank is specified as true' do
134
+ it "should throw an exception when passed an empty value" do
135
+ expect { Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: true).validate('') }.to_not raise_error
136
+ end
137
+ end
138
+ context 'when allow_blank is specified as false' do
139
+ it "should throw an exception when passed an empty value" do
140
+ expect { Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: false).validate('') }.to_not raise_error
141
+ end
142
+ end
143
+ context 'when allow_blank is not specified' do
144
+ it "should throw an exception when passed an empty value" do
145
+ expect { Apipie::ParamDescription.new(method_desc, :param, String).validate('') }.to_not raise_error
146
+ end
147
+ end
148
+ end
149
+
150
+ after do
151
+ Apipie.configuration.ignore_allow_blank_false = false
152
+ end
153
+ end
154
+
155
+ context 'when the parameter is a boolean' do
156
+ it "should not throw an exception when passed false" do
157
+ expect { Apipie::ParamDescription.new(method_desc, :param, :boolean).validate(false) }.to_not raise_error
158
+ end
159
+
160
+ it "should still not throw an exception when passed false with explicit allow_blank: false" do
161
+ expect { Apipie::ParamDescription.new(method_desc, :param, :boolean, allow_blank: false).validate(false) }.to_not raise_error
162
+ end
163
+
164
+ it "should throw an exception when passed an empty value" do
165
+ expect { Apipie::ParamDescription.new(method_desc, :param, :boolean).validate('') }.to raise_error(Apipie::ParamInvalid)
166
+ end
167
+ end
168
+
169
+ context "when the parameter is a custom type with ignore_allow_blank? returning true" do
170
+ it "should not throw an exception when passed a blank but valid value" do
171
+ expect { Apipie::ParamDescription.new(method_desc, :param, :custom_bool).validate(false) }.to_not raise_error
172
+ end
173
+
174
+ it "should still not throw an exception when passed false with explicit allow_blank: false" do
175
+ expect { Apipie::ParamDescription.new(method_desc, :param, :custom_bool, allow_blank: false).validate(false) }.to_not raise_error
176
+ end
177
+
178
+ it "should throw an exception when passed an invalid but blank value" do
179
+ expect { Apipie::ParamDescription.new(method_desc, :param, :custom_bool).validate("") }.to raise_error(Apipie::ParamInvalid)
180
+ end
181
+ end
182
+
183
+ context 'when the parameter is a string' do
184
+ context 'when allow_blank is specified as true' do
185
+ it "should throw an exception when passed an empty value" do
186
+ expect { Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: true).validate('') }.to_not raise_error
187
+ end
188
+ end
189
+ context 'when allow_blank is specified as false' do
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)
192
+ end
193
+ end
194
+ context 'when allow_blank is not specified' do
195
+ it "should throw an exception when passed an empty value" do
196
+ expect { Apipie::ParamDescription.new(method_desc, :param, String).validate('') }.to raise_error(Apipie::ParamInvalid)
197
+ end
198
+ end
199
+ end
200
+ end
201
+
116
202
  describe "concern substitution" do
117
203
 
118
204
  let(:concern_dsl_data) { dsl_data.merge(:from_concern => true) }
@@ -288,7 +374,7 @@ describe Apipie::ParamDescription do
288
374
 
289
375
  subject do
290
376
  Apipie::ParamDescription.new(method_desc, :param, Hash) do
291
- param :answer, Fixnum
377
+ param :answer, Integer
292
378
  end
293
379
  end
294
380
 
@@ -306,7 +392,7 @@ describe Apipie::ParamDescription do
306
392
 
307
393
  subject do
308
394
  Apipie::ParamDescription.new(method_desc, :param, Array) do
309
- param :answer, Fixnum
395
+ param :answer, Integer
310
396
  end
311
397
  end
312
398
 
@@ -336,9 +422,9 @@ describe Apipie::ParamDescription do
336
422
 
337
423
  describe "Array with classes" do
338
424
  it "should be valid for objects included in class array" do
339
- param = Apipie::ParamDescription.new(method_desc, :param, [Fixnum, String])
425
+ param = Apipie::ParamDescription.new(method_desc, :param, [Integer, String])
340
426
  expect { param.validate("1") }.not_to raise_error
341
- expect { param.validate(Fixnum) }.to raise_error(Apipie::ParamInvalid)
427
+ expect { param.validate(Integer) }.to raise_error(Apipie::ParamInvalid)
342
428
  end
343
429
  end
344
430
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe 'rake tasks' do
4
4
  include_context "rake"
5
5
 
6
- let(:doc_path) { "user_specified_doc_path" }
6
+ let(:doc_path) { 'tmp/user_specified_doc_path' }
7
7
 
8
8
  before do
9
9
  Apipie.configuration.doc_path = doc_path
@@ -49,9 +49,7 @@ describe 'rake tasks' do
49
49
  end
50
50
 
51
51
  describe 'apipie:cache' do
52
- let(:cache_output) do
53
- File.join(::Rails.root, 'public', 'apipie-cache')
54
- end
52
+ let(:cache_output) { Apipie.configuration.cache_dir }
55
53
 
56
54
  let(:apidoc_html) do
57
55
  File.read("#{cache_output}.html")