[CDRIVER-2738] Bulk functions for insert, update, and replace should not support bypassDocumentValidation option Created: 11/Jul/18  Updated: 28/Oct/23  Resolved: 24/Jul/18

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: 1.10.0
Fix Version/s: 1.13.0

Type: Bug Priority: Major - P3
Reporter: Jeremy Mikola Assignee: A. Jesse Jiryu Davis
Resolution: Fixed Votes: 0
Labels: bulk
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CDRIVER-2373 IDL for functions that take flexible ... Closed

 Description   

As part of CDRIVER-2373, db38474 and ef63400 added a "bypassDocumentValidation" option to the insert, update, and replace with_opts functions in mongoc-bulk-operation.c. This doesn't make sense, as mongoc_bulk_operation_t already tracks bypass_document_validation on its own. The bulk-level option is set at init time and applies to all commands executed within the bulk.

If we consider inserts, we can see that mongoc_bulk_operation_insert_with_opts() calls _mongoc_bulk_insert_opts_parse() to validate the options. Assuming that the bson_t *opts argument is successfully parsed into _mongoc_bulk_insert_opts_t insert_opts, only insert_opts.validate is used later in the function. The other bypass and extra fields on that struct are completely ignored.

There are now two paths: we either append to and existing insert command or create a new one. If we append to an existing insert, we delegate to _mongoc_write_command_insert_append() and only pass the validated document. The bypass option for insert_with_opts has been ignored.

Assuming we create a new insert command, we delegate to _mongoc_write_command_init_insert and pass our empty mongoc_write_command_t command struct, the validated document and bson_t *opts, and our bulk->flags (which contains the original "ordered" and "bypassDocumentValidation" options used to create the mongoc_bulk_operation_t.

Jumping into mongoc-write-command.c, _mongoc_write_command_init_insert() delegates to _mongoc_write_command_init_bulk() to initialize a mongoc_write_command_t command. This function copies the cmd_opts (originally bson_t *opts) into the command->cmd_opts and also assigns bulk->flags to command->flags.

Further down the line, _mongoc_write_opquery() and _mongoc_write_opmsg() will both end up calling _mongoc_write_command_init(), which may end up appending "bypassDocumentValidation" into the command document again based on command->flags.

I believe there are a few errors here:

  • According to the CRUD spec, the bulk write models were never intended to support a "bypassDocumentValidation" option. That option is supported only on bulkWrite() and other non-bulk methods (e.g. insertOne(), updateMany()).
  • The current behavior in libmongoc is prone to appending "bypassDocumentValidation" multiple times in some cases, and disregarding the mongoc_bulk_operation_t-level option in other cases.
  • In the event that an inserted document is appended to an existing insert command, the operation-level "bypassDocumentValidation" may be ignored entirely. This can be unexpected from the user's perspective, as it depends entirely on the order in which operations are added to a bulk.

Ideally, libmongoc should prohibit a "bypassDocumentValidation" option for functions that add insert, replace, and update operations to a bulk write.



 Comments   
Comment by Githook User [ 24/Jul/18 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'email': 'jesse@mongodb.com', 'username': 'ajdavis'}

Message: CDRIVER-2738 no bypassDocumentValidation option per bulk op
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/54d5e03d52556f65c1d1b092feb122b171593648

Comment by A. Jesse Jiryu Davis [ 11/Jul/18 ]

Agreed, Jeremy. Thanks for the report.

Generated at Wed Feb 07 21:16:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.