Skip to content

Commit 779ee62

Browse files
author
rstam
committed
CSHARP-930: Handle dictionary keys with null characters.
1 parent 3480169 commit 779ee62

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

MongoDB.Bson/Serialization/Serializers/DictionaryGenericSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public override void Serialize(
172172
foreach (object key in dictionary.Keys)
173173
{
174174
var name = key as string; // key might not be a string
175-
if (string.IsNullOrEmpty(name) || name[0] == '$' || name.IndexOf('.') != -1)
175+
if (string.IsNullOrEmpty(name) || name[0] == '$' || name.IndexOf('.') != -1 || name.IndexOf('\0') != -1)
176176
{
177177
dictionaryRepresentation = DictionaryRepresentation.ArrayOfArrays;
178178
break;

MongoDB.Bson/Serialization/Serializers/DictionarySerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public override void Serialize(
183183
foreach (object key in dictionary.Keys)
184184
{
185185
var name = key as string; // key might not be a string
186-
if (string.IsNullOrEmpty(name) || name[0] == '$' || name.IndexOf('.') != -1)
186+
if (string.IsNullOrEmpty(name) || name[0] == '$' || name.IndexOf('.') != -1 || name.IndexOf('\0') != -1)
187187
{
188188
dictionaryRepresentation = DictionaryRepresentation.ArrayOfArrays;
189189
break;

MongoDB.BsonUnitTests/Serialization/Serializers/DictionaryGenericSerializerTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ public void TestEmpty()
8080
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
8181
}
8282

83+
[TestCase("")]
84+
[TestCase("$x")]
85+
[TestCase("x.y")]
86+
[TestCase("x\0")]
87+
[TestCase("x\u0000")]
88+
public void TestInvalidKey(string key)
89+
{
90+
var d = new Dictionary<object, object> { { key, 1 } };
91+
var sd = CreateSortedDictionary(d);
92+
var sl = CreateSortedList(d);
93+
var obj = new T { D = d, ID = d, SD = sd, SL = sl };
94+
var json = obj.ToJson();
95+
var rep = string.Format("[['{0}', 1]]", key.Replace("\u0000", "\\u0000"));
96+
var expected = "{ 'D' : #R, 'ID' : #R, 'SD' : #R, 'SL' : #R }".Replace("#R", rep).Replace("'", "\"");
97+
Assert.AreEqual(expected, json);
98+
99+
var bson = obj.ToBson();
100+
var rehydrated = BsonSerializer.Deserialize<T>(bson);
101+
Assert.IsInstanceOf<Dictionary<object, object>>(rehydrated.D);
102+
Assert.IsInstanceOf<Dictionary<object, object>>(rehydrated.ID);
103+
Assert.IsInstanceOf<SortedDictionary<object, object>>(rehydrated.SD);
104+
Assert.IsInstanceOf<SortedList<object, object>>(rehydrated.SL);
105+
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
106+
}
107+
83108
[Test]
84109
public void TestOneC()
85110
{

MongoDB.BsonUnitTests/Serialization/Serializers/DictionarySerializerTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ public void TestEmpty()
8585
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
8686
}
8787

88+
[TestCase("")]
89+
[TestCase("$x")]
90+
[TestCase("x.y")]
91+
[TestCase("x\0")]
92+
[TestCase("x\u0000")]
93+
public void TestInvalidKey(string key)
94+
{
95+
var ht = new Hashtable { { key, 1 } };
96+
var ld = CreateListDictionary(ht);
97+
var od = CreateOrderedDictionary(ht);
98+
var sl = CreateSortedList(ht);
99+
var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
100+
var json = obj.ToJson();
101+
var rep = string.Format("[['{0}', 1]]", key.Replace("\u0000", "\\u0000"));
102+
var expected = "{ 'HT' : #R, 'ID' : #R, 'LD' : #R, 'OD' : #R, 'SL' : #R }".Replace("#R", rep).Replace("'", "\"");
103+
Assert.AreEqual(expected, json);
104+
105+
var bson = obj.ToBson();
106+
var rehydrated = BsonSerializer.Deserialize<T>(bson);
107+
Assert.IsInstanceOf<Hashtable>(rehydrated.HT);
108+
Assert.IsInstanceOf<Hashtable>(rehydrated.ID);
109+
Assert.IsInstanceOf<ListDictionary>(rehydrated.LD);
110+
Assert.IsInstanceOf<OrderedDictionary>(rehydrated.OD);
111+
Assert.IsInstanceOf<SortedList>(rehydrated.SL);
112+
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
113+
}
114+
88115
[Test]
89116
public void TestOneC()
90117
{

0 commit comments

Comments
 (0)