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

MongoDB.Bson.Serialization.Serializers.DictionarySerializer do not support empty string as key when CheckElementNames is true

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Blocker - P1 Blocker - P1
    • 1.9
    • Affects Version/s: 1.8.3
    • Component/s: Serialization
    • Labels:
    • Minor Change

      We have been running into a pretty bad bug where we noticed some objects were failing to be inserted into mongo. We were getting the following stack trace:

      System.IndexOutOfRangeException
      Index was outside the bounds of the array.
      at MongoDB.Bson.IO.BsonWriter.CheckElementName(String name)
      at MongoDB.Bson.IO.BsonWriter.WriteName(String name)
      at MongoDB.Bson.Serialization.Serializers.DictionarySerializer`2.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
      at MongoDB.Bson.Serialization.BsonClassMapSerializer.SerializeMember(BsonWriter bsonWriter, Object obj, BsonMemberMap memberMap)
      at MongoDB.Bson.Serialization.BsonClassMapSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
      at MongoDB.Bson.Serialization.BsonClassMapSerializer.SerializeMember(BsonWriter bsonWriter, Object obj, BsonMemberMap memberMap)
      at MongoDB.Bson.Serialization.BsonClassMapSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
      at MongoDB.Driver.Internal.MongoInsertMessage.AddDocument(BsonBuffer buffer, Type nominalType, Object document)
      at MongoDB.Driver.Operations.InsertOperation.Execute(MongoConnection connection)
      at MongoDB.Driver.MongoCollection.InsertBatch(Type nominalType, IEnumerable documents, MongoInsertOptions options)
      at MongoDB.Driver.MongoCollection.Insert(Type nominalType, Object document, MongoInsertOptions options)

      When researching this issue we came across this: https://jira.mongodb.org/browse/CSHARP-624, which seemed like it was the issue but we were still experiencing it...

      Upon looking at the code... We tracked this down to the CheckElementName name is not checking for an empty string (just null), it's just trying to access the first character (which there are none). The following unit test reproduces this issue in the smallest amount of code possible.

      [Test]
      public void TestEmptyDictionaryKeySerialization() {
      var dictionary = new Dictionary<string, string>() {

      { "k", "v" }

      ,

      { "", "emptyKey" }

      };

      using (var buffer = new BsonBuffer()) {
      using (var bsonWriter = BsonWriter.Create(buffer, new BsonBinaryWriterSettings()))

      { bsonWriter.CheckElementNames = true; var serializer = new Bson.Serialization.Serializers.DictionarySerializer(); serializer.Serialize(bsonWriter, dictionary.GetType(), dictionary, serializer.DefaultSerializationOptions); }

      }
      }

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            bniemyjski Blake Niemyjski
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: