Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CSHARP-564: fix serialization of properties declared as an interface …

…and having a value of null.
  • Loading branch information...
commit 98a01290ffd0ae4e46aa5f96a5b4acce4bc6a5a1 1 parent aa6fc73
rstam authored
View
43 Bson/Serialization/BsonClassMapSerializer.cs
@@ -473,19 +473,30 @@ private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap
{
try
{
+ object value;
+
var nominalType = memberMap.MemberType;
- Type actualType;
- if (bsonReader.GetCurrentBsonType() == BsonType.Null)
+ var bsonType = bsonReader.GetCurrentBsonType();
+ if (bsonType == BsonType.Null && nominalType.IsInterface)
{
- actualType = nominalType;
+ bsonReader.ReadNull();
+ value = null;
}
- else
- {
- var discriminatorConvention = memberMap.GetDiscriminatorConvention();
- actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found
+ else{
+ Type actualType;
+ if (bsonType == BsonType.Null)
+ {
+ actualType = nominalType;
+ }
+ else
+ {
+ var discriminatorConvention = memberMap.GetDiscriminatorConvention();
+ actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found
+ }
+ var serializer = memberMap.GetSerializer(actualType);
+ value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions);
}
- var serializer = memberMap.GetSerializer(actualType);
- var value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions);
+
memberMap.Setter(obj, value);
}
catch (Exception ex)
@@ -541,10 +552,18 @@ private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap me
}
bsonWriter.WriteName(memberMap.ElementName);
+
var nominalType = memberMap.MemberType;
- var actualType = (value == null) ? nominalType : value.GetType();
- var serializer = memberMap.GetSerializer(actualType);
- serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
+ if (value == null && nominalType.IsInterface)
+ {
+ bsonWriter.WriteNull();
+ }
+ else
+ {
+ var actualType = (value == null) ? nominalType : value.GetType();
+ var serializer = memberMap.GetSerializer(actualType);
+ serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
+ }
}
private void VerifyNominalType(Type nominalType)
View
1  BsonUnitTests/BsonUnitTests.csproj
@@ -151,6 +151,7 @@
<Compile Include="Jira\CSharp467Tests.cs" />
<Compile Include="Jira\CSharp479Tests.cs" />
<Compile Include="Jira\CSharp515Tests.cs" />
+ <Compile Include="Jira\CSharp564Tests.cs" />
<Compile Include="Jira\CSharp70Tests.cs" />
<Compile Include="Jira\CSharp71Tests.cs" />
<Compile Include="Jira\CSharp74Tests.cs" />
View
62 BsonUnitTests/Jira/CSharp564Tests.cs
@@ -0,0 +1,62 @@
+/* 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.Bson.Serialization;
+
+namespace MongoDB.BsonUnitTests.Jira.CSharp564
+{
+ [TestFixture]
+ public class CSharp564Tests
+ {
+ interface IWhatever { }
+ class Whatever : IWhatever { }
+
+ class Person
+ {
+ public IWhatever Whatever { get; set; }
+ }
+
+ [Test]
+ public void TestPersonWhateverNull()
+ {
+ var p = new Person();
+ var json = p.ToJson();
+ var expected = "{ 'Whatever' : null }".Replace("'", "\"");
+ Assert.AreEqual(expected, json);
+
+ var r = BsonSerializer.Deserialize<Person>(json);
+ Assert.AreEqual(null, r.Whatever);
+ }
+
+ [Test]
+ public void TestPersonWhateverNotNull()
+ {
+ var p = new Person() { Whatever = new Whatever() };
+ var json = p.ToJson();
+ var expected = "{ 'Whatever' : { '_t' : 'Whatever' } }".Replace("'", "\"");
+ Assert.AreEqual(expected, json);
+
+ var r = BsonSerializer.Deserialize<Person>(json);
+ Assert.IsInstanceOf<Whatever>(r.Whatever);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.