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

For GuidRepresentationMode.V3, an exception is thrown when saving an ExpandoObject with a Guid

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Serialization
    • None

      When saving an ExpandoObject that contains a GUID property, GuidSerializer throws an exception if BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3.

      See this code: mongo-csharp-driver/GuidSerializer.cs at master · mongodb/mongo-csharp-driver (github.com)

      Exception is thrown at line 174: 

       

      throw new BsonSerializationException("GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.");

       

      The following code reproduces the issue:

      BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
      var client = new MongoClient("mongodb://localhost:27017");
      var db = client.GetDatabase("test");
      var coll = db.GetCollection<ExpandoObject>("expando");
      var exp = new ExpandoObject();
      ((IDictionary<string, object>)exp)["MyGuid"] = Guid.NewGuid();
      await coll.InsertOneAsync(exp);

      If the first line is omitted, the expandoobject is serialized and saved to db, but not as a UUID, but as binary, e.g.

      Binary('RlOOu1meGkOh0ZMoDqcAiA==', 3)
      
      
      
      

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            m.wildgruber@sevantage.de Markus Wildgruber
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: