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

Client cannot resolve serializer anymore when using IQueryable with base type

    • Type: Icon: Task Task
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: LINQ3
    • 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?

      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.

        1. Program.cs
          1 kB
        2. ConsoleApp4.csproj
          0.3 kB

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            sebastian@squidex.io Sebastian Stehle
            Votes:
            4 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: