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

Update.SetWrapped throws NullReferenceException when value is null

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.5
    • Affects Version/s: 1.4.2
    • Component/s: None
    • Labels:
      None
    • Minor Change

      Passing C# null to Update.SetWrapped<T> should be valid because T is a POCO and not a BsonValue. A C# null value of T will almost certainly be serialized as a BsonNull.Value (although technically it is up to the serializer for T to decide how to serialize C# null).

      To reproduce:

      public class C
      {
          public ObjectId Id { get; set; }
          public Point P { get; set; }
      }
      
      public class Point
      {
          public int X { get; set; }
          public int Y { get; set; }
      }
      
      var doc = new C { P = new Point { X = 1, Y = 2 } };
      collection.Insert(doc);
      var id = doc.Id;
      Console.WriteLine(doc.ToJson());
      
      var query = Query.EQ("_id", id);
      var update = Update.SetWrapped<Point>("P", null);
      collection.Update(query, update);
      
      doc = collection.FindOne();
      Console.WriteLine(doc.ToJson());
      

      In 1.4 and later this throws a NullReferenceException. In earlier versions it did not throw a NullReferenceException but it also didn't work.

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

              Created:
              Updated:
              Resolved: