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

ReplaceWith not respecting custom element name configured by the serializer

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.22.0
    • Component/s: None
    • None

      Summary

      ReplaceWith, ReplaceRoot & Project pipeline stages don't seem to respect the serializer's configuration when using a new expression of a customized class map.

      How to Reproduce

      Run the following unit test

      public class ReplaceWithBug
      {
          [Fact]
          public void ReplaceWithShouldRenderElementNameCorrectly()
          {
              BsonClassMap.RegisterClassMap<User>(cm =>
              {
                  cm.AutoMap();
                  cm.MapMember(c => c.UserId).SetElementName("uuid");
              });
      
              var client = new MongoClient(new MongoClientSettings { LinqProvider = MongoDB.Driver.Linq.LinqProvider.V3 });
              var collection = client.GetDatabase("db").GetCollection<User>("user");
      
              var rendered = PipelineStageDefinitionBuilder
                  .ReplaceWith<User, User>(u => new User { UserId = u.UserId })
                  .Render(collection.DocumentSerializer, collection.Settings.SerializerRegistry, collection.Database.Client.Settings.LinqProvider);
      
              var bson = rendered.Document.ToString();
      
              Assert.Equal("""{ "$replaceWith" : { "uuid" : "$uuid" } }""", bson);
          }
      }
      
      public class User
      {
          public Guid UserId { get; set; }
      }
      

       

      Running the unit test produces the following error:

        Message: 
          Assert.Equal() Failure: Strings differ
                                           ↓ (pos 22)
          Expected: "{ "$replaceWith" : { "uuid" : "$uuid" } }"
          Actual:   ···""$replaceWith" : { "UserId" : "$uuid" } }"
                                            ↑ (pos 22)
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            wassimk@vypex.com Wassim Khalil
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: