apipie-rails 0.9.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rubocop.yml +2 -2
- data/.rubocop.yml +23 -14
- data/.rubocop_todo.yml +103 -487
- data/CHANGELOG.md +20 -3
- data/README.rst +12 -12
- data/app/controllers/apipie/apipies_controller.rb +6 -6
- data/app/helpers/apipie_helper.rb +1 -1
- data/lib/apipie/apipie_module.rb +5 -5
- data/lib/apipie/application.rb +81 -55
- data/lib/apipie/configuration.rb +19 -26
- data/lib/apipie/dsl_definition.rb +8 -9
- data/lib/apipie/error_description.rb +1 -1
- data/lib/apipie/errors.rb +2 -16
- data/lib/apipie/extractor/collector.rb +3 -3
- data/lib/apipie/extractor/recorder.rb +1 -1
- data/lib/apipie/extractor.rb +2 -2
- data/lib/apipie/generator/config.rb +12 -0
- data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
- data/lib/apipie/generator/swagger/config.rb +78 -0
- data/lib/apipie/generator/swagger/context.rb +12 -1
- data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
- data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +86 -0
- data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
- data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
- data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
- data/lib/apipie/generator/swagger/method_description/response_service.rb +58 -0
- data/lib/apipie/generator/swagger/method_description.rb +2 -0
- data/lib/apipie/generator/swagger/operation_id.rb +2 -2
- data/lib/apipie/generator/swagger/param_description/builder.rb +4 -4
- data/lib/apipie/generator/swagger/param_description/composite.rb +9 -1
- data/lib/apipie/generator/swagger/param_description/in.rb +1 -1
- data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
- data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
- data/lib/apipie/generator/swagger/param_description/type.rb +9 -2
- data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
- data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
- data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
- data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
- data/lib/apipie/generator/swagger/schema.rb +63 -0
- data/lib/apipie/generator/swagger/type_extractor.rb +0 -19
- data/lib/apipie/generator/swagger/warning.rb +3 -6
- data/lib/apipie/generator/swagger/warning_writer.rb +7 -1
- data/lib/apipie/helpers.rb +3 -3
- data/lib/apipie/method_description.rb +5 -3
- data/lib/apipie/param_description.rb +4 -2
- data/lib/apipie/resource_description.rb +11 -8
- data/lib/apipie/response_description.rb +1 -1
- data/lib/apipie/response_description_adapter.rb +3 -3
- data/lib/apipie/routing.rb +1 -1
- data/lib/apipie/rspec/response_validation_helper.rb +1 -1
- data/lib/apipie/swagger_generator.rb +27 -551
- data/lib/apipie/validator.rb +9 -5
- data/lib/apipie/version.rb +1 -1
- data/lib/apipie-rails.rb +17 -0
- data/lib/tasks/apipie.rake +25 -20
- data/spec/controllers/api/v2/nested/resources_controller_spec.rb +2 -2
- data/spec/controllers/pets_controller_spec.rb +10 -16
- data/spec/controllers/users_controller_spec.rb +2 -2
- data/spec/dummy/app/controllers/pets_controller.rb +4 -4
- data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
- data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
- data/spec/dummy/app/controllers/users_controller.rb +5 -5
- data/spec/dummy/config.ru +1 -1
- data/spec/lib/apipie/apipies_controller_spec.rb +4 -0
- data/spec/lib/apipie/application_spec.rb +25 -15
- data/spec/lib/apipie/configuration_spec.rb +15 -0
- data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
- data/spec/lib/apipie/generator/swagger/context_spec.rb +23 -2
- data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +106 -0
- data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
- data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +1 -1
- data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +2 -2
- data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +7 -7
- data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
- data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
- data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
- data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
- data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
- data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +0 -43
- data/spec/lib/apipie/generator/swagger/warning_spec.rb +1 -1
- data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +19 -7
- data/spec/lib/apipie/method_description_spec.rb +101 -66
- data/spec/lib/apipie/no_documented_method_spec.rb +17 -0
- data/spec/lib/apipie/param_description_spec.rb +209 -49
- data/spec/lib/apipie/param_group_spec.rb +1 -0
- data/spec/lib/apipie/resource_description_spec.rb +71 -28
- data/spec/lib/apipie/response_does_not_match_swagger_schema_spec.rb +35 -0
- data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
- data/spec/lib/apipie/validator_spec.rb +47 -11
- data/spec/lib/rake_spec.rb +1 -1
- data/spec/lib/swagger/rake_swagger_spec.rb +6 -6
- data/spec/lib/swagger/swagger_dsl_spec.rb +17 -11
- data/spec/lib/validators/array_validator_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -2
- 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
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
16
|
+
let(:resource_description) do
|
17
|
+
Apipie::ResourceDescription.new(
|
18
|
+
ApplicationController,
|
19
|
+
'dummy',
|
20
|
+
resource_description_dsl
|
21
|
+
)
|
22
|
+
end
|
12
23
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
36
|
+
it { is_expected.to be_nil }
|
28
37
|
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
35
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
85
|
+
describe '#method_apis_to_json' do
|
86
|
+
describe 'deprecated' do
|
87
|
+
subject { method_description.method_apis_to_json.first[:deprecated] }
|
49
88
|
|
50
|
-
|
51
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
92
|
+
let(:api_args) do
|
93
|
+
[[:GET, "/foo/bar", "description", { deprecated: api_deprecated }]]
|
94
|
+
end
|
63
95
|
|
64
|
-
|
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
|
-
|
98
|
+
context 'when api is deprecated' do
|
99
|
+
let(:api_deprecated) { true }
|
70
100
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
82
|
-
|
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
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
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
|
119
|
-
|
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
|
123
|
-
|
124
|
-
|
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
|
-
|
128
|
-
|
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
|
133
|
-
|
134
|
-
|
135
|
-
|
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
|
-
|
139
|
-
|
140
|
-
|
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
|
-
|
144
|
-
|
145
|
-
|
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
|
-
|
151
|
-
|
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
|
156
|
-
|
157
|
-
|
158
|
-
|
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
|
-
|
161
|
-
|
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
|
-
|
165
|
-
|
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
|
170
|
-
|
171
|
-
|
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
|
-
|
175
|
-
|
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
|
-
|
179
|
-
|
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
|
184
|
-
|
185
|
-
|
186
|
-
|
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
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
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
|
-
|
197
|
-
|
198
|
-
|
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,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
|
12
|
+
describe '#_methods' do
|
13
|
+
subject(:methods) { resource_description._methods }
|
8
14
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
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
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
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
|