Our iter functions handle empty keys in different ways.
- bson_iter_init_find finds empty keys
- bson_iter_init_find_w_len does not find empty keys (i.e. trying to find by passing length 0 won't find an empty key in a document)
- bson_iter_find_descendant can't descend into empty keys
Example below:
static void test_bson_iter_empty_key (void) { /* create the bson document { "" : { "x" : 1 } } */ bson_t* bson = BCON_NEW("", "{", "x", BCON_INT32(1), "}"); bson_iter_t iter; bson_iter_t descendant; /* a find works, because internally it passes -1 to bson_iter_init_find_w_len */ BSON_ASSERT(bson_iter_init_find (&iter, bson, "")); BSON_ASSERT(BSON_ITER_HOLDS_DOCUMENT (&iter)); /* a find_w_len and length 0 does *not* work, because we return early. */ BSON_ASSERT(!bson_iter_init_find_w_len (&iter, bson, "", 0)); /* similarly, we can't descend if we've iterated on an empty key. */ bson_iter_init (&iter, bson); BSON_ASSERT(!bson_iter_find_descendant (&iter, ".x", &descendant)); }
Note, this wasn't introduced in CDRIVER-2414. bson_iter_find_descendant had the same behavior.
I propose we change bson_iter_find_descendant and bson_iter_init_find_w_len to find empty keys. This also makes it possible to do CXX-1500 and keep behavior of finding empty keys in the C++ driver.
- is depended on by
-
CXX-1500 document::view::find should use bson_iter...w_len functions
- Closed