Skip to content

Commit

Permalink
Fixed parsing of empty collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
tszolar committed Jul 17, 2014
1 parent 9746394 commit e526e9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/kosapi_client/entity/data_mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,15 @@ def set_mapped_attribute(instance, name, source_hash, mapping_options)
value = source_hash[key]
if value.nil?
raise "Missing value for attribute #{name}" if mapping_options[:required]
return
if mapping_options[:type].is_a?(Array)
value = []
else
return
end
else
value = convert_type(value, mapping_options[:type])
end
value = convert_type(value, mapping_options[:type])
instance.send("#{name}=".to_sym, value)
instance.send("#{name}=".to_sym, value)
end

def convert_type(value, type)
Expand Down
6 changes: 6 additions & 0 deletions spec/kosapi_client/entity/data_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
expect(parsed.foo).to eq :bar
end

it 'parses nil collection as empty array' do
dummy_class.map_data :foo, [Integer]
parsed = dummy_class.parse({ })
expect(parsed.foo).to eq []
end

end

describe '.map_data' do
Expand Down

0 comments on commit e526e9d

Please sign in to comment.