From 79acba5417265e6ba53d3e4e3f9d7154629789d8 Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Thu, 22 Oct 2009 15:20:27 -0500 Subject: [PATCH] fix for inserting duplicate _id when using C extension and doc responds to has_key? for both '_id' and :_id (MongoMapper related) --- ext/cbson/cbson.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c index 6c69c60..86051c4 100644 --- a/ext/cbson/cbson.c +++ b/ext/cbson/cbson.c @@ -438,15 +438,14 @@ static void write_doc(bson_buffer* buffer, VALUE hash, VALUE check_keys) { int length_location = buffer_save_bytes(buffer, 4); int length; - VALUE key = rb_str_new2("_id"); - if (rb_funcall(hash, rb_intern("has_key?"), 1, key) == Qtrue) { - VALUE id = rb_hash_aref(hash, key); - write_element_allow_id(key, id, pack_extra(buffer, check_keys), 1); - } - key = ID2SYM(rb_intern("_id")); - if (rb_funcall(hash, rb_intern("has_key?"), 1, key) == Qtrue) { - VALUE id = rb_hash_aref(hash, key); - write_element_allow_id(key, id, pack_extra(buffer, check_keys), 1); + VALUE id_str = rb_str_new2("_id"); + VALUE id_sym = ID2SYM(rb_intern("_id")); + if (rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) { + VALUE id = rb_hash_aref(hash, id_str); + write_element_allow_id(id_str, id, pack_extra(buffer, check_keys), 1); + } else if (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue) { + VALUE id = rb_hash_aref(hash, id_sym); + write_element_allow_id(id_sym, id, pack_extra(buffer, check_keys), 1); } // we have to check for an OrderedHash and handle that specially