[CSHARP-4057] LinqV3 Null Coalescing operator with empty string expression results in "Cannot find serializer for null" Created: 16/Feb/22  Updated: 28/Oct/23  Resolved: 29/Jun/22

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

Type: Bug Priority: Unknown
Reporter: Rich Collette Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2022-02-16-10-37-27-541.png    
Issue Links:
Related
related to CSHARP-2674 string.IsNullOrEmpty is false when fi... Closed
Epic Link: CSHARP-3615
Backwards Compatibility: Fully Compatible

 Description   

Summary

When doing an aggregate containing a projection expression like:

.Project(
    augmentedProductType => new ProductTypeSearchResult
    {
        IsExternalUrl = string.IsNullOrEmpty(augmentedProductType.ShopUrl ?? "")
    }

 
An InvalidOperationException occurs with message "Cannot find serializer for null."

This happens with string.Empty as well.

Driver version 2.14.1

How to Reproduce

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

Additional Background

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

This seems to occur in ConstantExpressionToAggregationExpressionTranslator when making the call to get the serializer.



 Comments   
Comment by Robert Stam [ 29/Jun/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which is included in the 2.14 release.

Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality.

To configure a client to use the LINQ3 provider use code like the following

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

Comment by Githook User [ 29/Jun/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4057: Verify that issue is not present in LINQ3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/345aaaee6c91b27442bca5b0b1e5392a3d5760e7

Comment by Richard Collette [ 01/Mar/22 ]

Your attempt to reproduce  is not using aggregation and the bug appears to be specific to aggregation as indicated by the fact the error is occurring in a stack trace containing ConstantExpressionToAggregationExpressionTranslator.

Unfortunately I probably will not be able to find the time to assemble a self contained example (which is difficult to do by the way since you need a collection to work with).

 

Comment by Boris Dogadov [ 16/Feb/22 ]

Hi richard.collette@precisely.com,

Is it possible to provide a full self-contained sample that reproduces the problem?
We were not able to reproduce this failure with the following code: 

var clientSettings = MongoClientSettings.FromConnectionString(@"mongodb://localhost:27017");
clientSettings.LinqProvider = LinqProvider.V3; 
var client = new MongoClient(clientSettings);
var database = client.GetDatabase("db");
var collection = database.GetCollection<Summary>("summaries");
var movies = collection.
  Find(FilterDefinition<Summary>.Empty).
  Project(s => new Movie { BoolField = string.IsNullOrEmpty(s.StrField ?? "") }).
  ToList();

 

2.14.1 driver,  both LINQ2 and LINQ3 providers.

Thanks.

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