Skip to content
This repository has been archived by the owner on Oct 13, 2020. It is now read-only.

Commit

Permalink
CDRIVER-1947 $-keys at top level are ok
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdavis committed Feb 24, 2017
1 parent 0a06f04 commit 6c3e657
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/bson/bson-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,9 @@ _bson_json_read_map_key (bson_json_reader_t *reader, /* IN */
}

if (bson->read_state == BSON_JSON_IN_START_MAP) {
if (len > 0 && val[0] == '$' && _is_known_key ((const char *) val, len)) {
if (len > 0 && val[0] == '$' &&
_is_known_key ((const char *) val, len) &&
bson->n >= 0 /* key is in subdocument */) {
bson->read_state = BSON_JSON_IN_BSON_TYPE;
bson->bson_type = (bson_type_t) 0;
memset (&bson->bson_type_data, 0, sizeof bson->bson_type_data);
Expand Down
27 changes: 1 addition & 26 deletions tests/test-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1754,36 +1754,14 @@ test_bson_json_timestamp (void)
fprintf (stderr, "%s\n", error.message);
}

assert (r);
BSON_ASSERT (r);

bson_eq_bson (&b, &compare);
bson_destroy (&compare);
bson_destroy (&b);
}


static void
test_bson_json_special_keys_at_top (void)
{
const char *invalid[] = {
/* needs outer key name, like {x: {$date: {$numberLong: "1234"}}} */
"{ \"$date\" : { \"$numberLong\" : \"1234\" } }",
"{ \"$oid\" : { \"$numberLong\" : \"1234\" } }",
"{ \"$oid\" : \"563b029096f9bc35c61df3a3\" }",
};

bson_t b;
bson_error_t error;
int i;

for (i = 0; i < sizeof (invalid) / sizeof (const char *); i++) {
BSON_ASSERT (!bson_init_from_json (&b, invalid[i], -1, &error));
ASSERT_CMPINT (error.domain, ==, BSON_ERROR_JSON);
ASSERT_CMPINT (error.code, ==, BSON_JSON_ERROR_READ_CORRUPT_JS);
ASSERT_CMPSTR (error.message, "Invalid MongoDB extended JSON");
}
}

static void
test_bson_array_as_json (void)
{
Expand Down Expand Up @@ -1861,9 +1839,6 @@ test_json_install (TestSuite *suite)
TestSuite_Add (
suite, "/bson/as_json/binary_order", test_bson_json_binary_order);
TestSuite_Add (suite, "/bson/as_json_spacing", test_bson_as_json_spacing);
TestSuite_Add (suite,
"/bson/as_json_special_keys_at_top",
test_bson_json_special_keys_at_top);
TestSuite_Add (suite, "/bson/array_as_json", test_bson_array_as_json);
TestSuite_Add (
suite, "/bson/json/allow_multiple", test_bson_json_allow_multiple);
Expand Down

0 comments on commit 6c3e657

Please sign in to comment.