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

ObjectSerializer can't deserialize custom generic classes

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Serialization
    • None
    • Dotnet Drivers

       

      using MongoDB.Bson;
      using MongoDB.Bson.Serialization;
      using MongoDB.Bson.Serialization.Serializers;
      using MongoDB.Driver;
      
      var objectSerializer = new ObjectSerializer(ObjectSerializer.AllAllowedTypes); 
      
      BsonSerializer.RegisterSerializer(objectSerializer);
      
      var client = new MongoClient();
      
      var collection = client.GetDatabase("test").GetCollection<D>("test_collection");
      
      var testDocument = new D { O = new A<int> { X = 1 } };
      collection.InsertOne(testDocument);
      
      var result = collection.Find(Builders<D>.Filter.Empty).Single();
      Console.WriteLine($"TestDocument X value: {((A<int>)result.O).X}");
      
      
      public class A<TX>
      {
          public ObjectId Id { get; set; }
          public TX X { get; set; }
      }
      
      public class D
      {
          public ObjectId Id { get; set; }
          public object O { get; set; }
      }

      If you run the code above as is, everything works fine because as the InsertOne operation serialized the testDocument, it registered the discriminator for the generic type A<> with its associated type. So when we call find, we can deserialize without problems. 

      Now if you comment out the insert code and only do find. You get this error:

      Unhandled exception. System.FormatException: An error occurred while deserializing the O property of class D: Unknown discriminator value 'A`1'.
       ---> MongoDB.Bson.BsonSerializationException: Unknown discriminator value 'A`1'.
         at MongoDB.Bson.Serialization.BsonSerializer.LookupActualType(Type nominalType, BsonValue discriminator) in mongo-csharp-driver/src/MongoDB.Bson/Serialization/BsonSerializer.cs:line 339
         at MongoDB.Bson.Serialization.Conventions.ObjectDiscriminatorConvention.GetActualType(IBsonReader bsonReader, Type nominalType) in mongo-csharp-driver/src/MongoDB.Bson/Serialization/Conventions/ObjectDiscriminatorConvention.cs:line 127
         at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args) in mongo-csharp-driver/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs:line 380
         at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) in mongo-csharp-driver/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs:line 223
         at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) in mongo-csharp-driver/src/MongoDB.Bson/Serialization/Serializers/SerializerBase.cs:line 119
         at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context) in mongo-csharp-driver/src/MongoDB.Bson/Serialization/IBsonSerializerExtensions.cs:line 36
         at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap) in mongo-csharp-driver/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:line 545
       

       

            Assignee:
            adelin.mbidaowona@mongodb.com Adelin Mbida Owona
            Reporter:
            adelin.mbidaowona@mongodb.com Adelin Mbida Owona
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: