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

GuidRepresentation made obsolete while function appears impared and documentation remains out of date.

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.11.0
    • Component/s: API, BSON, Documentation
    • None
    • Environment:
      All
    • 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?

      Show
      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?

      Linked to https://jira.mongodb.org/browse/CSHARP-3179

      The entirety of the commonly used BsonDefaults method of setting GuidRepresentation has been marked obsolete while the documentation still refers to it and neither the "obsolete" warning or xmldoc inform you of what the replacement is beyond the vague statement `[Obsolete("Configure serializers instead.")]`

      The following example will fail with `MongoDB.Driver.MongoCommandException: 'Command findAndModify failed: After applying the update, the (immutable) field '_id' was found to have been altered to _id: UUID("45206b70-146b-4947-9de2-aaab99c2223b").'`

      Appending `?uuidRepresentation=Standard` to the connection string and the upsert will work as expected (previous behaviour); as would setting `BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;` both of these configurations correctly store the ID as UUID() in mongo.

      Swapping BSONID for [BsonRepresentation(BsonType.Binary)] will allow the code to run otherwise unaltered but then stores the ID as BinData(3, "a1A8tGgqy0myU3jvfIW3wQ==")

      Changing the ID field to [BsonId, BsonGuidRepresentation(GuidRepresentation.Standard)]
      results in the immutable id error again.

      using MongoDB.Driver;
      using System;
      using System.Threading.Tasks;
      using MongoDB.Bson;
      using MongoDB.Bson.Serialization.Attributes;
      
      namespace MongoGuidStandardIssue
      {
      
          [Serializable]
          public class ReadModel
          {
              public ReadModel(Guid id)
              {
                  Id = id;
                  CreatedDate = DateTimeOffset.UtcNow;
              }
      
              [BsonId]
              public Guid Id { get; set; }
      
              [BsonRepresentation(BsonType.String)]
              public DateTimeOffset CreatedDate { get; set; }
      
          }
      
          public sealed class UserReadModel : ReadModel
          {
              public string Username { get; set; }
              public UserReadModel(Guid id) : base(id) { }
          }
      
      
          public static class Program
          {
              public static async Task Main(string[] args)
              {
                  BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
                  var mongoClient = new MongoClient("mongodb://localhost:32768");
      
                  var readStoreDb = mongoClient.GetDatabase("test");
      
                  var userReadStore = readStoreDb.GetCollection<UserReadModel>(nameof(UserReadModel));
      
                  var userId = Guid.NewGuid();
                  var newUser = new UserReadModel(userId)
                  {
                      CreatedDate = DateTimeOffset.UtcNow,
                      Username = "Test " + DateTime.Now.Ticks.ToString()
                  };
      
                  var findOneAndReplaceTask = await userReadStore.FindOneAndReplaceAsync<UserReadModel>(_ => _.Id == newUser.Id, newUser, new FindOneAndReplaceOptions<UserReadModel> { IsUpsert = true, ReturnDocument = ReturnDocument.After });
      
                  Console.WriteLine("Done");
                  Console.ReadLine();
              }
          }
      }
      
      

       
      We appear to have gone from one line of code to configure ID storage to config roulette.

      Its hard to tell if this is an underlying driver issue, a lack of validation or just a communication/documentation issue; the commit messages around the change don't shine much light on it either. ( ` CSHARP-2724: Implement specification for handling native UUID types. `
       https://github.com/mongodb/mongo-csharp-driver/commit/b0b11348e1fb2418ca6a5999a2494db54ec186e5 ) the addition of new code that is immediately marked as obsolete also muddies the water a tad.

      Thanks

        1. image-2020-08-28-09-48-09-587.png
          image-2020-08-28-09-48-09-587.png
          80 kB
        2. MongoGuidStandardIssue.csproj
          0.3 kB
        3. Program.cs
          2 kB

            Assignee:
            Unassigned Unassigned
            Reporter:
            chris.mckee@ivendi.com Chris McKee
            Votes:
            5 Vote for this issue
            Watchers:
            13 Start watching this issue

              Created:
              Updated:
              Resolved: