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

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

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 2.1
    • 2.0.1
    • Serialization
    • None

    Description

      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);
      

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: