Skip to content

Commit 139e96b

Browse files
authored
Merge pull request #2507 from ibat-alliance/master
Fix Set type with values
2 parents f4e2af5 + de0a431 commit 139e96b

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
* [#2504](https://github.com/ruby-grape/grape/pull/2504): Fix leaky modules in specs - [@ericproulx](https://github.com/ericproulx).
1414
* [#2506](https://github.com/ruby-grape/grape/pull/2506): Fix fetch_formatter api_format - [@ericproulx](https://github.com/ericproulx).
15+
* [#2507](https://github.com/ruby-grape/grape/pull/2507): Fix type: Set with values - [@nikolai-b](https://github.com/nikolai-b).
1516
* Your contribution here.
1617

1718
### 2.2.0 (2024-09-14)

lib/grape/validations/params_scope.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def validate(type, options, attrs, doc, opts)
490490
def validate_value_coercion(coerce_type, *values_list)
491491
return unless coerce_type
492492

493-
coerce_type = coerce_type.first if coerce_type.is_a?(Array)
493+
coerce_type = coerce_type.first if coerce_type.is_a?(Enumerable)
494494
values_list.each do |values|
495495
next if !values || values.is_a?(Proc)
496496

spec/grape/validations/params_scope_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,24 @@ def initialize(value)
189189
end
190190
end
191191

192+
context 'a Set with coerce type explicitly given' do
193+
context 'and the values are allowed' do
194+
it 'does not raise an exception' do
195+
expect do
196+
subject.params { optional :numbers, type: Set[Integer], values: 0..2, default: 0..2 }
197+
end.not_to raise_error
198+
end
199+
end
200+
201+
context 'and the values are not allowed' do
202+
it 'raises exception' do
203+
expect do
204+
subject.params { optional :numbers, type: Set[Integer], values: %w[a b] }
205+
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
206+
end
207+
end
208+
end
209+
192210
context 'with range values' do
193211
context "when left range endpoint isn't #kind_of? the type" do
194212
it 'raises exception' do

0 commit comments

Comments
 (0)