Enum Deserializer Issue with BsonType.String

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.7.0
    • Component/s: BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None

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

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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.

              Assignee:
              Robert Stam
              Reporter:
              Sun Yu
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: