Skip to content

Commit 1afeeff

Browse files
committed
Preserve empty documents for verbose results and error reply
Result maps should only be null if verboseResults=false. Additionally, the error reply document can default to an empty document on error (as it is in libmongoc).
1 parent 7264710 commit 1afeeff

4 files changed

+28
-8
lines changed

Diff for: src/MongoDB/BulkWriteCommandResult.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ void php_phongo_bulkwritecommandresult_init_ce(INIT_FUNC_ARGS)
401401

402402
static inline bson_t* _bson_copy_or_null(const bson_t* bson)
403403
{
404-
return bson_empty0(bson) ? NULL : bson_copy(bson);
404+
return bson ? bson_copy(bson) : NULL;
405405
}
406406

407407
php_phongo_bulkwritecommandresult_t* phongo_bulkwritecommandresult_init(zval* return_value, mongoc_bulkwritereturn_t* bw_ret, zval* manager)
@@ -421,16 +421,18 @@ php_phongo_bulkwritecommandresult_t* phongo_bulkwritecommandresult_init(zval* re
421421
bwcr->modified_count = mongoc_bulkwriteresult_modifiedcount(bw_ret->res);
422422
bwcr->deleted_count = mongoc_bulkwriteresult_deletedcount(bw_ret->res);
423423

424+
// Result documents will null if verboseResults=false
424425
bwcr->insert_results = _bson_copy_or_null(mongoc_bulkwriteresult_insertresults(bw_ret->res));
425426
bwcr->update_results = _bson_copy_or_null(mongoc_bulkwriteresult_updateresults(bw_ret->res));
426427
bwcr->delete_results = _bson_copy_or_null(mongoc_bulkwriteresult_deleteresults(bw_ret->res));
427428
}
428429

429-
// Copy mongoc_bulkwriteexception_t fields
430+
/* If any error(s) occurred, mongoc_bulkwriteexception_t will be non-null.
431+
* Copy its fields into the result object. */
430432
if (bw_ret->exc) {
431-
bwcr->error_reply = _bson_copy_or_null(mongoc_bulkwriteexception_errorreply(bw_ret->exc));
432-
bwcr->write_errors = _bson_copy_or_null(mongoc_bulkwriteexception_writeerrors(bw_ret->exc));
433-
bwcr->write_concern_errors = _bson_copy_or_null(mongoc_bulkwriteexception_writeconcernerrors(bw_ret->exc));
433+
bwcr->error_reply = bson_copy(mongoc_bulkwriteexception_errorreply(bw_ret->exc));
434+
bwcr->write_errors = bson_copy(mongoc_bulkwriteexception_writeerrors(bw_ret->exc));
435+
bwcr->write_concern_errors = bson_copy(mongoc_bulkwriteexception_writeconcernerrors(bw_ret->exc));
434436
}
435437

436438
ZVAL_ZVAL(&bwcr->manager, manager, 1, 0);

Diff for: tests/bulkwritecommand/bulkwritecommand-ctor-bypassDocumentValidation-002.phpt

+7-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ object(MongoDB\Driver\BulkWriteCommandResult)#%d (%d) {
106106
array(0) {
107107
}
108108
["errorReply"]=>
109-
NULL
109+
object(MongoDB\BSON\Document)#%d (%d) {
110+
["data"]=>
111+
string(8) "BQAAAAA="
112+
["value"]=>
113+
object(stdClass)#%d (%d) {
114+
}
115+
}
110116
}
111117
===DONE===

Diff for: tests/bulkwritecommand/bulkwritecommand-ctor-ordered-001.phpt

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ object(MongoDB\Driver\BulkWriteCommandResult)#%d (%d) {
6767
array(0) {
6868
}
6969
["errorReply"]=>
70-
NULL
70+
object(MongoDB\BSON\Document)#%d (%d) {
71+
["data"]=>
72+
string(8) "BQAAAAA="
73+
["value"]=>
74+
object(stdClass)#%d (%d) {
75+
}
76+
}
7177
}
7278
===DONE===

Diff for: tests/bulkwritecommand/bulkwritecommand-ctor-ordered-002.phpt

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ object(MongoDB\Driver\BulkWriteCommandResult)#%d (%d) {
6767
array(0) {
6868
}
6969
["errorReply"]=>
70-
NULL
70+
object(MongoDB\BSON\Document)#%d (%d) {
71+
["data"]=>
72+
string(8) "BQAAAAA="
73+
["value"]=>
74+
object(stdClass)#%d (%d) {
75+
}
76+
}
7177
}
7278
===DONE===

0 commit comments

Comments
 (0)