Skip to content

Commit bc7db69

Browse files
committed
Merge pull request #995 from u2/jordansexton_patch-1
Added support for coercion to Set or Set[Other]
2 parents 34ac1ed + 26361ac commit bc7db69

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#### Features
55

6+
* [#995](https://github.com/intridea/grape/issues/995): Added support for coercion to Set or Set[Other] - [@jordansexton](https://github.com/jordansexton) [@u2](https://github.com/u2).
67
* [#980](https://github.com/intridea/grape/issues/980): Grape is now eager-loaded - [@u2](https://github.com/u2).
78
* [#956](https://github.com/intridea/grape/issues/956): Support `present` with `Grape::Presenters::Presenter` - [@u2](https://github.com/u2).
89
* [#974](https://github.com/intridea/grape/pull/974): Added `error!` to `rescue_from` blocks - [@whatasunnyday](https://github.com/whatasunnyday).

lib/grape/validations/validators/coerce.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ def _valid_single_type?(klass, val)
4141
end
4242

4343
def valid_type?(val)
44-
if @option.is_a?(Array)
45-
_valid_array_type?(@option[0], val)
44+
if @option.is_a?(Array) || @option.is_a?(Set)
45+
_valid_array_type?(@option.first, val)
4646
else
4747
_valid_single_type?(@option, val)
4848
end
4949
end
5050

5151
def coerce_value(type, val)
5252
# Don't coerce things other than nil to Arrays or Hashes
53-
return val || [] if type == Array
54-
return val || {} if type == Hash
53+
return val || [] if type == Array
54+
return val || Set.new if type == Set
55+
return val || {} if type == Hash
5556

5657
converter = Virtus::Attribute.build(type)
5758
converter.coerce(val)

spec/grape/validations/validators/coerce_spec.rb

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,60 @@ class User
122122
expect(last_response.body).to eq('Fixnum')
123123
end
124124

125-
it 'Array of Integers' do
126-
subject.params do
127-
requires :arry, coerce: Array[Integer]
128-
end
129-
subject.get '/array' do
130-
params[:arry][0].class
125+
context 'Array' do
126+
it 'Array of Integers' do
127+
subject.params do
128+
requires :arry, coerce: Array[Integer]
129+
end
130+
subject.get '/array' do
131+
params[:arry][0].class
132+
end
133+
134+
get '/array', arry: %w(1 2 3)
135+
expect(last_response.status).to eq(200)
136+
expect(last_response.body).to eq('Fixnum')
131137
end
132138

133-
get '/array', arry: %w(1 2 3)
134-
expect(last_response.status).to eq(200)
135-
expect(last_response.body).to eq('Fixnum')
136-
end
139+
it 'Array of Bools' do
140+
subject.params do
141+
requires :arry, coerce: Array[Virtus::Attribute::Boolean]
142+
end
143+
subject.get '/array' do
144+
params[:arry][0].class
145+
end
137146

138-
it 'Array of Bools' do
139-
subject.params do
140-
requires :arry, coerce: Array[Virtus::Attribute::Boolean]
147+
get 'array', arry: [1, 0]
148+
expect(last_response.status).to eq(200)
149+
expect(last_response.body).to eq('TrueClass')
141150
end
142-
subject.get '/array' do
143-
params[:arry][0].class
151+
end
152+
153+
context 'Set' do
154+
it 'Set of Integers' do
155+
subject.params do
156+
requires :set, coerce: Set[Integer]
157+
end
158+
subject.get '/set' do
159+
params[:set].first.class
160+
end
161+
162+
get '/set', set: Set.new([1, 2, 3, 4]).to_a
163+
expect(last_response.status).to eq(200)
164+
expect(last_response.body).to eq('Fixnum')
144165
end
145166

146-
get 'array', arry: [1, 0]
147-
expect(last_response.status).to eq(200)
148-
expect(last_response.body).to eq('TrueClass')
167+
it 'Set of Bools' do
168+
subject.params do
169+
requires :set, coerce: Set[Virtus::Attribute::Boolean]
170+
end
171+
subject.get '/set' do
172+
params[:set].first.class
173+
end
174+
175+
get '/set', set: Set.new([1, 0]).to_a
176+
expect(last_response.status).to eq(200)
177+
expect(last_response.body).to eq('TrueClass')
178+
end
149179
end
150180

151181
it 'Bool' do

0 commit comments

Comments
 (0)