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

ReplaceWith not respecting custom element name configured by the serializer

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Unknown Unknown
    • None
    • 2.22.0
    • None
    • None

    Description

      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)
      

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated: