Permalink
Browse files

CSHARP-532: fixed bug with typed builders when using a value with a d…

…ifferent type than the nominal type.
  • Loading branch information...
1 parent cc6218c commit 42c17f2c636315f762d2a450e9b6d7cde6ca6e53 @craiggwilson committed Jul 19, 2012
@@ -100,7 +100,7 @@ public BsonValue SerializeValue(BsonSerializationInfo serializationInfo, object
var bsonWriter = BsonWriter.Create(bsonDocument);
bsonWriter.WriteStartDocument();
bsonWriter.WriteName("value");
- serializationInfo.Serializer.Serialize(bsonWriter, serializationInfo.NominalType, value, serializationInfo.SerializationOptions);
+ Serialize(bsonWriter, serializationInfo, value);
bsonWriter.WriteEndDocument();
return bsonDocument[0];
}
@@ -120,11 +120,23 @@ public BsonArray SerializeValues(BsonSerializationInfo serializationInfo, IEnume
bsonWriter.WriteStartArray();
foreach (var value in values)
{
- serializationInfo.Serializer.Serialize(bsonWriter, serializationInfo.NominalType, value, serializationInfo.SerializationOptions);
+ Serialize(bsonWriter, serializationInfo, value);
}
bsonWriter.WriteEndArray();
bsonWriter.WriteEndDocument();
return bsonDocument[0].AsBsonArray;
}
+
+ // private methods
+ private void Serialize(BsonWriter bsonWriter, BsonSerializationInfo serializationInfo, object value)
+ {
+ var serializer = serializationInfo.Serializer;
+ var actualType = (value == null) ? serializationInfo.NominalType : value.GetType();
+ if (actualType != serializationInfo.NominalType)
+ {
+ serializer = BsonSerializer.LookupSerializer(actualType);
+ }
+ serializer.Serialize(bsonWriter, serializationInfo.NominalType, value, serializationInfo.SerializationOptions);
+ }
}
-}
+}
@@ -127,6 +127,7 @@
<Compile Include="GridFS\MongoGridFSSettingsTests.cs" />
<Compile Include="GridFS\MongoGridFSStreamTests.cs" />
<Compile Include="GridFS\MongoGridFSTests.cs" />
+ <Compile Include="Jira\CSharp532Tests.cs" />
<Compile Include="Jira\CSharp475Tests.cs" />
<Compile Include="Jira\CSharp101Tests.cs" />
<Compile Include="Jira\CSharp103Tests.cs" />
@@ -0,0 +1,58 @@
+/* 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.CSharp532
+{
+ [TestFixture]
+ public class CSharp532Tests
+ {
+ [BsonKnownTypes(typeof(B))]
+ public abstract class A
+ {
+
+ }
+
+ public class B : A
+ {
+
+ }
+
+ public class C
+ {
+ [BsonElement("a")]
+ public A A { get; set; }
+ }
+
+ [Test]
+ public void TestTypedBuildersWithSubclasses()
+ {
+ var b = new B();
+
+ var t = Update<C>.Set(c => c.A, b);
+ }
+ }
+}

0 comments on commit 42c17f2

Please sign in to comment.