NullReferenceException in IEnumerableSerializerBase.Serialize when projecting nullable array with conditional Select

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 3.9.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • 🔴 Roadblock
    • Fully Compatible
    • C 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?
    • None
    • None
    • None
    • None
    • None
    • None

      Summary:

       NullReferenceException in IEnumerableSerializerBase.Serialize when projecting nullable array with conditional Select

      Environment:

       - MongoDB.Driver
        3.8.1 (NuGet, regression from 3.1.0)
       - Windows 11 64-bit x86_64
       - .NET 10 SDK
       - MongoDB
        7.0 standalone (docker)

      How to Reproduce:

      Full repro repo with two single-file .NET 10 code: https://github.com/realTeddy/mongo-bug-report/tree/main/MongoDriverNreRepro

       1. Start MongoDB: docker compose up -d
       2. Run dotnet run repro_old.cs (driver
        3.1.0) — both queries succeed
       3. Run dotnet run repro_new.cs (driver
        3.8.1) — broken query throws NRE

      The repro inserts three documents:

       1. Loan with Attachments = null
       2. Loan with Attachments = [] (empty list)
       3. Loan with two populated Attachment items

      The following LINQ projection throws NullReferenceException on document #1 (null Attachments) with driver 3.8.1:

       

       var results = collection.AsQueryable()
           .Select(l => new LoanDto
           {
               Id = l.Id,
               Attachments = l.Attachments == null
                   ? null
                   : l.Attachments.Select(a => new Attachment
                   {
                       AttachmentId = a.AttachmentId,
                       Title = a.Title
                   })
           })
           .ToList();

       

      Stack trace:

       

      System.NullReferenceException: Object reference not set to an instance of an object.
          at MongoDB.Driver.Linq.Linq3Implementation.Serializers.IEnumerableSerializerBase`2.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TEnumerable value)
          at MongoDB.Bson.Serialization.IBsonSerializerExtensions.ToBsonValue(IBsonSerializer serializer, Object value)
          at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ConstantExpressionToAggregationExpressionTranslator.Translate(...)
          at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ConditionalExpressionToAggregationExpressionTranslator.Translate(...)

       

      Additional Background:

      The NRE occurs during query translation (not execution), when the LINQ3 provider tries to serialize the null constant in the ternary's true-branch through 
      IEnumerableSerializerBase.Serialize, which doesn't handle null.

      Workaround — replace the ternary with null-coalesce:

       

       Attachments = (l.Attachments ?? new List<Attachment>())
           .Select(a => new Attachment { AttachmentId = a.AttachmentId, Title = a.Title })

       

            Assignee:
            Ferdinando Papale
            Reporter:
            Tewodros Assefa
            None
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: