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
@@ -1,98 +1,133 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Apipie::MethodDescription do
4
-
5
4
  let(:dsl_data) { ActionController::Base.send(:_apipie_dsl_data_init) }
5
+ let(:deprecated_resource_description) { false }
6
+ let(:method_description_method) { :some_method }
6
7
 
7
- describe "metadata" do
8
+ let(:resource_description_dsl) do
9
+ ActionController::Base.send(:_apipie_dsl_data_init).merge(
10
+ {
11
+ deprecated: deprecated_resource_description
12
+ }
13
+ )
14
+ end
8
15
 
9
- before(:each) do
10
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
11
- end
16
+ let(:resource_description) do
17
+ Apipie::ResourceDescription.new(
18
+ ApplicationController,
19
+ 'dummy',
20
+ resource_description_dsl
21
+ )
22
+ end
12
23
 
13
- it "should return nil when no metadata is provided" do
14
- method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
15
- expect(method.to_json[:metadata]).to eq(nil)
16
- end
24
+ let(:method_description) do
25
+ Apipie::MethodDescription.new(
26
+ method_description_method,
27
+ resource_description,
28
+ dsl_data
29
+ )
30
+ end
17
31
 
18
- it "should return the metadata" do
19
- meta = {
20
- :lenght => 32,
21
- :weight => '830g'
22
- }
23
- method = Apipie::MethodDescription.new(:a, @resource, dsl_data.update(:meta => meta))
24
- expect(method.to_json[:metadata]).to eq(meta)
25
- end
32
+ describe '#to_json' do
33
+ describe 'metadata' do
34
+ subject { method_description.to_json[:metadata] }
26
35
 
27
- end
36
+ it { is_expected.to be_nil }
28
37
 
29
- describe "deprecated flag" do
30
- before(:each) do
31
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
32
- end
38
+ context 'when meta are given' do
39
+ let(:meta) { { length: 32, weight: '830g' } }
40
+ let(:dsl_data) { super().merge(meta: meta) }
33
41
 
34
- it "should return the deprecated flag when provided" do
35
- dsl_data[:api_args] = [[:GET, "/foo/bar", "description", {:deprecated => true}]]
36
- method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
37
- expect(method.method_apis_to_json.first[:deprecated]).to eq(true)
42
+ it { is_expected.to eq(meta) }
43
+ end
38
44
  end
39
45
 
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)
46
+ describe 'params' do
47
+ subject(:json_params) { method_description.to_json[:params].map { |h| h[:name] } }
48
+
49
+ let(:dsl_data) do
50
+ super().merge(
51
+ {
52
+ params: [
53
+ [:a, String, nil, {}, nil],
54
+ [:b, String, nil, {}, nil],
55
+ [:c, String, nil, {}, nil]
56
+ ]
57
+ }
58
+ )
59
+ end
60
+
61
+ it 'should be ordered' do
62
+ expect(json_params).to eq(%w[a b c])
63
+ end
64
+
65
+ context 'when param is only for response' do
66
+ let(:dsl_data) do
67
+ super().merge(
68
+ {
69
+ params: [
70
+ [:a, String, nil, { only_in: :request }, nil],
71
+ [:b, String, nil, { only_in: :response }, nil],
72
+ [:c, String, nil, {}, nil]
73
+ ]
74
+ }
75
+ )
76
+ end
77
+
78
+ it 'should ignore response-only parameters' do
79
+ expect(json_params).to eq(%w[a c])
80
+ end
81
+ end
45
82
  end
46
83
  end
47
84
 
48
- describe "params descriptions" do
85
+ describe '#method_apis_to_json' do
86
+ describe 'deprecated' do
87
+ subject { method_description.method_apis_to_json.first[:deprecated] }
49
88
 
50
- before(:each) do
51
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
52
- dsl_data[:params] = [[:a, String, nil, {}, nil],
53
- [:b, String, nil, {}, nil],
54
- [:c, String, nil, {}, nil]]
55
- @method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
56
- @resource.add_method_description @method
57
- end
89
+ let(:api_deprecated) { false }
90
+ let(:dsl_data) { super().merge(api_args: api_args) }
58
91
 
59
- it "should be ordered" do
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'])
62
- end
92
+ let(:api_args) do
93
+ [[:GET, "/foo/bar", "description", { deprecated: api_deprecated }]]
94
+ end
63
95
 
64
- it "should be still ordered" do
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'])
67
- end
96
+ it { is_expected.to eq(false) }
68
97
 
69
- end
98
+ context 'when api is deprecated' do
99
+ let(:api_deprecated) { true }
70
100
 
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
101
+ it { is_expected.to eq(true) }
102
+ end
103
+
104
+ context 'when resource description is deprecated' do
105
+ let(:deprecated_resource_description) { true }
80
106
 
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'])
107
+ it { is_expected.to eq(true) }
108
+ end
84
109
  end
85
110
  end
86
111
 
112
+ describe '#returns' do
113
+ subject(:method_desc) { method_description }
87
114
 
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] }
115
+ context 'when both :param_group and :array_of are specified' do
116
+ let(:returns) do
117
+ { 200 => [{ param_group: 'pet', array_of: 'pet' }, nil, nil] }
118
+ end
92
119
 
93
- expect { Apipie::MethodDescription.new(:a, @resource, dsl_data) }.to raise_error(Apipie::ReturnsMultipleDefinitionError)
120
+ let(:dsl_data) { super().merge({ returns: returns }) }
121
+
122
+ it 'raises an error' do
123
+ expect { method_desc }.to raise_error(Apipie::ReturnsMultipleDefinitionError)
124
+ end
94
125
  end
95
126
  end
96
127
 
128
+ describe '#method_name' do
129
+ subject { method_description.method_name }
97
130
 
131
+ it { is_expected.to eq(method_description_method.to_s) }
132
+ end
98
133
  end
@@ -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
@@ -114,88 +114,210 @@ describe Apipie::ParamDescription do
114
114
  end
115
115
 
116
116
  describe 'validate' do
117
+ subject { param_description.validate(validation_value) }
118
+
119
+ let(:allow_blank) { nil }
120
+
121
+ let(:param_description) do
122
+ Apipie::ParamDescription.new(
123
+ method_desc,
124
+ :param,
125
+ validation_type,
126
+ allow_blank: allow_blank
127
+ )
128
+ end
129
+
117
130
  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
131
+ before { Apipie.configuration.ignore_allow_blank_false = true }
132
+ after { Apipie.configuration.ignore_allow_blank_false = false }
121
133
 
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
134
+ context 'when validation type is :boolean' do
135
+ let(:validation_type) { :boolean }
136
+
137
+ context 'when validation value is false' do
138
+ let(:validation_value) { false }
139
+
140
+ it 'should not raise an error' do
141
+ expect { subject }.not_to raise_error
142
+ end
125
143
  end
126
144
 
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)
145
+ context 'when validation value is an empty string' do
146
+ let(:validation_value) { '' }
147
+
148
+ it 'should raise an error' do
149
+ expect { subject }.to raise_error(Apipie::ParamInvalid)
150
+ end
129
151
  end
130
152
  end
131
153
 
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
154
+ context 'when validation type is a boolean array' do
155
+ let(:validation_type) { [true, false] }
156
+
157
+ context 'when validation value is false' do
158
+ let(:validation_value) { false }
159
+
160
+ it 'should not raise an error' do
161
+ expect { subject }.not_to raise_error
136
162
  end
137
163
  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
164
+
165
+ context 'when validation value is true' do
166
+ let(:validation_value) { true }
167
+
168
+ it 'should not raise an error' do
169
+ expect { subject }.not_to raise_error
141
170
  end
142
171
  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
172
+
173
+ context 'when validation value is an empty string' do
174
+ let(:validation_value) { '' }
175
+
176
+ it 'should raise an error' do
177
+ expect { subject }.to raise_error(Apipie::ParamInvalid)
146
178
  end
147
179
  end
148
180
  end
149
181
 
150
- after do
151
- Apipie.configuration.ignore_allow_blank_false = false
182
+ context 'when validation type is String' do
183
+ let(:validation_type) { String }
184
+
185
+ context 'when validation value is empty string' do
186
+ let(:validation_value) { '' }
187
+
188
+ it 'should not raise an error' do
189
+ expect { subject }.not_to raise_error
190
+ end
191
+
192
+ context 'when allow_blank is specified as true' do
193
+ let(:allow_blank) { true }
194
+
195
+ it 'should not raise an error' do
196
+ expect { subject }.not_to raise_error
197
+ end
198
+ end
199
+ end
152
200
  end
153
201
  end
154
202
 
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
203
+ context 'when the validation type is :boolean' do
204
+ let(:validation_type) { :boolean }
205
+
206
+ context 'when validation value' do
207
+ let(:validation_value) { false }
208
+
209
+ it 'should not raise an error' do
210
+ expect { subject }.not_to raise_error
211
+ end
212
+
213
+ context 'when allow_blank is false' do
214
+ let(:allow_blank) { false }
215
+
216
+ it 'should not raise an error' do
217
+ expect { subject }.not_to raise_error
218
+ end
219
+ end
220
+
221
+ context 'when allow_blank is true' do
222
+ let(:allow_blank) { true }
159
223
 
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
224
+ it 'should not raise an error' do
225
+ expect { subject }.not_to raise_error
226
+ end
227
+ end
162
228
  end
163
229
 
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)
230
+ context 'when validation value is empty string' do
231
+ let(:validation_value) { '' }
232
+
233
+ it 'should raise an error' do
234
+ expect { subject }.to raise_error(Apipie::ParamInvalid)
235
+ end
166
236
  end
167
237
  end
168
238
 
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
239
+ context 'when validation type is a boolean array' do
240
+ let(:validation_type) { [true, false] }
241
+
242
+ context 'when validation value is false' do
243
+ let(:validation_value) { false }
244
+
245
+ it 'should not raise an error' do
246
+ expect { subject }.not_to raise_error
247
+ end
172
248
  end
173
249
 
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
250
+ context 'when validation value is true' do
251
+ let(:validation_value) { true }
252
+
253
+ it 'should not raise an error' do
254
+ expect { subject }.not_to raise_error
255
+ end
176
256
  end
177
257
 
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)
258
+ context 'when validation value is an empty string' do
259
+ let(:validation_value) { '' }
260
+
261
+ it 'should raise an error' do
262
+ expect { subject }.to raise_error(Apipie::ParamInvalid)
263
+ end
180
264
  end
181
265
  end
182
266
 
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
267
+ context 'when the validation type is a custom one' do
268
+ let(:validation_type) { :custom_bool }
269
+
270
+ context 'when ignore_allow_blank? returning true' do
271
+ before do
272
+ allow(CustomBoolValidator).to receive(:ignore_allow_blank?).and_return(true)
273
+ end
274
+
275
+ context 'when a blank but valid value is passed' do
276
+ let(:validation_value) { false }
277
+
278
+ it 'should not raise an error' do
279
+ expect { subject }.not_to raise_error
280
+ end
281
+
282
+ context 'when allow_blank is false' do
283
+ let(:allow_blank) { false }
284
+
285
+ it 'should not raise an error' do
286
+ expect { subject }.not_to raise_error
287
+ end
288
+ end
289
+ end
290
+
291
+ context 'when a blank but invalid value is passed' do
292
+ let(:validation_value) { '' }
293
+
294
+ it 'should raise an error' do
295
+ expect { subject }.to raise_error(Apipie::ParamInvalid)
296
+ end
187
297
  end
188
298
  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 do
192
- Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: false).validate('')
193
- end.to raise_error(Apipie::ParamInvalid)
299
+ end
300
+
301
+ context 'when validation type is String' do
302
+ let(:validation_type) { String }
303
+
304
+ context 'when a blank but invalid value is passed' do
305
+ let(:validation_value) { '' }
306
+
307
+ it 'should raise an error' do
308
+ expect { subject }.to raise_error(Apipie::ParamInvalid)
194
309
  end
195
310
  end
196
- context 'when allow_blank is not specified' do
197
- it "should throw an exception when passed an empty value" do
198
- expect { Apipie::ParamDescription.new(method_desc, :param, String).validate('') }.to raise_error(Apipie::ParamInvalid)
311
+
312
+ context 'when allow_blank is specified as true' do
313
+ let(:allow_blank) { true }
314
+
315
+ context 'when and empty string is given' do
316
+ let(:validation_value) { '' }
317
+
318
+ it 'should not raise an error' do
319
+ expect { subject }.not_to raise_error
320
+ end
199
321
  end
200
322
  end
201
323
  end
@@ -239,7 +361,6 @@ describe Apipie::ParamDescription do
239
361
  end
240
362
  end
241
363
 
242
-
243
364
  describe "required_by_default config option" do
244
365
  context "parameters required by default" do
245
366
 
@@ -369,7 +490,6 @@ describe Apipie::ParamDescription do
369
490
  end
370
491
  end
371
492
 
372
-
373
493
  describe 'sub params' do
374
494
 
375
495
  context 'with HashValidator' do
@@ -508,4 +628,44 @@ describe Apipie::ParamDescription do
508
628
  end
509
629
  end
510
630
 
631
+ describe '#required' do
632
+ subject { param_description.required }
633
+
634
+ let(:param_description) do
635
+ Apipie::ParamDescription.new(method_desc, :param, validator, options)
636
+ end
637
+
638
+ context 'when is passed in options' do
639
+ let(:validator) { String }
640
+
641
+ context 'when is false' do
642
+ let(:options) { { required: false } }
643
+
644
+ it { is_expected.to eq(false) }
645
+ end
646
+
647
+ context 'when is true' do
648
+ let(:options) { { required: true } }
649
+
650
+ it { is_expected.to eq(true) }
651
+ end
652
+ end
653
+
654
+ context 'when validator options are passed' do
655
+ let(:options) { { only_in: :response } }
656
+ let(:validator) { { array_of: String, required: required } }
657
+
658
+ context 'when required is false' do
659
+ let(:required) { false }
660
+
661
+ it { is_expected.to eq(false) }
662
+ end
663
+
664
+ context 'when required is true' do
665
+ let(:required) { true }
666
+
667
+ it { is_expected.to eq(true) }
668
+ end
669
+ end
670
+ end
511
671
  end
@@ -1,6 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "param groups" do
4
+ before { Apipie.reload_documentation }
4
5
 
5
6
  it "lets reuse the params description in more actions" do
6
7
  user_create_desc = Apipie["users#create"].params[:user]
@@ -1,48 +1,91 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Apipie::ResourceDescription do
4
+ let(:resource_description) do
5
+ Apipie::ResourceDescription.new(controller, id, dsl_data)
6
+ end
4
7
 
8
+ let(:controller) { ApplicationController }
9
+ let(:id) { 'dummy' }
5
10
  let(:dsl_data) { ActionController::Base.send(:_apipie_dsl_data_init) }
6
11
 
7
- describe "metadata" do
12
+ describe '#_methods' do
13
+ subject(:methods) { resource_description._methods }
8
14
 
