Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-555

BsonDocument object model methods should throw ArgumentNullException instead of ignoring C# null

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.0
    • Affects Version/s: 1.8
    • Component/s: None
    • None

      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

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: