Skip to content

Commit f299bd0

Browse files
committed
Fix coercion for array type: do not coerce array value if type is the same as specified
1 parent 51f5c1b commit f299bd0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/shallow_attributes/type/array.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def coerce(values, options = {})
2828
raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{values}" for type "Array")
2929
end
3030
values.map! do |value|
31-
ShallowAttributes::Type.coerce(item_klass(options[:of]), value)
31+
klass_const = item_klass(options[:of])
32+
value.is_a?(klass_const) ? value : ShallowAttributes::Type.coerce(klass_const, value)
3233
end
3334
end
3435

test/array_type_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@
2727
end
2828
end
2929

30-
describe 'when value is Array' do
30+
describe 'when value is Array of specified types' do
31+
let(:arbitrary_class) { Class.new }
32+
let(:arbitrary_value) { arbitrary_class.new }
33+
34+
it 'returns array of non-coerced values' do
35+
type.coerce([arbitrary_value], of: arbitrary_class).must_equal [arbitrary_value]
36+
end
37+
end
38+
39+
describe 'when value is Array of non-specified types' do
3140
it 'returns array of specific type' do
3241
type.coerce([], of: Integer).must_equal []
3342
type.coerce(['1', '2'], of: Integer).must_equal [1, 2]

0 commit comments

Comments
 (0)