9
- it "should return nil when no metadata is provided" do
10
- resource = Apipie::ResourceDescription.new(ApplicationController, "dummy", dsl_data)
11
- expect(resource.to_json[:metadata]).to eq(nil)
12
- end
15
+ context 'when has method descriptions' do
16
+ before do
17
+ resource_description.add_method_description(
18
+ Apipie::MethodDescription.new(:a, resource_description, dsl_data)
19
+ )
20
+ resource_description.add_method_description(
21
+ Apipie::MethodDescription.new(:b, resource_description, dsl_data)
22
+ )
23
+ resource_description.add_method_description(
24
+ Apipie::MethodDescription.new(:c, resource_description, dsl_data)
25
+ )
26
+ end
13
27
 
14
- it "should return the metadata" do
15
- meta = {
16
- :lenght => 32,
17
- :weight => '830g'
18
- }
19
- resource = Apipie::ResourceDescription.new(ApplicationController, "dummy", dsl_data.update(:meta => meta))
20
- expect(resource.to_json[:metadata]).to eq(meta)
28
+ it 'should be ordered' do
29
+ expect(methods.keys).to eq([:a, :b, :c])
30
+ end
21
31
  end
22
-
23
32
  end
24
33
 
25
- describe "methods descriptions" do
34
+ describe '#to_json' do
35
+ let(:json_data) { resource_description.to_json }
36
+
37
+ describe 'metadata' do
38
+ subject { json_data[:metadata] }
26
39
 
27
- before(:each) do
28
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
29
- a = Apipie::MethodDescription.new(:a, @resource, dsl_data)
30
- b = Apipie::MethodDescription.new(:b, @resource, dsl_data)
31
- c = Apipie::MethodDescription.new(:c, @resource, dsl_data)
32
- @resource.add_method_description(a)
33
- @resource.add_method_description(b)
34
- @resource.add_method_description(c)
40
+ it { is_expected.to be_nil }
41
+
42
+ context 'when meta data are provided' do
43
+ let(:meta) { { length: 32, weight: '830g' } }
44
+ let(:dsl_data) { super().update({ meta: meta }) }
45
+
46
+ it { is_expected.to eq(meta) }
47
+ end
35
48
  end
36
49
 
37
- it "should be ordered" do
38
- expect(@resource._methods.keys).to eq([:a, :b, :c])
39
- expect(@resource.to_json[:methods].map { |h| h[:name] }).to eq(['a', 'b', 'c'])
50
+ describe 'methods' do
51
+ subject(:methods_as_json) { json_data[:methods] }
52
+
53
+ context 'when has method descriptions' do
54
+ before do
55
+ resource_description.add_method_description(
56
+ Apipie::MethodDescription.new(:a, resource_description, dsl_data)
57
+ )
58
+ resource_description.add_method_description(
59
+ Apipie::MethodDescription.new(:b, resource_description, dsl_data)
60
+ )
61
+ resource_description.add_method_description(
62
+ Apipie::MethodDescription.new(:c, resource_description, dsl_data)
63
+ )
64
+ end
65
+
66
+ it 'should be ordered' do
67
+ expect(methods_as_json.map { |h| h[:name] }).to eq(%w[a b c])
68
+ end
69
+ end
40
70
  end
71
+ end
72
+
73
+ describe 'name' do
74
+ subject { resource_description.name }
75
+
76
+ it { is_expected.to eq('Dummy') }
41
77
 
42
- it "should be still ordered" do
43
- expect(@resource._methods.keys).to eq([:a, :b, :c])
44
- expect(@resource.to_json[:methods].map { |h| h[:name] }).to eq(['a', 'b', 'c'])
78
+ context 'when given id contains dashes' do
79
+ let(:id) { 'some-nested-resource' }
80
+
81
+ it { is_expected.to eq('Some::Nested::Resource') }
45
82
  end
46
83
 
84
+ context 'when resource_name is given' do
85
+ let(:resource_name) { 'Some-Resource' }
86
+ let(:dsl_data) { super().merge!(resource_name: 'Some-Resource') }
87
+
88
+ it { is_expected.to eq(resource_name) }
89
+ end
47
90
  end
48
91
  end