Browse Source

Incorporated Brian Knight's fix for CSHARP-310 with minor changes.

pull/71/head
rstam 14 years ago
parent
commit
bb88a48b01
  1. 2
      Bson/Serialization/BsonClassMap.cs
  2. 9
      Bson/Serialization/BsonMemberMap.cs
  3. 11
      BsonUnitTests/Jira/CSharp310Tests.cs

2
Bson/Serialization/BsonClassMap.cs

@ -878,7 +878,7 @@ namespace MongoDB.Bson.Serialization {
var defaultValue = conventions.DefaultValueConvention.GetDefaultValue(memberInfo); var defaultValue = conventions.DefaultValueConvention.GetDefaultValue(memberInfo);
if (defaultValue != null) { if (defaultValue != null) {
memberMap.SetDefaultValue(defaultValue, memberMap.SerializeDefaultValue);
memberMap.SetDefaultValue(defaultValue);
} }
// see if the class has a method called ShouldSerializeXyz where Xyz is the name of this member // see if the class has a method called ShouldSerializeXyz where Xyz is the name of this member

9
Bson/Serialization/BsonMemberMap.cs

@ -242,7 +242,9 @@ namespace MongoDB.Bson.Serialization {
public BsonMemberMap SetDefaultValue( public BsonMemberMap SetDefaultValue(
object defaultValue object defaultValue
) { ) {
return SetDefaultValue(defaultValue, true); // serializeDefaultValue
this.defaultValue = defaultValue;
this.hasDefaultValue = true;
return this;
} }
/// <summary> /// <summary>
@ -255,9 +257,8 @@ namespace MongoDB.Bson.Serialization {
object defaultValue, object defaultValue,
bool serializeDefaultValue bool serializeDefaultValue
) { ) {
this.hasDefaultValue = true;
this.serializeDefaultValue = serializeDefaultValue;
this.defaultValue = defaultValue;
SetDefaultValue(defaultValue);
SetSerializeDefaultValue(serializeDefaultValue);
return this; return this;
} }

11
BsonUnitTests/Jira/CSharp310Tests.cs

@ -24,23 +24,20 @@ using MongoDB.Bson;
using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions; using MongoDB.Bson.Serialization.Conventions;
namespace MongoDB.BsonUnitTests.Jira { namespace MongoDB.BsonUnitTests.Jira {
[TestFixture] [TestFixture]
public class CSharp310Tests { public class CSharp310Tests {
private class C { private class C {
public int Id; public int Id;
public Guid G;
public Guid G = Guid.Empty;
} }
private class EmptyGuidDefaultValueConvention : IDefaultValueConvention { private class EmptyGuidDefaultValueConvention : IDefaultValueConvention {
public object GetDefaultValue(MemberInfo memberInfo) { public object GetDefaultValue(MemberInfo memberInfo) {
var type = (memberInfo.MemberType == MemberTypes.Field)
? ((FieldInfo)memberInfo).FieldType : ((PropertyInfo)memberInfo).PropertyType;
var type = (memberInfo.MemberType == MemberTypes.Field) ? ((FieldInfo) memberInfo).FieldType : ((PropertyInfo) memberInfo).PropertyType;
if (type == typeof(Guid)) { if (type == typeof(Guid)) {
return Guid.Empty; return Guid.Empty;
}
else {
} else {
return null; return null;
} }
} }
@ -67,4 +64,4 @@ namespace MongoDB.BsonUnitTests.Jira {
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson<object>())); Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson<object>()));
} }
} }
}
}
Loading…
Cancel
Save