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

ObjectSerializer should not be dependent on the order of the _t and _v elements

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.10.1, 2.0.2, 2.1
    • Affects Version/s: 1.10
    • Component/s: Serialization
    • Environment:
      C# Driver 1.10.0.62, mongo 2.6.10 on Ubuntu

      Customer reported this issue. They receive an exception when the driver attempts to deserialize documents:

      Failed EXCEPTION: System.IO.FileFormatException: An error occurred while deserializing the Fields property of class Expected element name to be '_t', not '_v'. ---> System.IO.FileFormatException: Expected element name to be '_t', not '_v'.
      at MongoDB.Bson.IO.BsonReader.VerifyName(String expectedName)
      at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase`1.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
      at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
      — End of inner exception stack trace —
      at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
      at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
      at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase`1.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
      at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
      

      Customer provided code sample:

      Here's the call stack:

       at MongoDB.Bson.IO.BsonReader.VerifyName(String expectedName)
         at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase`1.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
         at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
         --- End of inner exception stack trace ---
         at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
         at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
         at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase`1.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
         at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
         --- End of inner exception stack trace ---
         at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
         at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
         at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
         at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadBodyFrom(BsonBuffer buffer)
         at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializer serializer, IBsonSerializationOptions serializationOptions)
         at MongoDB.Driver.Operations.QueryOperation`1.GetFirstBatch(IConnectionProvider connectionProvider)
         at MongoDB.Driver.Operations.QueryOperation`1.Execute(IConnectionProvider connectionProvider)
         at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
         at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
         at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector)
         at OT.GC.DataMart.ETL.ReservationHistory.Repository.MongoDb.MongoDbReservationHistoryRepository.Get(Int64 rid, String reservationId)
      
      

      and also this reduced sample which reproduces the error:

      namespace WikiExampleConsole
      {
        public class TestClass
        {
          public object SomeData;
        }
       
        class Program
        {
          static void Main(string[] args)
          {
            string rawJsonTFirst = "{ \"SomeData\" : { \"_t\" : \"System.Object[]\", \"_v\" : [] } }";
            Console.WriteLine("_t first {0}", BsonSerializer.Deserialize<TestClass>(rawJsonTFirst)); // succeeds
       
            string rawJsonVFirst = "{ \"SomeData\" : { \"_v\" : [], \"_t\" : \"System.Object[]\" } }";
            Console.WriteLine("_v first {0}", BsonSerializer.Deserialize<TestClass>(rawJsonVFirst)); // throws
          }
        }
      }
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            jared.wadsworth Jared Wadsworth
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: