Fixed BSON serialization bug: was modifying objects with ids.

This commit is contained in:
Jim Menard 2009-01-28 15:38:31 -05:00
parent 128b11e92c
commit dc3ef378d3
3 changed files with 17 additions and 3 deletions

View File

@ -80,9 +80,9 @@ class BSON
@buf.put_int(0)
# Write key/value pairs. Always write _id first if it exists.
oid = obj.delete('_id') || obj.delete(:_id)
oid = obj['_id'] || obj[:_id]
serialize_key_value('_id', oid) if oid
obj.each {|k, v| serialize_key_value(k, v) }
obj.each {|k, v| serialize_key_value(k, v) unless k == '_id' || k == :_id }
serialize_eoo_element(@buf)
@buf.put_int(@buf.size, 0)

View File

@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'mongo'
s.version = '0.3.0'
s.version = '0.3.1'
s.platform = Gem::Platform::RUBY
s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'

View File

@ -155,4 +155,18 @@ class BSONTest < Test::Unit::TestCase
assert_equal '_id', roundtrip.keys.first
end
def test_do_not_change_original_object
val = OrderedHash.new
val['not_id'] = 1
val['_id'] = 2
assert val.keys.include?('_id')
@b.serialize(val)
assert val.keys.include?('_id')
val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
assert val.keys.include?(:_id)
@b.serialize(val)
assert val.keys.include?(:_id)
end
end