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

Support Dictionary get_Item for keys that aren't strings but are serialized as strings

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.3.0
    • Affects Version/s: 3.1.0
    • Component/s: Linq
    • None
    • Fully Compatible
    • Dotnet Drivers
    • Needed
    • 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?

      We could document that the Dictionary key no longer HAS to be a string as long as it serializes as a string.

      Show
      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? We could document that the Dictionary key no longer HAS to be a string as long as it serializes as a string.
    • None
    • None
    • None
    • None
    • None
    • None

       

      Hello.

      color: Color value is invalid

      I have configured the serialization so that GUID is converted to string and back.

       BsonSerializer.TryRegisterSerializer(new GuidSerializer(BsonType.String));
              var guidSerializer = new GuidSerializer(BsonType.String);
              var stringSerializer = BsonSerializer.LookupSerializer<object>();
              BsonSerializer.RegisterSerializer(typeof(Dictionary<Guid, object>), 
                  new DictionaryInterfaceImplementerSerializer<Dictionary<Guid, object>, Guid, object>(
                      DictionaryRepresentation.Document,
                      guidSerializer,
                      stringSerializer
                  )
              );
      

      Its work fine then GUID is in root of Document 

       

      public class Lib
      {
          public Guid MyIDField { get; set; }
      }
      

      But then is in Dictionary 

      public Dictionary<Guid, GameLib> Games { get; set; } = [];

      Driver ignore RegisterSerializer(typeof(Dictionary<Guid, object>)  and throw exception:

      MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: f.Games.get_Item(229341dc-7125-4093-b407-7092245c7d9b).
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.GetItemMethodToFilterFieldTranslator.Translate(TranslationContext context, Expression expression, MethodInfo method, Expression fieldExpression, ReadOnlyCollection`1 arguments)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.GetItemMethodToFilterFieldTranslator.Translate(TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.MethodCallExpressionToFilterFieldTranslator.Translate(TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ExpressionToFilterFieldTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.MemberExpressionToFilterFieldTranslator.Translate(TranslationContext context, MemberExpression memberExpression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ExpressionToFilterFieldTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.LinqProviderAdapter.TranslateExpressionToField[TDocument,TField](Expression`1 expression, IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions, Boolean allowScalarValueForArrayField)
         at MongoDB.Driver.ExpressionFieldDefinition`2.Render(RenderArgs`1 args)
         at MongoDB.Driver.OperatorUpdateDefinition`2.Render(RenderArgs`1 args)
         at MongoDB.Driver.CombinedUpdateDefinition`1.Render(RenderArgs`1 args)
         at MongoDB.Driver.MongoCollectionImpl`1.ConvertWriteModelToWriteRequest(WriteModel`1 model, Int32 index, RenderArgs`1 renderArgs)
         at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass83_0

       

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            al.alexov@inbox.ru alexov inbox
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              None
              None
              None
              None