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

Atlas Search doesn't respect the correct serializer

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Dotnet Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      If you try to perform a search using Atlas Search with SearchDefinitionBuilder and you want to apply an Equals filter (but I think it applies to all filters) on a field of type Guid, the generated query does not respect the serialiser registered

      How to Reproduce

      Register guid serialization as string like this

      _BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));_ 
      

      then apply a query on a guid field like this

      
      _var fb = Builders<BundleDocument>.Search;
      
      var compoundSearchDefinition = Builders<BundleDocument>
          .Search
          .Compound()
          .Must(fb.Equals(x => x.ShopId, request.ShopId));_ 
      

      The query will serialize the GUID as Binary

       _\{ "equals" : { "value" : { "$binary" : { "base64" : "LH/9mkkbQtONt9OjBhEMIg==", "subType" : "04" } }, "path" : "shopId" }_  
      

       

      Additional Background

      The issue is on the method ToBsonValue of EqualsSearchDefinition that looks like this

      __

      private static BsonValue ToBsonValue(TField value) =>
          value switch
          {
              bool v => (BsonBoolean)v,
              sbyte v => (BsonInt32)v,
              byte v => (BsonInt32)v,
              short v => (BsonInt32)v,
              ushort v => (BsonInt32)v,
              int v => (BsonInt32)v,
              uint v => (BsonInt64)v,
              long v => (BsonInt64)v,
              float v => (BsonDouble)v,
              double v => (BsonDouble)v,
              DateTime v => (BsonDateTime)v,
              DateTimeOffset v => (BsonDateTime)v.UtcDateTime,
              ObjectId v => (BsonObjectId)v,
              Guid v => new BsonBinaryData(v, GuidRepresentation.Standard),
              string v => (BsonString)v,
              null => BsonNull.Value,
              _ => throw new InvalidCastException()
          }; 
      

       

            Assignee:
            ferdinando.papale@mongodb.com Ferdinando Papale
            Reporter:
            imperugo@gmail.com Ugo Lattanzi
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              None
              None
              None
              None