Skip to content

Commit f6c7c1a

Browse files
aherlihyhanumantmk
authored andcommitted
CDRIVER-443 added validate to bulk_insert
Closes #113
1 parent 021a050 commit f6c7c1a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/mongoc/mongoc-collection.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,28 @@ mongoc_collection_insert_bulk (mongoc_collection_t *collection,
10891089
write_concern = collection->write_concern;
10901090
}
10911091

1092+
if (!(flags & MONGOC_INSERT_NO_VALIDATE)) {
1093+
int i;
1094+
1095+
for (i = 0; i < n_documents; i++) {
1096+
if (!bson_validate (documents[i],
1097+
(BSON_VALIDATE_UTF8 |
1098+
BSON_VALIDATE_UTF8_ALLOW_NULL |
1099+
BSON_VALIDATE_DOLLAR_KEYS |
1100+
BSON_VALIDATE_DOT_KEYS),
1101+
NULL)) {
1102+
bson_set_error (error,
1103+
MONGOC_ERROR_BSON,
1104+
MONGOC_ERROR_BSON_INVALID,
1105+
"A document was corrupt or contained "
1106+
"invalid characters . or $");
1107+
RETURN (false);
1108+
}
1109+
}
1110+
} else {
1111+
flags &= ~MONGOC_INSERT_NO_VALIDATE;
1112+
}
1113+
10921114
bson_clear (&collection->gle);
10931115

10941116
_mongoc_write_result_init (&result);

tests/test-mongoc-collection.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ test_insert_bulk (void)
190190
count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error);
191191
ASSERT (count == 6);
192192

193+
/* test validate */
194+
for (i = 0; i < 10; i++) {
195+
bson_destroy (&b[i]);
196+
bson_init (&b[i]);
197+
BSON_APPEND_INT32 (&b[i], "$invalid_dollar_prefixed_name", i);
198+
bptr[i] = &b[i];
199+
}
200+
BEGIN_IGNORE_DEPRECATIONS;
201+
r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE,
202+
(const bson_t **)bptr, 10, NULL, &error);
203+
END_IGNORE_DEPRECATIONS;
204+
ASSERT (!r);
205+
ASSERT (error.domain == MONGOC_ERROR_BSON);
206+
ASSERT (error.code == MONGOC_ERROR_BSON_INVALID);
207+
193208
bson_destroy(&q);
194209
for (i = 0; i < 10; i++) {
195210
bson_destroy(&b[i]);

0 commit comments

Comments
 (0)