Skip to content

Commit

Permalink
Incorporated Brian Knight's fix for CSHARP-310 with minor changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Oct 18, 2011
1 parent af4da34 commit bb88a48
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Bson/Serialization/BsonClassMap.cs
Expand Up @@ -878,7 +878,7 @@ MemberInfo memberInfo

var defaultValue = conventions.DefaultValueConvention.GetDefaultValue(memberInfo);
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
Expand Down
9 changes: 5 additions & 4 deletions Bson/Serialization/BsonMemberMap.cs
Expand Up @@ -242,7 +242,9 @@ Type actualType
public BsonMemberMap SetDefaultValue(
object defaultValue
) {
return SetDefaultValue(defaultValue, true); // serializeDefaultValue
this.defaultValue = defaultValue;
this.hasDefaultValue = true;
return this;
}

/// <summary>
Expand All @@ -255,9 +257,8 @@ object defaultValue
object defaultValue,
bool serializeDefaultValue
) {
this.hasDefaultValue = true;
this.serializeDefaultValue = serializeDefaultValue;
this.defaultValue = defaultValue;
SetDefaultValue(defaultValue);
SetSerializeDefaultValue(serializeDefaultValue);
return this;
}

Expand Down
11 changes: 4 additions & 7 deletions BsonUnitTests/Jira/CSharp310Tests.cs
Expand Up @@ -24,23 +24,20 @@
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;


namespace MongoDB.BsonUnitTests.Jira {
[TestFixture]
public class CSharp310Tests {
private class C {
public int Id;
public Guid G;
public Guid G = Guid.Empty;
}

private class EmptyGuidDefaultValueConvention : IDefaultValueConvention {
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)) {
return Guid.Empty;
}
else {
} else {
return null;
}
}
Expand All @@ -67,4 +64,4 @@ private class EmptyGuidDefaultValueConvention : IDefaultValueConvention {
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson<object>()));
}
}
}
}

0 comments on commit bb88a48

Please sign in to comment.