grape 1.5.2 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +47 -0
  3. data/CONTRIBUTING.md +2 -1
  4. data/README.md +33 -3
  5. data/UPGRADING.md +71 -2
  6. data/grape.gemspec +5 -5
  7. data/lib/grape/api/instance.rb +13 -17
  8. data/lib/grape/api.rb +18 -13
  9. data/lib/grape/cookies.rb +2 -0
  10. data/lib/grape/dsl/desc.rb +3 -5
  11. data/lib/grape/dsl/headers.rb +5 -2
  12. data/lib/grape/dsl/helpers.rb +7 -5
  13. data/lib/grape/dsl/inside_route.rb +17 -8
  14. data/lib/grape/dsl/middleware.rb +4 -4
  15. data/lib/grape/dsl/parameters.rb +3 -3
  16. data/lib/grape/dsl/request_response.rb +9 -6
  17. data/lib/grape/dsl/routing.rb +2 -2
  18. data/lib/grape/dsl/settings.rb +5 -5
  19. data/lib/grape/endpoint.rb +21 -36
  20. data/lib/grape/error_formatter/json.rb +2 -6
  21. data/lib/grape/error_formatter/xml.rb +2 -6
  22. data/lib/grape/exceptions/empty_message_body.rb +11 -0
  23. data/lib/grape/exceptions/validation.rb +1 -2
  24. data/lib/grape/formatter/json.rb +1 -0
  25. data/lib/grape/formatter/serializable_hash.rb +2 -1
  26. data/lib/grape/formatter/xml.rb +1 -0
  27. data/lib/grape/locale/en.yml +1 -1
  28. data/lib/grape/middleware/auth/dsl.rb +7 -1
  29. data/lib/grape/middleware/base.rb +3 -1
  30. data/lib/grape/middleware/formatter.rb +4 -4
  31. data/lib/grape/middleware/stack.rb +2 -2
  32. data/lib/grape/middleware/versioner/accept_version_header.rb +3 -5
  33. data/lib/grape/middleware/versioner/header.rb +6 -4
  34. data/lib/grape/middleware/versioner/param.rb +1 -0
  35. data/lib/grape/middleware/versioner/parse_media_type_patch.rb +2 -1
  36. data/lib/grape/middleware/versioner/path.rb +2 -0
  37. data/lib/grape/parser/json.rb +1 -1
  38. data/lib/grape/parser/xml.rb +1 -1
  39. data/lib/grape/path.rb +1 -0
  40. data/lib/grape/request.rb +3 -0
  41. data/lib/grape/router/pattern.rb +1 -1
  42. data/lib/grape/router/route.rb +2 -2
  43. data/lib/grape/router.rb +6 -0
  44. data/lib/grape/util/inheritable_setting.rb +1 -3
  45. data/lib/grape/util/lazy_value.rb +3 -2
  46. data/lib/grape/util/strict_hash_configuration.rb +1 -1
  47. data/lib/grape/validations/params_scope.rb +88 -55
  48. data/lib/grape/validations/types/custom_type_coercer.rb +1 -2
  49. data/lib/grape/validations/types/dry_type_coercer.rb +1 -1
  50. data/lib/grape/validations/types/json.rb +2 -1
  51. data/lib/grape/validations/types/primitive_coercer.rb +3 -3
  52. data/lib/grape/validations/validators/all_or_none.rb +8 -5
  53. data/lib/grape/validations/validators/allow_blank.rb +9 -7
  54. data/lib/grape/validations/validators/as.rb +6 -8
  55. data/lib/grape/validations/validators/at_least_one_of.rb +7 -4
  56. data/lib/grape/validations/validators/base.rb +75 -70
  57. data/lib/grape/validations/validators/coerce.rb +63 -79
  58. data/lib/grape/validations/validators/default.rb +37 -34
  59. data/lib/grape/validations/validators/exactly_one_of.rb +9 -6
  60. data/lib/grape/validations/validators/except_values.rb +13 -11
  61. data/lib/grape/validations/validators/multiple_params_base.rb +24 -20
  62. data/lib/grape/validations/validators/mutual_exclusion.rb +8 -5
  63. data/lib/grape/validations/validators/presence.rb +7 -4
  64. data/lib/grape/validations/validators/regexp.rb +8 -5
  65. data/lib/grape/validations/validators/same_as.rb +18 -15
  66. data/lib/grape/validations/validators/values.rb +61 -56
  67. data/lib/grape/validations.rb +6 -0
  68. data/lib/grape/version.rb +1 -1
  69. data/lib/grape.rb +4 -1
  70. data/spec/grape/api/custom_validations_spec.rb +77 -45
  71. data/spec/grape/api/deeply_included_options_spec.rb +3 -3
  72. data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -1
  73. data/spec/grape/api/invalid_format_spec.rb +2 -0
  74. data/spec/grape/api/recognize_path_spec.rb +1 -1
  75. data/spec/grape/api/routes_with_requirements_spec.rb +8 -8
  76. data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -15
  77. data/spec/grape/api_remount_spec.rb +16 -15
  78. data/spec/grape/api_spec.rb +510 -220
  79. data/spec/grape/dsl/callbacks_spec.rb +2 -1
  80. data/spec/grape/dsl/headers_spec.rb +39 -9
  81. data/spec/grape/dsl/helpers_spec.rb +3 -2
  82. data/spec/grape/dsl/inside_route_spec.rb +6 -4
  83. data/spec/grape/dsl/logger_spec.rb +16 -18
  84. data/spec/grape/dsl/middleware_spec.rb +2 -1
  85. data/spec/grape/dsl/parameters_spec.rb +2 -0
  86. data/spec/grape/dsl/request_response_spec.rb +1 -0
  87. data/spec/grape/dsl/routing_spec.rb +10 -7
  88. data/spec/grape/endpoint/declared_spec.rb +259 -12
  89. data/spec/grape/endpoint_spec.rb +77 -55
  90. data/spec/grape/entity_spec.rb +22 -22
  91. data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -0
  92. data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -22
  93. data/spec/grape/exceptions/validation_errors_spec.rb +13 -10
  94. data/spec/grape/exceptions/validation_spec.rb +5 -3
  95. data/spec/grape/extensions/param_builders/hash_spec.rb +7 -7
  96. data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -8
  97. data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -8
  98. data/spec/grape/integration/rack_sendfile_spec.rb +1 -1
  99. data/spec/grape/loading_spec.rb +8 -8
  100. data/spec/grape/middleware/auth/dsl_spec.rb +15 -6
  101. data/spec/grape/middleware/auth/strategies_spec.rb +60 -20
  102. data/spec/grape/middleware/base_spec.rb +24 -15
  103. data/spec/grape/middleware/error_spec.rb +2 -2
  104. data/spec/grape/middleware/exception_spec.rb +111 -161
  105. data/spec/grape/middleware/formatter_spec.rb +27 -6
  106. data/spec/grape/middleware/globals_spec.rb +7 -4
  107. data/spec/grape/middleware/stack_spec.rb +14 -12
  108. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -1
  109. data/spec/grape/middleware/versioner/header_spec.rb +14 -13
  110. data/spec/grape/middleware/versioner/param_spec.rb +7 -1
  111. data/spec/grape/middleware/versioner/path_spec.rb +5 -1
  112. data/spec/grape/middleware/versioner_spec.rb +1 -1
  113. data/spec/grape/parser_spec.rb +4 -0
  114. data/spec/grape/path_spec.rb +52 -52
  115. data/spec/grape/presenters/presenter_spec.rb +7 -6
  116. data/spec/grape/request_spec.rb +6 -4
  117. data/spec/grape/util/inheritable_setting_spec.rb +7 -7
  118. data/spec/grape/util/inheritable_values_spec.rb +3 -2
  119. data/spec/grape/util/reverse_stackable_values_spec.rb +3 -1
  120. data/spec/grape/util/stackable_values_spec.rb +7 -5
  121. data/spec/grape/validations/instance_behaivour_spec.rb +9 -10
  122. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +1 -0
  123. data/spec/grape/validations/params_scope_spec.rb +46 -10
  124. data/spec/grape/validations/single_attribute_iterator_spec.rb +2 -1
  125. data/spec/grape/validations/types/primitive_coercer_spec.rb +4 -4
  126. data/spec/grape/validations/types_spec.rb +8 -8
  127. data/spec/grape/validations/validators/all_or_none_spec.rb +50 -56
  128. data/spec/grape/validations/validators/allow_blank_spec.rb +136 -140
  129. data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -56
  130. data/spec/grape/validations/validators/coerce_spec.rb +99 -22
  131. data/spec/grape/validations/validators/default_spec.rb +72 -78
  132. data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
  133. data/spec/grape/validations/validators/except_values_spec.rb +3 -3
  134. data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -77
  135. data/spec/grape/validations/validators/presence_spec.rb +16 -1
  136. data/spec/grape/validations/validators/regexp_spec.rb +25 -31
  137. data/spec/grape/validations/validators/same_as_spec.rb +14 -20
  138. data/spec/grape/validations/validators/values_spec.rb +183 -178
  139. data/spec/grape/validations_spec.rb +99 -58
  140. data/spec/integration/eager_load/eager_load_spec.rb +2 -2
  141. data/spec/integration/multi_json/json_spec.rb +1 -1
  142. data/spec/integration/multi_xml/xml_spec.rb +1 -1
  143. data/spec/shared/versioning_examples.rb +12 -9
  144. data/spec/spec_helper.rb +12 -2
  145. data/spec/support/basic_auth_encode_helpers.rb +1 -1
  146. metadata +102 -101
@@ -12,7 +12,8 @@ module Grape
12
12
 
13
13
  describe Callbacks do
14
14
  subject { Class.new(CallbacksSpec::Dummy) }
15
- let(:proc) { ->() {} }
15
+
16
+ let(:proc) { -> {} }
16
17
 
17
18
  describe '.before' do
18
19
  it 'adds a block to "before"' do
@@ -12,21 +12,51 @@ module Grape
12
12
  describe Headers do
13
13
  subject { HeadersSpec::Dummy.new }
14
14
 
15
- describe '#header' do
16
- describe 'set' do
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 'Name', 'Value'
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
- it 'returns value' do
22
- expect(subject.header['Name']).to eq 'Value'
23
- expect(subject.header('Name')).to eq 'Value'
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
- it 'returns nil' do
28
- expect(subject.header['Name']).to be nil
29
- expect(subject.header('Name')).to be nil
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.exactly(2).times
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.to_not raise_exception
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 }.to_not raise_error
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).to_not receive(:warn)
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).to_not receive(:warn)
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).to_not receive(:warn)
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
- module Grape
6
- module DSL
7
- module LoggerSpec
8
- class Dummy
9
- extend Grape::DSL::Logger
10
- end
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
- describe Logger do
13
- subject { Class.new(LoggerSpec::Dummy) }
14
- let(:logger) { double(:logger) }
12
+ end
15
13
 
16
- describe '.logger' do
17
- it 'sets a logger' do
18
- subject.logger logger
19
- expect(subject.logger).to eq logger
20
- end
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
- it 'returns a logger' do
23
- expect(subject.logger(logger)).to eq logger
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
@@ -12,7 +12,8 @@ module Grape
12
12
 
13
13
  describe Middleware do
14
14
  subject { Class.new(MiddlewareSpec::Dummy) }
15
- let(:proc) { ->() {} }
15
+
16
+ let(:proc) { -> {} }
16
17
  let(:foo_middleware) { Class.new }
17
18
  let(:bar_middleware) { Class.new }
18
19
 
@@ -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
 
@@ -20,6 +20,7 @@ module Grape
20
20
 
21
21
  describe RequestResponse do
22
22
  subject { Class.new(RequestResponseSpec::Dummy) }
23
+
23
24
  let(:c_type) { 'application/json' }
24
25
  let(:format) { 'txt' }
25
26
 
@@ -12,7 +12,8 @@ module Grape
12
12
 
13
13
  describe Routing do
14
14
  subject { Class.new(RoutingSpec::Dummy) }
15
- let(:proc) { ->() {} }
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
- .to_not change(subject.endpoints, :count)
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
- it 'it does not call prepare_routes again' do
237
- expect(subject).to_not receive(:prepare_routes)
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
- .to_not change { options }
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 'should show nil for nested params if include_missing is true' do
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 'should show nil for multiple allowed types if include_missing is true' do
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.key?(:id)).to be_truthy
572
- expect(json.key?(:artist_id)).not_to be_truthy
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.key?(:artist_id)).to be_truthy
580
- expect(json.key?(:id)).not_to be_truthy
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.key?(:filter)).to be_truthy
588
- expect(json.key?(:id)).to be_truthy
589
- expect(json.key?(:artist_id)).not_to be_truthy
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.key?(:compositor_id)).to be_truthy
597
- expect(json.key?(:id)).not_to be_truthy
598
- expect(json.key?(:artist_id)).not_to be_truthy
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