Skip to content

Commit af02623

Browse files
committed
Add test +fix for null dictionary propertys.
1 parent 828ea90 commit af02623

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,20 @@ public void CanDeserializeADictionaryWithEnumAsKey()
346346
Assert.AreEqual(9,prop.Dict[DateTimeKind.Utc]);
347347
}
348348

349-
public class DictionaryWithEnumAsValueHelper
349+
public class NullDictionaryPropertyHelper
350350
{
351-
public Dictionary<int,DateTimeKind> Dict { get; set; }
351+
public Dictionary<string, string> Dict { get; set; }
352352
}
353+
354+
[Test]
355+
public void CanDeserializeAndNullDictionaryProperty()
356+
{
357+
var bson = Serialize<Document>(new Document("Dict", null));
358+
var prop = Deserialize<NullDictionaryPropertyHelper>(bson);
359+
360+
Assert.IsNotNull(prop);
361+
Assert.IsNull(prop.Dict);
362+
}
363+
353364
}
354365
}

source/MongoDB/Configuration/DictionaryAdapters/GenericDictionaryDictionaryAdapter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public Type ValueType
3535
/// <returns></returns>
3636
public object CreateDictionary(Document document)
3737
{
38+
if(document==null)
39+
return null;
40+
3841
return document.ToDictionary(pair => (TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
3942
}
4043

source/MongoDB/Configuration/DictionaryAdapters/GenericSortedListDictionaryAdapter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public Type ValueType
3434
/// <returns></returns>
3535
public object CreateDictionary(Document document)
3636
{
37+
if(document == null)
38+
return null;
39+
3740
var list = new SortedList<TKey, TValue>();
3841

3942
foreach(var pair in document)

source/MongoDB/Configuration/DictionaryAdapters/HashtableDictionaryAdapter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public Type ValueType
3434
/// <returns></returns>
3535
public object CreateDictionary(Document document)
3636
{
37+
if(document == null)
38+
return null;
39+
3740
var hashtable = new Hashtable();
3841

3942
foreach (var pair in document)

0 commit comments

Comments
 (0)