From dc3ef378d3254001310b4203f784bd1d3eec09d6 Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Wed, 28 Jan 2009 15:38:31 -0500 Subject: [PATCH] Fixed BSON serialization bug: was modifying objects with ids. --- lib/mongo/util/bson.rb | 4 ++-- mongo-ruby-driver.gemspec | 2 +- tests/test_bson.rb | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index 53af04b..6569521 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -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) diff --git a/mongo-ruby-driver.gemspec b/mongo-ruby-driver.gemspec index e66d07d..3ee6624 100644 --- a/mongo-ruby-driver.gemspec +++ b/mongo-ruby-driver.gemspec @@ -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.' diff --git a/tests/test_bson.rb b/tests/test_bson.rb index f214c38..e58b035 100644 --- a/tests/test_bson.rb +++ b/tests/test_bson.rb @@ -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