grape 1.5.2 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +47 -0
- data/CONTRIBUTING.md +2 -1
- data/README.md +33 -3
- data/UPGRADING.md +71 -2
- data/grape.gemspec +5 -5
- data/lib/grape/api/instance.rb +13 -17
- data/lib/grape/api.rb +18 -13
- data/lib/grape/cookies.rb +2 -0
- data/lib/grape/dsl/desc.rb +3 -5
- data/lib/grape/dsl/headers.rb +5 -2
- data/lib/grape/dsl/helpers.rb +7 -5
- data/lib/grape/dsl/inside_route.rb +17 -8
- data/lib/grape/dsl/middleware.rb +4 -4
- data/lib/grape/dsl/parameters.rb +3 -3
- data/lib/grape/dsl/request_response.rb +9 -6
- data/lib/grape/dsl/routing.rb +2 -2
- data/lib/grape/dsl/settings.rb +5 -5
- data/lib/grape/endpoint.rb +21 -36
- data/lib/grape/error_formatter/json.rb +2 -6
- data/lib/grape/error_formatter/xml.rb +2 -6
- data/lib/grape/exceptions/empty_message_body.rb +11 -0
- data/lib/grape/exceptions/validation.rb +1 -2
- data/lib/grape/formatter/json.rb +1 -0
- data/lib/grape/formatter/serializable_hash.rb +2 -1
- data/lib/grape/formatter/xml.rb +1 -0
- data/lib/grape/locale/en.yml +1 -1
- data/lib/grape/middleware/auth/dsl.rb +7 -1
- data/lib/grape/middleware/base.rb +3 -1
- data/lib/grape/middleware/formatter.rb +4 -4
- data/lib/grape/middleware/stack.rb +2 -2
- data/lib/grape/middleware/versioner/accept_version_header.rb +3 -5
- data/lib/grape/middleware/versioner/header.rb +6 -4
- data/lib/grape/middleware/versioner/param.rb +1 -0
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +2 -1
- data/lib/grape/middleware/versioner/path.rb +2 -0
- data/lib/grape/parser/json.rb +1 -1
- data/lib/grape/parser/xml.rb +1 -1
- data/lib/grape/path.rb +1 -0
- data/lib/grape/request.rb +3 -0
- data/lib/grape/router/pattern.rb +1 -1
- data/lib/grape/router/route.rb +2 -2
- data/lib/grape/router.rb +6 -0
- data/lib/grape/util/inheritable_setting.rb +1 -3
- data/lib/grape/util/lazy_value.rb +3 -2
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/params_scope.rb +88 -55
- data/lib/grape/validations/types/custom_type_coercer.rb +1 -2
- data/lib/grape/validations/types/dry_type_coercer.rb +1 -1
- data/lib/grape/validations/types/json.rb +2 -1
- data/lib/grape/validations/types/primitive_coercer.rb +3 -3
- data/lib/grape/validations/validators/all_or_none.rb +8 -5
- data/lib/grape/validations/validators/allow_blank.rb +9 -7
- data/lib/grape/validations/validators/as.rb +6 -8
- data/lib/grape/validations/validators/at_least_one_of.rb +7 -4
- data/lib/grape/validations/validators/base.rb +75 -70
- data/lib/grape/validations/validators/coerce.rb +63 -79
- data/lib/grape/validations/validators/default.rb +37 -34
- data/lib/grape/validations/validators/exactly_one_of.rb +9 -6
- data/lib/grape/validations/validators/except_values.rb +13 -11
- data/lib/grape/validations/validators/multiple_params_base.rb +24 -20
- data/lib/grape/validations/validators/mutual_exclusion.rb +8 -5
- data/lib/grape/validations/validators/presence.rb +7 -4
- data/lib/grape/validations/validators/regexp.rb +8 -5
- data/lib/grape/validations/validators/same_as.rb +18 -15
- data/lib/grape/validations/validators/values.rb +61 -56
- data/lib/grape/validations.rb +6 -0
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +4 -1
- data/spec/grape/api/custom_validations_spec.rb +77 -45
- data/spec/grape/api/deeply_included_options_spec.rb +3 -3
- data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -1
- data/spec/grape/api/invalid_format_spec.rb +2 -0
- data/spec/grape/api/recognize_path_spec.rb +1 -1
- data/spec/grape/api/routes_with_requirements_spec.rb +8 -8
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -15
- data/spec/grape/api_remount_spec.rb +16 -15
- data/spec/grape/api_spec.rb +510 -220
- data/spec/grape/dsl/callbacks_spec.rb +2 -1
- data/spec/grape/dsl/headers_spec.rb +39 -9
- data/spec/grape/dsl/helpers_spec.rb +3 -2
- data/spec/grape/dsl/inside_route_spec.rb +6 -4
- data/spec/grape/dsl/logger_spec.rb +16 -18
- data/spec/grape/dsl/middleware_spec.rb +2 -1
- data/spec/grape/dsl/parameters_spec.rb +2 -0
- data/spec/grape/dsl/request_response_spec.rb +1 -0
- data/spec/grape/dsl/routing_spec.rb +10 -7
- data/spec/grape/endpoint/declared_spec.rb +259 -12
- data/spec/grape/endpoint_spec.rb +77 -55
- data/spec/grape/entity_spec.rb +22 -22
- data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -0
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -22
- data/spec/grape/exceptions/validation_errors_spec.rb +13 -10
- data/spec/grape/exceptions/validation_spec.rb +5 -3
- data/spec/grape/extensions/param_builders/hash_spec.rb +7 -7
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -8
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -8
- data/spec/grape/integration/rack_sendfile_spec.rb +1 -1
- data/spec/grape/loading_spec.rb +8 -8
- data/spec/grape/middleware/auth/dsl_spec.rb +15 -6
- data/spec/grape/middleware/auth/strategies_spec.rb +60 -20
- data/spec/grape/middleware/base_spec.rb +24 -15
- data/spec/grape/middleware/error_spec.rb +2 -2
- data/spec/grape/middleware/exception_spec.rb +111 -161
- data/spec/grape/middleware/formatter_spec.rb +27 -6
- data/spec/grape/middleware/globals_spec.rb +7 -4
- data/spec/grape/middleware/stack_spec.rb +14 -12
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -1
- data/spec/grape/middleware/versioner/header_spec.rb +14 -13
- data/spec/grape/middleware/versioner/param_spec.rb +7 -1
- data/spec/grape/middleware/versioner/path_spec.rb +5 -1
- data/spec/grape/middleware/versioner_spec.rb +1 -1
- data/spec/grape/parser_spec.rb +4 -0
- data/spec/grape/path_spec.rb +52 -52
- data/spec/grape/presenters/presenter_spec.rb +7 -6
- data/spec/grape/request_spec.rb +6 -4
- data/spec/grape/util/inheritable_setting_spec.rb +7 -7
- data/spec/grape/util/inheritable_values_spec.rb +3 -2
- data/spec/grape/util/reverse_stackable_values_spec.rb +3 -1
- data/spec/grape/util/stackable_values_spec.rb +7 -5
- data/spec/grape/validations/instance_behaivour_spec.rb +9 -10
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +1 -0
- data/spec/grape/validations/params_scope_spec.rb +46 -10
- data/spec/grape/validations/single_attribute_iterator_spec.rb +2 -1
- data/spec/grape/validations/types/primitive_coercer_spec.rb +4 -4
- data/spec/grape/validations/types_spec.rb +8 -8
- data/spec/grape/validations/validators/all_or_none_spec.rb +50 -56
- data/spec/grape/validations/validators/allow_blank_spec.rb +136 -140
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -56
- data/spec/grape/validations/validators/coerce_spec.rb +99 -22
- data/spec/grape/validations/validators/default_spec.rb +72 -78
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
- data/spec/grape/validations/validators/except_values_spec.rb +3 -3
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -77
- data/spec/grape/validations/validators/presence_spec.rb +16 -1
- data/spec/grape/validations/validators/regexp_spec.rb +25 -31
- data/spec/grape/validations/validators/same_as_spec.rb +14 -20
- data/spec/grape/validations/validators/values_spec.rb +183 -178
- data/spec/grape/validations_spec.rb +99 -58
- data/spec/integration/eager_load/eager_load_spec.rb +2 -2
- data/spec/integration/multi_json/json_spec.rb +1 -1
- data/spec/integration/multi_xml/xml_spec.rb +1 -1
- data/spec/shared/versioning_examples.rb +12 -9
- data/spec/spec_helper.rb +12 -2
- data/spec/support/basic_auth_encode_helpers.rb +1 -1
- metadata +102 -101
@@ -12,21 +12,51 @@ module Grape
|
|
12
12
|
describe Headers do
|
13
13
|
subject { HeadersSpec::Dummy.new }
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
let(:header_data) do
|
16
|
+
{ 'First Key' => 'First Value',
|
17
|
+
'Second Key' => 'Second Value' }
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when headers are set' do
|
21
|
+
describe '#header' do
|
17
22
|
before do
|
18
|
-
subject.header
|
23
|
+
header_data.each { |k, v| subject.header(k, v) }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'get' do
|
27
|
+
it 'returns a specifc value' do
|
28
|
+
expect(subject.header['First Key']).to eq 'First Value'
|
29
|
+
expect(subject.header['Second Key']).to eq 'Second Value'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns all set headers' do
|
33
|
+
expect(subject.header).to eq header_data
|
34
|
+
expect(subject.headers).to eq header_data
|
35
|
+
end
|
19
36
|
end
|
20
37
|
|
21
|
-
|
22
|
-
|
23
|
-
|
38
|
+
describe 'set' do
|
39
|
+
it 'returns value' do
|
40
|
+
expect(subject.header('Third Key', 'Third Value'))
|
41
|
+
expect(subject.header['Third Key']).to eq 'Third Value'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'delete' do
|
46
|
+
it 'deletes a header key-value pair' do
|
47
|
+
expect(subject.header('First Key')).to eq header_data['First Key']
|
48
|
+
expect(subject.header).not_to have_key('First Key')
|
49
|
+
end
|
24
50
|
end
|
25
51
|
end
|
52
|
+
end
|
26
53
|
|
27
|
-
|
28
|
-
|
29
|
-
|
54
|
+
context 'when no headers are set' do
|
55
|
+
describe '#header' do
|
56
|
+
it 'returns nil' do
|
57
|
+
expect(subject.header['First Key']).to be nil
|
58
|
+
expect(subject.header('First Key')).to be nil
|
59
|
+
end
|
30
60
|
end
|
31
61
|
end
|
32
62
|
end
|
@@ -34,6 +34,7 @@ module Grape
|
|
34
34
|
|
35
35
|
describe Helpers do
|
36
36
|
subject { Class.new(HelpersSpec::Dummy) }
|
37
|
+
|
37
38
|
let(:proc) do
|
38
39
|
lambda do |*|
|
39
40
|
def test
|
@@ -54,7 +55,7 @@ module Grape
|
|
54
55
|
it 'uses provided modules' do
|
55
56
|
mod = Module.new
|
56
57
|
|
57
|
-
expect(subject).to receive(:namespace_stackable).with(:helpers, kind_of(Grape::DSL::Helpers::BaseHelper)).and_call_original.
|
58
|
+
expect(subject).to receive(:namespace_stackable).with(:helpers, kind_of(Grape::DSL::Helpers::BaseHelper)).and_call_original.twice
|
58
59
|
expect(subject).to receive(:namespace_stackable).with(:helpers).and_call_original
|
59
60
|
subject.helpers(mod, &proc)
|
60
61
|
|
@@ -92,7 +93,7 @@ module Grape
|
|
92
93
|
use :requires_toggle_prm
|
93
94
|
end
|
94
95
|
end
|
95
|
-
end.
|
96
|
+
end.not_to raise_exception
|
96
97
|
end
|
97
98
|
end
|
98
99
|
end
|
@@ -43,6 +43,7 @@ describe Grape::Endpoint do
|
|
43
43
|
before do
|
44
44
|
catch(:error) { subject.error! 'Not Found', 404 }
|
45
45
|
end
|
46
|
+
|
46
47
|
it 'sets status' do
|
47
48
|
expect(subject.status).to eq 404
|
48
49
|
end
|
@@ -53,6 +54,7 @@ describe Grape::Endpoint do
|
|
53
54
|
subject.namespace_inheritable(:default_error_status, 500)
|
54
55
|
catch(:error) { subject.error! 'Unknown' }
|
55
56
|
end
|
57
|
+
|
56
58
|
it 'sets status to default_error_status' do
|
57
59
|
expect(subject.status).to eq 500
|
58
60
|
end
|
@@ -136,7 +138,7 @@ describe Grape::Endpoint do
|
|
136
138
|
end
|
137
139
|
|
138
140
|
it 'accepts unknown Integer status codes' do
|
139
|
-
expect { subject.status 210 }.
|
141
|
+
expect { subject.status 210 }.not_to raise_error
|
140
142
|
end
|
141
143
|
|
142
144
|
it 'raises error if status is not a integer or symbol' do
|
@@ -273,7 +275,7 @@ describe Grape::Endpoint do
|
|
273
275
|
end
|
274
276
|
|
275
277
|
it 'sends no deprecation warnings' do
|
276
|
-
expect(subject).
|
278
|
+
expect(subject).not_to receive(:warn)
|
277
279
|
|
278
280
|
subject.sendfile file_path
|
279
281
|
end
|
@@ -334,7 +336,7 @@ describe Grape::Endpoint do
|
|
334
336
|
end
|
335
337
|
|
336
338
|
it 'emits no deprecation warnings' do
|
337
|
-
expect(subject).
|
339
|
+
expect(subject).not_to receive(:warn)
|
338
340
|
|
339
341
|
subject.stream file_path
|
340
342
|
end
|
@@ -384,7 +386,7 @@ describe Grape::Endpoint do
|
|
384
386
|
end
|
385
387
|
|
386
388
|
it 'emits no deprecation warnings' do
|
387
|
-
expect(subject).
|
389
|
+
expect(subject).not_to receive(:warn)
|
388
390
|
|
389
391
|
subject.stream stream_object
|
390
392
|
end
|
@@ -2,27 +2,25 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
describe Grape::DSL::Logger do
|
6
|
+
subject { Class.new(dummy_logger) }
|
7
|
+
|
8
|
+
let(:dummy_logger) do
|
9
|
+
Class.new do
|
10
|
+
extend Grape::DSL::Logger
|
11
11
|
end
|
12
|
-
|
13
|
-
subject { Class.new(LoggerSpec::Dummy) }
|
14
|
-
let(:logger) { double(:logger) }
|
12
|
+
end
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
let(:logger) { instance_double(::Logger) }
|
15
|
+
|
16
|
+
describe '.logger' do
|
17
|
+
it 'sets a logger' do
|
18
|
+
subject.logger logger
|
19
|
+
expect(subject.logger).to eq logger
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
22
|
+
it 'returns a logger' do
|
23
|
+
expect(subject.logger(logger)).to eq logger
|
26
24
|
end
|
27
25
|
end
|
28
26
|
end
|
@@ -40,6 +40,7 @@ module Grape
|
|
40
40
|
|
41
41
|
def extract_message_option(attrs)
|
42
42
|
return nil unless attrs.is_a?(Array)
|
43
|
+
|
43
44
|
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
|
44
45
|
opts.key?(:message) && !opts[:message].nil? ? opts.delete(:message) : nil
|
45
46
|
end
|
@@ -54,6 +55,7 @@ module Grape
|
|
54
55
|
allow_message_expectations_on_nil
|
55
56
|
allow(subject.api).to receive(:namespace_stackable).with(:named_params)
|
56
57
|
end
|
58
|
+
|
57
59
|
let(:options) { { option: 'value' } }
|
58
60
|
let(:named_params) { { params_group: proc {} } }
|
59
61
|
|
@@ -12,7 +12,8 @@ module Grape
|
|
12
12
|
|
13
13
|
describe Routing do
|
14
14
|
subject { Class.new(RoutingSpec::Dummy) }
|
15
|
-
|
15
|
+
|
16
|
+
let(:proc) { -> {} }
|
16
17
|
let(:options) { { a: :b } }
|
17
18
|
let(:path) { '/dummy' }
|
18
19
|
|
@@ -109,7 +110,7 @@ module Grape
|
|
109
110
|
it 'does not duplicate identical endpoints' do
|
110
111
|
subject.route(:any)
|
111
112
|
expect { subject.route(:any) }
|
112
|
-
.
|
113
|
+
.not_to change(subject.endpoints, :count)
|
113
114
|
end
|
114
115
|
|
115
116
|
it 'generates correct endpoint options' do
|
@@ -233,21 +234,23 @@ module Grape
|
|
233
234
|
allow(subject).to receive(:prepare_routes).and_return(routes)
|
234
235
|
subject.routes
|
235
236
|
end
|
236
|
-
|
237
|
-
|
237
|
+
|
238
|
+
it 'does not call prepare_routes again' do
|
239
|
+
expect(subject).not_to receive(:prepare_routes)
|
238
240
|
expect(subject.routes).to eq routes
|
239
241
|
end
|
240
242
|
end
|
241
243
|
end
|
242
244
|
|
243
245
|
describe '.route_param' do
|
246
|
+
let!(:options) { { requirements: regex } }
|
247
|
+
let(:regex) { /(.*)/ }
|
248
|
+
|
244
249
|
it 'calls #namespace with given params' do
|
245
250
|
expect(subject).to receive(:namespace).with(':foo', {}).and_yield
|
246
251
|
subject.route_param('foo', {}, &proc {})
|
247
252
|
end
|
248
253
|
|
249
|
-
let(:regex) { /(.*)/ }
|
250
|
-
let!(:options) { { requirements: regex } }
|
251
254
|
it 'nests requirements option under param name' do
|
252
255
|
expect(subject).to receive(:namespace) do |_param, options|
|
253
256
|
expect(options[:requirements][:foo]).to eq regex
|
@@ -258,7 +261,7 @@ module Grape
|
|
258
261
|
it 'does not modify options parameter' do
|
259
262
|
allow(subject).to receive(:namespace)
|
260
263
|
expect { subject.route_param('foo', options, &proc {}) }
|
261
|
-
.
|
264
|
+
.not_to change { options }
|
262
265
|
end
|
263
266
|
end
|
264
267
|
|
@@ -87,7 +87,7 @@ describe Grape::Endpoint do
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
it '
|
90
|
+
it 'shows nil for nested params if include_missing is true' do
|
91
91
|
subject.get '/declared' do
|
92
92
|
declared(params, include_missing: true)
|
93
93
|
end
|
@@ -97,7 +97,7 @@ describe Grape::Endpoint do
|
|
97
97
|
expect(JSON.parse(last_response.body)['nested']['fourth']).to be_nil
|
98
98
|
end
|
99
99
|
|
100
|
-
it '
|
100
|
+
it 'shows nil for multiple allowed types if include_missing is true' do
|
101
101
|
subject.get '/declared' do
|
102
102
|
declared(params, include_missing: true)
|
103
103
|
end
|
@@ -568,34 +568,281 @@ describe Grape::Endpoint do
|
|
568
568
|
get '/artists/1'
|
569
569
|
json = JSON.parse(last_response.body, symbolize_names: true)
|
570
570
|
|
571
|
-
expect(json.
|
572
|
-
expect(json.
|
571
|
+
expect(json).to be_key(:id)
|
572
|
+
expect(json).not_to be_key(:artist_id)
|
573
573
|
end
|
574
574
|
|
575
575
|
it 'return only :artist_id without :id' do
|
576
576
|
get '/artists/1/compositions'
|
577
577
|
json = JSON.parse(last_response.body, symbolize_names: true)
|
578
578
|
|
579
|
-
expect(json.
|
580
|
-
expect(json.
|
579
|
+
expect(json).to be_key(:artist_id)
|
580
|
+
expect(json).not_to be_key(:id)
|
581
581
|
end
|
582
582
|
|
583
583
|
it 'return :filter and :id parameters in declared for second enpoint inside route_param' do
|
584
584
|
get '/artists/1/some_route', filter: 'some_filter'
|
585
585
|
json = JSON.parse(last_response.body, symbolize_names: true)
|
586
586
|
|
587
|
-
expect(json.
|
588
|
-
expect(json.
|
589
|
-
expect(json.
|
587
|
+
expect(json).to be_key(:filter)
|
588
|
+
expect(json).to be_key(:id)
|
589
|
+
expect(json).not_to be_key(:artist_id)
|
590
590
|
end
|
591
591
|
|
592
592
|
it 'return :compositor_id for mounter in route_param' do
|
593
593
|
get '/artists/1/albums'
|
594
594
|
json = JSON.parse(last_response.body, symbolize_names: true)
|
595
595
|
|
596
|
-
expect(json.
|
597
|
-
expect(json.
|
598
|
-
expect(json.
|
596
|
+
expect(json).to be_key(:compositor_id)
|
597
|
+
expect(json).not_to be_key(:id)
|
598
|
+
expect(json).not_to be_key(:artist_id)
|
599
|
+
end
|
600
|
+
end
|
601
|
+
|
602
|
+
describe 'parameter renaming' do
|
603
|
+
context 'with a deeply nested parameter structure' do
|
604
|
+
let(:params) do
|
605
|
+
{
|
606
|
+
i_a: 'a',
|
607
|
+
i_b: {
|
608
|
+
i_c: 'c',
|
609
|
+
i_d: {
|
610
|
+
i_e: {
|
611
|
+
i_f: 'f',
|
612
|
+
i_g: 'g',
|
613
|
+
i_h: [
|
614
|
+
{
|
615
|
+
i_ha: 'ha1',
|
616
|
+
i_hb: {
|
617
|
+
i_hc: 'c'
|
618
|
+
}
|
619
|
+
},
|
620
|
+
{
|
621
|
+
i_ha: 'ha2',
|
622
|
+
i_hb: {
|
623
|
+
i_hc: 'c'
|
624
|
+
}
|
625
|
+
}
|
626
|
+
]
|
627
|
+
}
|
628
|
+
}
|
629
|
+
}
|
630
|
+
}
|
631
|
+
end
|
632
|
+
let(:declared) do
|
633
|
+
{
|
634
|
+
o_a: 'a',
|
635
|
+
o_b: {
|
636
|
+
o_c: 'c',
|
637
|
+
o_d: {
|
638
|
+
o_e: {
|
639
|
+
o_f: 'f',
|
640
|
+
o_g: 'g',
|
641
|
+
o_h: [
|
642
|
+
{
|
643
|
+
o_ha: 'ha1',
|
644
|
+
o_hb: {
|
645
|
+
o_hc: 'c'
|
646
|
+
}
|
647
|
+
},
|
648
|
+
{
|
649
|
+
o_ha: 'ha2',
|
650
|
+
o_hb: {
|
651
|
+
o_hc: 'c'
|
652
|
+
}
|
653
|
+
}
|
654
|
+
]
|
655
|
+
}
|
656
|
+
}
|
657
|
+
}
|
658
|
+
}
|
659
|
+
end
|
660
|
+
let(:params_keys) do
|
661
|
+
[
|
662
|
+
'i_a',
|
663
|
+
'i_b',
|
664
|
+
'i_b[i_c]',
|
665
|
+
'i_b[i_d]',
|
666
|
+
'i_b[i_d][i_e]',
|
667
|
+
'i_b[i_d][i_e][i_f]',
|
668
|
+
'i_b[i_d][i_e][i_g]',
|
669
|
+
'i_b[i_d][i_e][i_h]',
|
670
|
+
'i_b[i_d][i_e][i_h][i_ha]',
|
671
|
+
'i_b[i_d][i_e][i_h][i_hb]',
|
672
|
+
'i_b[i_d][i_e][i_h][i_hb][i_hc]'
|
673
|
+
]
|
674
|
+
end
|
675
|
+
|
676
|
+
before do
|
677
|
+
subject.format :json
|
678
|
+
subject.params do
|
679
|
+
optional :i_a, type: String, as: :o_a
|
680
|
+
optional :i_b, type: Hash, as: :o_b do
|
681
|
+
optional :i_c, type: String, as: :o_c
|
682
|
+
optional :i_d, type: Hash, as: :o_d do
|
683
|
+
optional :i_e, type: Hash, as: :o_e do
|
684
|
+
optional :i_f, type: String, as: :o_f
|
685
|
+
optional :i_g, type: String, as: :o_g
|
686
|
+
optional :i_h, type: Array, as: :o_h do
|
687
|
+
optional :i_ha, type: String, as: :o_ha
|
688
|
+
optional :i_hb, type: Hash, as: :o_hb do
|
689
|
+
optional :i_hc, type: String, as: :o_hc
|
690
|
+
end
|
691
|
+
end
|
692
|
+
end
|
693
|
+
end
|
694
|
+
end
|
695
|
+
end
|
696
|
+
subject.post '/test' do
|
697
|
+
declared(params, include_missing: false)
|
698
|
+
end
|
699
|
+
subject.post '/test/no-mod' do
|
700
|
+
before = params.to_h
|
701
|
+
declared(params, include_missing: false)
|
702
|
+
after = params.to_h
|
703
|
+
{ before: before, after: after }
|
704
|
+
end
|
705
|
+
end
|
706
|
+
|
707
|
+
it 'generates the correct parameter names for documentation' do
|
708
|
+
expect(subject.routes.first.params.keys).to match(params_keys)
|
709
|
+
end
|
710
|
+
|
711
|
+
it 'maps the renamed parameter correctly' do
|
712
|
+
post '/test', **params
|
713
|
+
expect(JSON.parse(last_response.body, symbolize_names: true)).to \
|
714
|
+
match(declared)
|
715
|
+
end
|
716
|
+
|
717
|
+
it 'maps no parameters when none are given' do
|
718
|
+
post '/test'
|
719
|
+
expect(JSON.parse(last_response.body)).to match({})
|
720
|
+
end
|
721
|
+
|
722
|
+
it 'does not modify the request params' do
|
723
|
+
post '/test/no-mod', **params
|
724
|
+
result = JSON.parse(last_response.body, symbolize_names: true)
|
725
|
+
expect(result[:before]).to match(result[:after])
|
726
|
+
end
|
727
|
+
end
|
728
|
+
|
729
|
+
context 'with a renamed root parameter' do
|
730
|
+
before do
|
731
|
+
subject.format :json
|
732
|
+
subject.params do
|
733
|
+
optional :email_address, type: String, regexp: /.+@.+/, as: :email
|
734
|
+
end
|
735
|
+
subject.post '/test' do
|
736
|
+
declared(params, include_missing: false)
|
737
|
+
end
|
738
|
+
end
|
739
|
+
|
740
|
+
it 'generates the correct parameter names for documentation' do
|
741
|
+
expect(subject.routes.first.params.keys).to match(%w[email_address])
|
742
|
+
end
|
743
|
+
|
744
|
+
it 'maps the renamed parameter correctly (original name)' do
|
745
|
+
post '/test', email_address: 'test@example.com'
|
746
|
+
expect(JSON.parse(last_response.body)).to \
|
747
|
+
match('email' => 'test@example.com')
|
748
|
+
end
|
749
|
+
|
750
|
+
it 'validates the renamed parameter correctly (original name)' do
|
751
|
+
post '/test', email_address: 'bad[at]example.com'
|
752
|
+
expect(JSON.parse(last_response.body)).to \
|
753
|
+
match('error' => 'email_address is invalid')
|
754
|
+
end
|
755
|
+
|
756
|
+
it 'ignores the renamed parameter (as name)' do
|
757
|
+
post '/test', email: 'test@example.com'
|
758
|
+
expect(JSON.parse(last_response.body)).to match({})
|
759
|
+
end
|
760
|
+
end
|
761
|
+
|
762
|
+
context 'with a renamed hash with nested parameters' do
|
763
|
+
before do
|
764
|
+
subject.format :json
|
765
|
+
subject.params do
|
766
|
+
optional :address, type: Hash, as: :address_attributes do
|
767
|
+
optional :street, type: String, values: ['Street 1', 'Street 2'],
|
768
|
+
default: 'Street 1'
|
769
|
+
optional :city, type: String
|
770
|
+
end
|
771
|
+
end
|
772
|
+
subject.post '/test' do
|
773
|
+
declared(params, include_missing: false)
|
774
|
+
end
|
775
|
+
end
|
776
|
+
|
777
|
+
it 'generates the correct parameter names for documentation' do
|
778
|
+
expect(subject.routes.first.params.keys).to \
|
779
|
+
match(%w[address address[street] address[city]])
|
780
|
+
end
|
781
|
+
|
782
|
+
it 'maps the renamed parameter correctly (original name)' do
|
783
|
+
post '/test', address: { city: 'Berlin', street: 'Street 2', t: 't' }
|
784
|
+
expect(JSON.parse(last_response.body)).to \
|
785
|
+
match('address_attributes' => { 'city' => 'Berlin',
|
786
|
+
'street' => 'Street 2' })
|
787
|
+
end
|
788
|
+
|
789
|
+
it 'validates the renamed parameter correctly (original name)' do
|
790
|
+
post '/test', address: { street: 'unknown' }
|
791
|
+
expect(JSON.parse(last_response.body)).to \
|
792
|
+
match('error' => 'address[street] does not have a valid value')
|
793
|
+
end
|
794
|
+
|
795
|
+
it 'ignores the renamed parameter (as name)' do
|
796
|
+
post '/test', address_attributes: { city: 'Berlin', unknown: '1' }
|
797
|
+
expect(JSON.parse(last_response.body)).to match({})
|
798
|
+
end
|
799
|
+
end
|
800
|
+
|
801
|
+
context 'with a renamed hash with nested renamed parameter' do
|
802
|
+
before do
|
803
|
+
subject.format :json
|
804
|
+
subject.params do
|
805
|
+
optional :user, type: Hash, as: :user_attributes do
|
806
|
+
optional :email_address, type: String, regexp: /.+@.+/, as: :email
|
807
|
+
end
|
808
|
+
end
|
809
|
+
subject.post '/test' do
|
810
|
+
declared(params, include_missing: false)
|
811
|
+
end
|
812
|
+
end
|
813
|
+
|
814
|
+
it 'generates the correct parameter names for documentation' do
|
815
|
+
expect(subject.routes.first.params.keys).to \
|
816
|
+
match(%w[user user[email_address]])
|
817
|
+
end
|
818
|
+
|
819
|
+
it 'maps the renamed parameter correctly (original name)' do
|
820
|
+
post '/test', user: { email_address: 'test@example.com' }
|
821
|
+
expect(JSON.parse(last_response.body)).to \
|
822
|
+
match('user_attributes' => { 'email' => 'test@example.com' })
|
823
|
+
end
|
824
|
+
|
825
|
+
it 'validates the renamed parameter correctly (original name)' do
|
826
|
+
post '/test', user: { email_address: 'bad[at]example.com' }
|
827
|
+
expect(JSON.parse(last_response.body)).to \
|
828
|
+
match('error' => 'user[email_address] is invalid')
|
829
|
+
end
|
830
|
+
|
831
|
+
it 'ignores the renamed parameter (as name, 1)' do
|
832
|
+
post '/test', user: { email: 'test@example.com' }
|
833
|
+
expect(JSON.parse(last_response.body)).to \
|
834
|
+
match({ 'user_attributes' => {} })
|
835
|
+
end
|
836
|
+
|
837
|
+
it 'ignores the renamed parameter (as name, 2)' do
|
838
|
+
post '/test', user_attributes: { email_address: 'test@example.com' }
|
839
|
+
expect(JSON.parse(last_response.body)).to match({})
|
840
|
+
end
|
841
|
+
|
842
|
+
it 'ignores the renamed parameter (as name, 3)' do
|
843
|
+
post '/test', user_attributes: { email: 'test@example.com' }
|
844
|
+
expect(JSON.parse(last_response.body)).to match({})
|
845
|
+
end
|
599
846
|
end
|
600
847
|
end
|
601
848
|
end
|