Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CSHARP-594: Fixed similar issue in MapToBsonValue and added unit tests.

  • Loading branch information...
commit 40e69fe1cf45b0ed9d68a551b0222a140fa26ba2 1 parent dc92da4
rstam authored
33 Bson/ObjectModel/BsonTypeMapper.cs
View
@@ -34,26 +34,6 @@ public static class BsonTypeMapper
private static Dictionary<Type, Conversion> __fromMappings = new Dictionary<Type, Conversion>
{
{ typeof(bool), Conversion.NewBsonBoolean },
- { typeof(BsonArray), Conversion.None },
- { typeof(BsonBinaryData), Conversion.None },
- { typeof(BsonBoolean), Conversion.None },
- { typeof(BsonDateTime), Conversion.None },
- { typeof(BsonDocument), Conversion.None },
- { typeof(BsonDouble), Conversion.None },
- { typeof(BsonInt32), Conversion.None },
- { typeof(BsonInt64), Conversion.None },
- { typeof(BsonJavaScript), Conversion.None },
- { typeof(BsonJavaScriptWithScope), Conversion.None },
- { typeof(BsonMaxKey), Conversion.None },
- { typeof(BsonMinKey), Conversion.None },
- { typeof(BsonNull), Conversion.None },
- { typeof(BsonObjectId), Conversion.None },
- { typeof(BsonRegularExpression), Conversion.None },
- { typeof(BsonString), Conversion.None },
- { typeof(BsonSymbol), Conversion.None },
- { typeof(BsonTimestamp), Conversion.None },
- { typeof(BsonUndefined), Conversion.None },
- { typeof(BsonValue), Conversion.None },
{ typeof(byte), Conversion.ByteToBsonInt32 },
{ typeof(byte[]), Conversion.ByteArrayToBsonBinary },
{ typeof(DateTime), Conversion.DateTimeToBsonDateTime },
@@ -198,6 +178,16 @@ public static BsonValue MapToBsonValue(object value, BsonType bsonType)
}
}
+ // handle subclasses of BsonDocument (like QueryDocument) correctly
+ if (bsonType == BsonType.Document)
+ {
+ var bsonDocument = value as BsonDocument;
+ if (bsonDocument != null)
+ {
+ return bsonDocument;
+ }
+ }
+
var valueType = value.GetType();
if (valueType.IsEnum)
{
@@ -443,9 +433,12 @@ public static bool TryMapToBsonValue(object value, out BsonValue bsonValue)
return true;
}
+ // also handles subclasses of BsonDocument (like QueryDocument) correctly
bsonValue = value as BsonValue;
if (bsonValue != null)
+ {
return true;
+ }
var valueType = value.GetType();
if (valueType.IsEnum)
1  DriverUnitTests/DriverUnitTests.csproj
View
@@ -173,6 +173,7 @@
<Compile Include="Jira\CSharp471Tests.cs" />
<Compile Include="Jira\CSharp524Tests.cs" />
<Compile Include="Jira\CSharp522Tests.cs" />
+ <Compile Include="Jira\CSharp594Tests.cs" />
<Compile Include="Jira\CSharp77Tests.cs" />
<Compile Include="Jira\CSharp92Tests.cs" />
<Compile Include="Jira\CSharp93Tests.cs" />
86 DriverUnitTests/Jira/CSharp594Tests.cs
View
@@ -0,0 +1,86 @@
+/* Copyright 2010-2012 10gen Inc.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+using MongoDB.Bson;
+using MongoDB.Driver;
+using MongoDB.Driver.Linq;
+using MongoDB.Bson.Serialization.Attributes;
+using MongoDB.Driver.Builders;
+
+namespace MongoDB.DriverUnitTests.Jira.CSharp594
+{
+ [TestFixture]
+ public class CSharp594Tests
+ {
+ [Test]
+ public void TestTryMapToBsonValueWithBsonValues()
+ {
+ // test all the BsonValue subclasses because we removed them from the __fromMappings table
+ var testValues = new BsonValue[]
+ {
+ new BsonArray(),
+ new BsonBinaryData(new byte[0]),
+ BsonBoolean.True,
+ new BsonDateTime(DateTime.UtcNow),
+ new BsonDocument("x", 1),
+ new BsonDouble(1.0),
+ new BsonInt32(1),
+ new BsonInt64(1),
+ new BsonJavaScript("code"),
+ new BsonJavaScriptWithScope("code", new BsonDocument("x", 1)),
+ BsonMaxKey.Value,
+ BsonMinKey.Value,
+ BsonNull.Value,
+ new BsonObjectId(ObjectId.GenerateNewId()),
+ new BsonRegularExpression("pattern"),
+ new BsonString("abc"),
+ BsonSymbol.Create("xyz"),
+ new BsonTimestamp(0),
+ BsonUndefined.Value
+ };
+ foreach (var testValue in testValues)
+ {
+ BsonValue bsonValue;
+ var ok = BsonTypeMapper.TryMapToBsonValue(testValue, out bsonValue);
+ Assert.AreEqual(true, ok);
+ Assert.AreSame(testValue, bsonValue);
+ }
+ }
+
+ [Test]
+ public void TestTryMapToBsonValueWithQueryDocument()
+ {
+ var query = new QueryDocument("x", 1);
+ BsonValue bsonValue;
+ var ok = BsonTypeMapper.TryMapToBsonValue(query, out bsonValue);
+ Assert.AreEqual(true, ok);
+ Assert.AreSame(query, bsonValue);
+ }
+
+ [Test]
+ public void TestMapToBsonValueWithQueryDocument()
+ {
+ var query = new QueryDocument("x", 1);
+ var bsonDocument = BsonTypeMapper.MapToBsonValue(query, BsonType.Document);
+ Assert.AreSame(query, bsonDocument);
+ }
+ }
+}
1  README.md
View
@@ -27,6 +27,7 @@ http://jira.mongodb.org/browse/CSHARP
* Teun Duynstee teun@duynstee.com
* Ken Egozi mail@kenegozi.com
* Simon Green simon@captaincodeman.com
+* Oleg Kosmakov kosmakoff@gmail.com
* Brian Knight brianknight10@gmail.com
* Richard Kreuter richard@10gen.com
* Kevin Lewis kevin.l.lewis@gmail.com
Please sign in to comment.
Something went wrong with that request. Please try again.