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

NullReferenceException thrown when deserializing an immutable class with an extra elements property

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.1
    • Affects Version/s: 2.0.1
    • Component/s: Serialization
    • None

      Given the following immutable class:

      public class C
      {
          [BsonElement]
          public int X { get; }
          [BsonExtraElements]
          [BsonDefaultValue(null)]
          public BsonDocument E { get; }
      
          [BsonConstructor]
          public C(int x, BsonDocument e)
          {
              X = x;
              E = e;
          }
      }
      

      Attempting to deserialize a document with an extra element throws a NullReferenceException.

      To reproduce:

      var json = "{ X : 1, Y : 2 }";
      var c = BsonSerializer.Deserialize<C>(json);
      

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

              Created:
              Updated:
              Resolved: