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

Enum Deserializer Issue with BsonType.String

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • 2.7.0
    • BSON

    Description

      In the source code of c# Driver v2.7,

      namespace MongoDB.Bson.Serialization.Serializers
      {
          public class EnumSerializer<TEnum> : StructSerializerBase<TEnum>, IRepresentationConfigurable<EnumSerializer<TEnum>> where TEnum : struct
          {
            ......
            public override TEnum Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
            {
              var bsonReader = context.Reader;
       
              var bsonType = bsonReader.GetCurrentBsonType();
              switch (bsonType)
              {
                  ......
                  case BsonType.String: return (TEnum)Enum.Parse(typeof(TEnum), bsonReader.ReadString());
                  default:
                      throw CreateCannotDeserializeFromBsonTypeException(bsonType);
              }
            }
           ......
          }
      }
      

      In this method, the code

      case BsonType.String: return (TEnum)Enum.Parse(typeof(TEnum), bsonReader.ReadString());
      

      will throw an ArgumentException when the ReadString() returns a value that is not defined in the TEnum type.

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            sunyutj Sun Yu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: