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

Regression of CSHARP-1521. ElemMatch requires IBsonArraySerializer for DictionaryInterfaceImplementerSerializer

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.10.2
    • Component/s: BSON, Operations
    • None

      The issue CSHARP-1521 occurs again in the newest 2.10.2 C# Driver.  Error message: 

      The serializer for field 'MyDictionary' must implement IBsonArraySerializer and provide item serialization info.

       

      Model is definied like this:

       public class Model
       {
           public Dictionary<long, Guid> MyDictionary { get; set; }
       }
      

       

      ClassMap is registered as below:

       BsonClassMap.RegisterClassMap<Model>(cm =>
       {
           cm.AutoMap();
           cm.MapProperty(c => c.MyDictionary).SetSerializer(new DictionaryInterfaceImplementerSerializer<Dictionary<long, Guid>>(DictionaryRepresentation.ArrayOfDocuments));
       });
      

       

       

      My query:

      Builders<Model>.Filter.ElemMatch(request => request.MyDictionary, f => f.Value.Equals(value));
      

       

      And I have a workaround that works for me, but I don't want to use it in long term:

      public class DictionaryArrayInterfaceImplementerSerializer<TDictionary, TKey, TValue> : DictionaryInterfaceImplementerSerializer<TDictionary>, IBsonArraySerializer where TDictionary : class, IDictionary, new()
      {
          public DictionaryArrayInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation) : base(dictionaryRepresentation)
          { }    public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
          {
              var serializer = new KeyValuePairSerializer<TKey, TValue>();
              serializationInfo = new BsonSerializationInfo(
                  null,
                  serializer,
                  serializer.ValueType);        return true;
          }
      }
      

      Basically I can create new serializer, which inherits from DictionaryInterfaceImplementerSerializer, but also implements IBsonArraySerializer and then everything works.

       

       

            Assignee:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Reporter:
            sekalek5@gmail.com Damian Miłosz
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: