[CSHARP-555] BsonDocument object model methods should throw ArgumentNullException instead of ignoring C# null Created: 06/Sep/12  Updated: 22/May/15  Resolved: 21/Apr/14

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.8
Fix Version/s: 2.0

Type: Improvement Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-1287 DOCS- BsonValues no longer accept nul... Closed
is related to CSHARP-509 Remove superfluous Create methods and... Closed

 Description   

The BsonDocument object model has a tricky way of ignoring C# null values sometimes, which allows constructs like the following:

BsonValue a = 1;
BsonValue b = null;
var document = new BsonDocument
{
    { "a", a },
    { "b", b } // will be ignored because b == null
};

The intent was to facilitate construction of documents with optional elements, but in practice users have been confused and/or surprised by this. So we are going to change the Add method (which the above code implicitly calls) to throw an ArgumentNullException in this case.

You can easily achieve the same result just by adding an explicit test:

BsonValue a = 1;
BsonValue b = null;
var document = new BsonDocument
{
    { "a", a },
    { "b", b, b != null } // will be ignored because test is false
};

The following is the complete list of methods that will be changed in this way:

BsonArray.Create (multiple overloads)
BsonArray.Add (multiple overloads)
BsonArray.AddRange (multiple overloads)
BsonBinaryData.Create (multiple overloads)
BsonBoolean.Create
BsonDateTime.Create
BsonDocument.Create
BsonDocument.Add (multiple overloads)
BsonDocument.Merge
BsonDouble.Create
BsonElement.Create (multiple overloads)
BsonInt32.Create
BsonInt64.Create
BsonJavaScript.Create (multiple overloads)
BsonJavaScriptWithScope.Create (multiple overloads)
BsonObjectId.Create(multiple overloads)
BsonRegularExpression.Create(multiple overloads)
BsonString.Create(multiple overloads)
BsonSymbol.Create(multiple overloads)
BsonTimeStamp.Create



 Comments   
Comment by Githook User [ 21/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-555: Fix some more failing unit tests that the weren't detected for some unknown reason by the NUnit Visual Studio plugin.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/2326a339d93e45c83210ac2fc20a1903d64ad921

Comment by Githook User [ 21/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-555: Throw if Merge is called with null argument. And fix failing unit tests that weren't detected in the last commit because the NUnit Visual Studio extension got messed up and wasn't running all the tests.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/2bf5441501b0aee877e46bc75fa1e56ea9813347

Comment by Robert Stam [ 21/Apr/14 ]

Part of this work was already done while working on CSHARP-509. See commit:

https://github.com/mongodb/mongo-csharp-driver/commit/8cad37b22079abde35b3dbdfbe6cd6e023d0fa5e

Comment by Robert Stam [ 24/Feb/13 ]

This work was prototyped on the experimental x2.0 branch. This ticket is being reopened for eventual porting to the official 2.0 branch.

Comment by Robert Stam [ 07/Sep/12 ]

Pushed to x2.0 branch.

Comment by Robert Stam [ 07/Sep/12 ]

In code review.

Comment by auto [ 07/Sep/12 ]

Author:

{u'date': u'2012-09-06T22:29:19-07:00', u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-555: changed BsonDocument object model so C# null is never skipped. It's either an error or mapped to BsonNull.Value as appropriate. Also removed a few inappropriate methods.
Branch: x2.0
https://github.com/mongodb/mongo-csharp-driver/commit/272f026095818e84949f7fe24bf413607fac4926

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