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

Differencies in how new Driver handle custom serialization

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.2.3
    • Component/s: Serialization

      I've setup a simple test to repro the problem here https://github.com/alkampfergit/MongoDbNewDriverTest

      If you create a class implementing IBsonSerializer in the old driver to control serialization of a series of class based on a base class, everything is ok.

      I have base class EventStoreIdentity then a GroupId and a UserId both inheriting from that class. I instruct driver to use custom serializer

      BsonSerializer.RegisterSerializer(typeof(GroupId), new EventStoreIdentityBsonSerializer());
      

      Then I declare a class that uses GroupId, No problem

        public class ObjectWithArrayOfIdentities
          {
              public String Id { get; set; }
      
              public GroupId[] Groups { get; set; }
          }
      

      then I have a class that declares an array with that type and the new driver is not able to serialize the object anymore.

        public class ObjectWithArrayOfIdentities
          {
              public String Id { get; set; }
      
              public GroupId[] Groups { get; set; }
          }
      

      It throws Unable to cast object of type 'NewDriver.Serializer.EventStoreIdentityBsonSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[CommonTestClasses.GroupId]'.

      With the old driver there is no problem in doing the same operation, it is able to serialize everything correctly.

      The same happens with classes that declare property using base class or generics (the new driver is not able to serialize them)

       public class ObjectWithGenericIdentity<T> where T : EventStoreIdentity
          {
              public String Id { get; set; }
      
              public T RelatedId { get; set; }
          }
      

      There are some workaround to avoid this problem, as an example creating a typed serializer that is able to serialize array, but it would be nice if the new driver behaves the same as the old one.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            alkampfer@nablasoft.com Gian Maria Ricci
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: