Skip to content

Commit a6465a6

Browse files
author
Robert Stam
committed
CSHARP-674: Handle null keys as well as null values in KeyValuePairSerializer.
1 parent 8b2c494 commit a6465a6

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,11 @@ public override void Serialize(
166166
var keyValuePair = (KeyValuePair<TKey, TValue>)value;
167167
var keyValuePairSerializationOptions = EnsureSerializationOptions<KeyValuePairSerializationOptions>(options);
168168

169-
var keySerializer = GetKeySerializer(keyValuePair.Key.GetType());
170-
171-
IBsonSerializer valueSerializer;
172-
if (null == keyValuePair.Value)
173-
{
174-
valueSerializer = GetValueSerializer(typeof(object));
175-
}
176-
else
177-
{
178-
valueSerializer = GetValueSerializer(keyValuePair.Value.GetType());
179-
}
169+
var keyType = (keyValuePair.Key == null) ? typeof(TKey) : keyValuePair.Key.GetType();
170+
var keySerializer = GetKeySerializer(keyType);
171+
var valueType = (keyValuePair.Value == null) ? typeof(TValue) : keyValuePair.Value.GetType();
172+
var valueSerializer = GetValueSerializer(valueType);
173+
180174
switch (keyValuePairSerializationOptions.Representation)
181175
{
182176
case BsonType.Array:

MongoDB.BsonUnitTests/MongoDB.BsonUnitTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<Compile Include="Serialization\LegacyBsonClassMapTests.cs" />
114114
<Compile Include="Serialization\Serializers\ExtraElementsTests.cs" />
115115
<Compile Include="Serialization\Options\RepresentationSerializationOptionsTests.cs" />
116+
<Compile Include="Serialization\Serializers\KeyValuePairSerializerTests.cs" />
116117
<Compile Include="Serialization\Serializers\SerializeFlagsTests.cs" />
117118
<Compile Include="Serialization\Serializers\AnimalHierarchyWithAttributesTests.cs" />
118119
<Compile Include="Serialization\Serializers\AnimalHierarchyWithoutAttributesTests.cs" />
@@ -241,4 +242,4 @@
241242
<Target Name="AfterBuild">
242243
</Target>
243244
-->
244-
</Project>
245+
</Project>

MongoDB.BsonUnitTests/Serialization/Serializers/KeyValuePairSerializerTests.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2012 10gen Inc.
1+
/* Copyright 2010-2013 10gen Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -13,29 +13,36 @@
1313
* limitations under the License.
1414
*/
1515

16-
using System;
1716
using System.Collections.Generic;
18-
using System.IO;
1917
using System.Linq;
20-
using System.Text;
21-
using NUnit.Framework;
22-
2318
using MongoDB.Bson;
24-
using MongoDB.Bson.IO;
2519
using MongoDB.Bson.Serialization;
26-
using MongoDB.Bson.Serialization.Attributes;
20+
using NUnit.Framework;
2721

2822
namespace MongoDB.BsonUnitTests.DefaultSerializer.Serializers
2923
{
3024
[TestFixture]
3125
public class KeyValuePairSerializerTests
3226
{
27+
[Test]
28+
public void TestNullKey()
29+
{
30+
var kvp = new KeyValuePair<string, object>(null, "value");
31+
var json = kvp.ToJson();
32+
var expected = "{ 'k' : null, 'v' : 'value' }".Replace("'", "\"");
33+
Assert.AreEqual(expected, json);
34+
35+
var bson = kvp.ToBson();
36+
var rehydrated = BsonSerializer.Deserialize<KeyValuePair<string, object>>(bson);
37+
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
38+
}
39+
3340
[Test]
3441
public void TestNullValue()
3542
{
36-
KeyValuePair<string, object> kvp = new KeyValuePair<string, object>("Value", null);
43+
var kvp = new KeyValuePair<string, object>("key", null);
3744
var json = kvp.ToJson();
38-
var expected = "{ 'k' : 'Value', 'v' : null }".Replace("'", "\"");
45+
var expected = "{ 'k' : 'key', 'v' : null }".Replace("'", "\"");
3946
Assert.AreEqual(expected, json);
4047

4148
var bson = kvp.ToBson();

0 commit comments

Comments
 (0)