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

FullDocument of the ChangeStreamDocument<TDocument> should not throw on BsonNull

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.26.0
    • Affects Version/s: 2.24.0, 2.25.0
    • Component/s: Change Streams
    • None
    • Fully Compatible
    • Dotnet Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Summary

      Please provide a clear and concise description of the bug.

      Property FullDocument of the ChangeStreamDocument<TDocument> does throw an exception if the underyling value of fullDocument is of type BsonNull. 

      There is a very similar property called FullDocumentBeforeChange that also has similar functionality, but it has an additional check for BsonNull. I think FullDocument just lacks this check.

       

      The exception is the following:

      System.InvalidCastException: 'Unable to cast object of type 'MongoDB.Bson.BsonNull' to type 'MongoDB.Bson.BsonDocument'.'

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

       

      driver versions: 2.24 and 2.25.

      replica set consists of 3 simple containerized instances of version 7.0 

       

      How to Reproduce

      Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

       

      Try to retrieve ChancheStreamDocument<BsonDocument> from the actual stream or create the document manually (easier to set up). The idea is to set fullDocument and fullDocumentBeforeChange the properties in BackingDocument to BsonNull.

       

      Example of the second approach (manual creation of ChangeStreamDocument<BsonDocument>):

      Try to create a ChangeStreamDocument<BsonDocument> and initialize it with a BsonDocument with {"fullDocument", BsonNull.Value },
          {"fullDocumentBeforeChange", BsonNull.Value } (as shown below). Later, check its FullDocumentBeforeChange (should be no error) and FullDocument (should be erroneous).

       

      var bsonDocumentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<BsonDocument>();
      var csd = new ChangeStreamDocument<BsonDocument>(new BsonDocument()
      {
          {"fullDocument", BsonNull.Value },
          {"fullDocumentBeforeChange", BsonNull.Value }
      }, bsonDocumentSerializer);
      var fullDocBeforeChange = csd.FullDocumentBeforeChange;   // <----- no error
      var fullDocAfterChange = csd.FullDocument;     // <----- InvalidCastException
      
      

       

       

      Either 

      Additional Background

      Please provide any additional background information that may be helpful in diagnosing the bug.

            Assignee:
            oleksandr.poliakov@mongodb.com Oleksandr Poliakov
            Reporter:
            adrian.chervinchuk1@gmail.com adrian chervinchuk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: