Skip to content
Browse files

Add test +fix for null dictionary propertys.

  • Loading branch information...
1 parent 828ea90 commit af026235d067451f1df06530586b93118ebcaf02 @lanwin lanwin committed
View
15 source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs
@@ -346,9 +346,20 @@ public void CanDeserializeADictionaryWithEnumAsKey()
Assert.AreEqual(9,prop.Dict[DateTimeKind.Utc]);
}
- public class DictionaryWithEnumAsValueHelper
+ public class NullDictionaryPropertyHelper
{
- public Dictionary<int,DateTimeKind> Dict { get; set; }
+ public Dictionary<string, string> Dict { get; set; }
}
+
+ [Test]
+ public void CanDeserializeAndNullDictionaryProperty()
+ {
+ var bson = Serialize<Document>(new Document("Dict", null));
+ var prop = Deserialize<NullDictionaryPropertyHelper>(bson);
+
+ Assert.IsNotNull(prop);
+ Assert.IsNull(prop.Dict);
+ }
+
}
}
View
3 source/MongoDB/Configuration/DictionaryAdapters/GenericDictionaryDictionaryAdapter.cs
@@ -35,6 +35,9 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
+ if(document==null)
+ return null;
+
return document.ToDictionary(pair => (TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
}
View
3 source/MongoDB/Configuration/DictionaryAdapters/GenericSortedListDictionaryAdapter.cs
@@ -34,6 +34,9 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
+ if(document == null)
+ return null;
+
var list = new SortedList<TKey, TValue>();
foreach(var pair in document)
View
3 source/MongoDB/Configuration/DictionaryAdapters/HashtableDictionaryAdapter.cs
@@ -34,6 +34,9 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
+ if(document == null)
+ return null;
+
var hashtable = new Hashtable();
foreach (var pair in document)

0 comments on commit af02623

Please sign in to comment.
Something went wrong with that request. Please try again.