From de0a43176d85f30a8fa50f12f2b12c131275f5b4 Mon Sep 17 00:00:00 2001 From: Nikolai B Date: Wed, 23 Oct 2024 12:58:19 +0100 Subject: [PATCH] Fix type: Set with values --- CHANGELOG.md | 1 + lib/grape/validations/params_scope.rb | 2 +- spec/grape/validations/params_scope_spec.rb | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7aea934087..a66dacc322 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * [#2504](https://github.com/ruby-grape/grape/pull/2504): Fix leaky modules in specs - [@ericproulx](https://github.com/ericproulx). * [#2506](https://github.com/ruby-grape/grape/pull/2506): Fix fetch_formatter api_format - [@ericproulx](https://github.com/ericproulx). +* [#2507](https://github.com/ruby-grape/grape/pull/2507): Fix type: Set with values - [@nikolai-b](https://github.com/nikolai-b). * Your contribution here. ### 2.2.0 (2024-09-14) diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index 1d3384883c..b3c4268f61 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -490,7 +490,7 @@ def validate(type, options, attrs, doc, opts) def validate_value_coercion(coerce_type, *values_list) return unless coerce_type - coerce_type = coerce_type.first if coerce_type.is_a?(Array) + coerce_type = coerce_type.first if coerce_type.is_a?(Enumerable) values_list.each do |values| next if !values || values.is_a?(Proc) diff --git a/spec/grape/validations/params_scope_spec.rb b/spec/grape/validations/params_scope_spec.rb index 454fdf6c03..8e26f6fa6f 100644 --- a/spec/grape/validations/params_scope_spec.rb +++ b/spec/grape/validations/params_scope_spec.rb @@ -189,6 +189,24 @@ def initialize(value) end end + context 'a Set with coerce type explicitly given' do + context 'and the values are allowed' do + it 'does not raise an exception' do + expect do + subject.params { optional :numbers, type: Set[Integer], values: 0..2, default: 0..2 } + end.not_to raise_error + end + end + + context 'and the values are not allowed' do + it 'raises exception' do + expect do + subject.params { optional :numbers, type: Set[Integer], values: %w[a b] } + end.to raise_error Grape::Exceptions::IncompatibleOptionValues + end + end + end + context 'with range values' do context "when left range endpoint isn't #kind_of? the type" do it 'raises exception' do