[CSHARP-486] Update.SetWrapped throws NullReferenceException when value is null Created: 04/Jun/12  Updated: 02/Apr/15  Resolved: 06/Jun/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.4.2
Fix Version/s: 1.5

Type: Bug 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

Backwards Compatibility: Minor Change

 Description   

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.



 Comments   
Comment by Robert Stam [ 05/Jun/12 ]

The same applies to AddToSetWrapped, PullWrapped, PushWrapped in addition to SetWrapped.

Also, the list of values passed to AddToSetEachWrapped, PullAllWrapped, PushAllWrapped might contain nulls (although the list itself can't be null).

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