[CSHARP-4535] Client cannot resolve serializer anymore when using IQueryable with base type Created: 18/Feb/23  Updated: 15/Nov/23

Status: Backlog
Project: C# Driver
Component/s: Linq
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Unknown
Reporter: Sebastian Stehle Assignee: Unassigned
Resolution: Unresolved Votes: 4
Labels: triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File ConsoleApp4.csproj     Text File Program.cs    
Issue Links:
Duplicate
is duplicated by CSHARP-4650 LINQ3 provider: ValueType 'X' of para... Closed

 Description   

I have a public class `Entity` and an entity that inherits from that called `MongoEntity`.

The MongoEntity is an implementation detail of the repository, so it is used only internally and is not exposed outside.

e.g. something like this:

 

class Entity {}
class MongoEntity : Entity {}
public class Repository{
   private IMongoCollection<MongoEntity> collection;
   
   public IQueryable<Entity> Entities {
       get => collection.AsQueryable();
   }
}

Until 2.18.0 it works fine, but it breaks with 2.19.0:

I get the following exception in my sample:

System.ArgumentException: 'ValueType 'ConsoleApp4.Program+MongoEntity' of parameterSerializer does not match parameter type 'ConsoleApp4.Program+Entity'. (Parameter 'parameterSerializer')'

In my real application I get the following error:

System.InvalidCastException: 'Unable to cast object of type 'MongoDB.Bson.Serialization.BsonClassMapSerializer`1[Notifo.Identity.MongoDb.MongoDbUser]' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[Microsoft.AspNetCore.Identity.IdentityUser]'.'

I was not able to reproduce this exception in my sample, but I guess it is the same root issue.

My very little example is attached.



 Comments   
Comment by Cam Murray [ 15/Nov/23 ]

Also experiencing this issue which has forced me to downgrade to LINQ2. Hopefully we get this fixed soon, thank you for your support!

Comment by Sys Ops [ 19/Oct/23 ]

P.S. Could you please update the Jira component to "LINQ3" if this only affects LINQ3?

Comment by Sys Ops [ 12/Oct/23 ]

Hi robert@mongodb.com, now in October I had hoped these critical LINQ3 issues would have been fixed by now. But if we run with the latest driver (2.22.0 ) our code still breaks with this error† blocking us from enabling LINQ3 until its fixed.
Earlier I learnt the promise for LINQ3 was no major/breaking changes so are you / the team working on a fix for this? If so, may I ask what's your ETA? If not, I'd hope you'll find time soon to tackle this (as as a non-paying customer I'm in no position at all to demand anything...)

† Also System.ArgumentException: ValueType 'Xxx.Yyy.Data.Questions.Section' of parameterSerializer does not match parameter type 'Xxx.Yyy.Data.CollectionItems.CollectionItem'. (Parameter 'parameterSerializer'

Comment by Robert Stam [ 30/Mar/23 ]

Thanks for catching that. I've created CSHARP-4590 to track updating the doc comments.

Comment by Jean Llorca [ 30/Mar/23 ]

I also use this pattern.
The comment on LinqProvider.V3 indicates it'll be the default in version 3 of the driver. I would recommend changing the comment if it is already the default now.

Comment by Sebastian Stehle [ 22/Feb/23 ]

Thanks for the workaround

Comment by Robert Stam [ 22/Feb/23 ]

Thanks for the further information.

I will continue looking into how to make this type of casting work with the new LINQ3 provider.

In the meantime you can manually select to continue using the LINQ2 provider like this:

var clientSettings = new MongoClientSettings { LinqProvider = LinqProvider.V2 }; // plus other settings including server address
var clent = new MongoClient(clientSettings);

 

Comment by Sebastian Stehle [ 22/Feb/23 ]

The cast from `IMongoQueryable<MongoEntity>` to `IQueryable<MongoEntity>` works fine. And it always works with 2.18.0, so you must have changed something.

Typically I do not expose IQueryable but it is necessary for the IUserStore in AspNet Identity.

Comment by Robert Stam [ 21/Feb/23 ]

I am able to reproduce this issue, and it is related to casting the `IMongoQueryable<MongoEntity>` to an `IQueryable<Entity>`.

I'm a little surprised changing `MongoEntity` to `Entity` did not result in a compilation error, but I guess that's because of the `out T` in the declaration of `IQueryable<out T>`.

This might not be easy to support (in general we weren't expecting users to cast `IMongoQueryable<T>` to `IQueryable<T>`, and certainly weren't expecting the type of `T` to change).

I'll investigate futther...

Comment by Robert Stam [ 21/Feb/23 ]

Thank you for reporting this issue. I will begin investigating it soon.

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