Skip to content

Commit 76730d4

Browse files
committed
RUBY-233 show invalid key on invalid key exception
1 parent 0c574b9 commit 76730d4

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

ext/cbson/cbson.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
231231
int i;
232232
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
233233
buffer_free(buffer);
234-
rb_raise(InvalidKeyName, "key must not start with '$'");
234+
rb_raise(InvalidKeyName, "%s - key must not start with '$'", RSTRING_PTR(key));
235235
}
236236
for (i = 0; i < RSTRING_LEN(key); i++) {
237237
if (RSTRING_PTR(key)[i] == '.') {
238238
buffer_free(buffer);
239-
rb_raise(InvalidKeyName, "key must not contain '.'");
239+
rb_raise(InvalidKeyName, "%s - key must not contain '.'", RSTRING_PTR(key));
240240
}
241241
}
242242
}

test/bson/bson_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,47 @@ def test_move_id_with_nested_doc
562562
end
563563
end
564564

565+
def test_invalid_key_names
566+
assert @encoder.serialize({"hello" => "world"}, true)
567+
assert @encoder.serialize({"hello" => {"hello" => "world"}}, true)
568+
569+
assert @encoder.serialize({"he$llo" => "world"}, true)
570+
assert @encoder.serialize({"hello" => {"hell$o" => "world"}}, true)
571+
572+
assert_raise BSON::InvalidDocument do
573+
@encoder.serialize({"he\0llo" => "world"}, true)
574+
end
575+
576+
assert_raise_error BSON::InvalidKeyName, "$hello" do
577+
@encoder.serialize({"$hello" => "world"}, true)
578+
end
579+
580+
assert_raise BSON::InvalidKeyName do
581+
@encoder.serialize({"hello" => {"$hello" => "world"}}, true)
582+
end
583+
584+
assert_raise_error BSON::InvalidKeyName, ".hello" do
585+
@encoder.serialize({".hello" => "world"}, true)
586+
end
587+
588+
assert_raise BSON::InvalidKeyName do
589+
@encoder.serialize({"hello" => {".hello" => "world"}}, true)
590+
end
591+
592+
assert_raise BSON::InvalidKeyName do
593+
@encoder.serialize({"hello." => "world"}, true)
594+
end
595+
596+
assert_raise BSON::InvalidKeyName do
597+
@encoder.serialize({"hello" => {"hello." => "world"}}, true)
598+
end
599+
600+
assert_raise BSON::InvalidKeyName do
601+
@encoder.serialize({"hel.lo" => "world"}, true)
602+
end
603+
604+
assert_raise BSON::InvalidKeyName do
605+
@encoder.serialize({"hello" => {"hel.lo" => "world"}}, true)
606+
end
607+
end
565608
end

test/db_api_test.rb

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -621,43 +621,6 @@ def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
621621
assert_equal("mike", @@coll.find_one()["hello"])
622622
end
623623

624-
def test_invalid_key_names
625-
@@coll.remove
626-
627-
@@coll.insert({"hello" => "world"})
628-
@@coll.insert({"hello" => {"hello" => "world"}})
629-
630-
assert_raise BSON::InvalidKeyName do
631-
@@coll.insert({"$hello" => "world"})
632-
end
633-
634-
assert_raise BSON::InvalidKeyName do
635-
@@coll.insert({"hello" => {"$hello" => "world"}})
636-
end
637-
638-
@@coll.insert({"he$llo" => "world"})
639-
@@coll.insert({"hello" => {"hell$o" => "world"}})
640-
641-
assert_raise BSON::InvalidKeyName do
642-
@@coll.insert({".hello" => "world"})
643-
end
644-
assert_raise BSON::InvalidKeyName do
645-
@@coll.insert({"hello" => {".hello" => "world"}})
646-
end
647-
assert_raise BSON::InvalidKeyName do
648-
@@coll.insert({"hello." => "world"})
649-
end
650-
assert_raise BSON::InvalidKeyName do
651-
@@coll.insert({"hello" => {"hello." => "world"}})
652-
end
653-
assert_raise BSON::InvalidKeyName do
654-
@@coll.insert({"hel.lo" => "world"})
655-
end
656-
assert_raise BSON::InvalidKeyName do
657-
@@coll.insert({"hello" => {"hel.lo" => "world"}})
658-
end
659-
end
660-
661624
def test_collection_names
662625
assert_raise TypeError do
663626
@@db.collection(5)

0 commit comments

Comments
 (0)