From 40e69fe1cf45b0ed9d68a551b0222a140fa26ba2 Mon Sep 17 00:00:00 2001 From: rstam Date: Tue, 9 Oct 2012 09:54:26 -0400 Subject: [PATCH] CSHARP-594: Fixed similar issue in MapToBsonValue and added unit tests. --- Bson/ObjectModel/BsonTypeMapper.cs | 33 ++++------ DriverUnitTests/DriverUnitTests.csproj | 1 + DriverUnitTests/Jira/CSharp594Tests.cs | 86 ++++++++++++++++++++++++++ README.md | 1 + 4 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 DriverUnitTests/Jira/CSharp594Tests.cs diff --git a/Bson/ObjectModel/BsonTypeMapper.cs b/Bson/ObjectModel/BsonTypeMapper.cs index c3920143c5..bdd3824a4e 100644 --- a/Bson/ObjectModel/BsonTypeMapper.cs +++ b/Bson/ObjectModel/BsonTypeMapper.cs @@ -34,26 +34,6 @@ namespace MongoDB.Bson private static Dictionary __fromMappings = new Dictionary { { 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 @@ namespace MongoDB.Bson } } + // 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 @@ namespace MongoDB.Bson 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) diff --git a/DriverUnitTests/DriverUnitTests.csproj b/DriverUnitTests/DriverUnitTests.csproj index b3a5d190e4..9f76a20f34 100644 --- a/DriverUnitTests/DriverUnitTests.csproj +++ b/DriverUnitTests/DriverUnitTests.csproj @@ -173,6 +173,7 @@ + diff --git a/DriverUnitTests/Jira/CSharp594Tests.cs b/DriverUnitTests/Jira/CSharp594Tests.cs new file mode 100644 index 0000000000..ac8502a3d5 --- /dev/null +++ b/DriverUnitTests/Jira/CSharp594Tests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index bd70827a68..b46eb51b18 100644 --- a/README.md +++ b/README.md @@ -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