Global GuidSerializer settings ignored for object-typed properties

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • 3.10.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Fully Compatible
    • Dotnet Drivers
    • Not Needed
    • 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?
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      The MongoDB .NET driver ignores globally registered GUID serialization settings when serializing properties of type object.

      This leads to an exception during serialization:

      MongoDB.Bson.BsonSerializationException : An error occurred while serializing the Value property of class MongoCases.MongoCases_Linq3_Guid+ParameterDbo: GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.
        ----> MongoDB.Bson.BsonSerializationException : GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.
         at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.SerializeMember(BsonSerializationContext context, Object obj, BsonMemberMap memberMap)
         at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.SerializeClass(BsonSerializationContext context, BsonSerializationArgs args, TClass document)
         at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
         at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, Object value)
         at MongoDB.Bson.BsonExtensionMethods.ToBsonDocument(Object obj, Type nominalType, IBsonSerializer serializer, Action`1 configurator, BsonSerializationArgs args)
         at MongoDB.Bson.BsonExtensionMethods.ToBsonDocument[TNominalType](TNominalType obj, IBsonSerializer`1 serializer, Action`1 configurator, BsonSerializationArgs args)

          

      <PackageReference Include="MongoDB.Bson" Version="3.5.2" />
      <PackageReference Include="MongoDB.Driver" Version="3.5.2" />

      How to Reproduce

       

      using MongoDB.Bson;
      using MongoDB.Bson.Serialization;
      using MongoDB.Bson.Serialization.Attributes;
      using MongoDB.Bson.Serialization.Serializers;
      
      namespace MongoCases;
      
      [TestFixture]
      public class MongoCases_Linq3_Guid
      {
          [Test]
          public async Task Test()
          {
              BsonSerializer.TryRegisterSerializer(new GuidSerializer(GuidRepresentation.CSharpLegacy));
      
              var parameterDbo = new ParameterDbo
              {
                  Type = "UserId",
                  Value = Guid.NewGuid()
              };
      
              var bson = parameterDbo.ToBsonDocument();
      
              // MongoDB.Bson.BsonSerializationException : An error occurred while serializing the Value property of class
              // MongoCases.MongoCases_Linq3_Guid+ParameterDbo: GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.
      
              Assert.That(bson, Is.Not.Null);
          }
      
          class ParameterDbo
          {
              [BsonId]
              public ObjectId Id { get; set; }
      
              [BsonElement("type")]
              public string Type { get; set; }
      
              [BsonElement("value")]
              public object Value { get; set; }
          }
      } 

       

       

            Assignee:
            Ferdinando Papale
            Reporter:
            flibustier seas
            None
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: