Skip to content

Commit 3fac6fe

Browse files
author
Robert Stam
committed
Merge branch 'csharp_467' of git://github.com/craiggwilson/mongo-csharp-driver into csharp467
2 parents 9abd747 + 01c9058 commit 3fac6fe

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

Bson/Serialization/BsonMemberMap.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class BsonMemberMap
5252
private bool _ignoreIfDefault;
5353
private bool _ignoreIfNull;
5454
private object _defaultValue;
55+
private bool _defaultValueSpecified;
5556

5657
// constructors
5758
/// <summary>
@@ -65,6 +66,7 @@ public BsonMemberMap(BsonClassMap classMap, MemberInfo memberInfo)
6566
_memberInfo = memberInfo;
6667
_memberType = BsonClassMap.GetMemberInfoType(memberInfo);
6768
_defaultValue = GetDefaultValue(_memberType);
69+
_defaultValueSpecified = false;
6870
}
6971

7072
// public properties
@@ -277,7 +279,10 @@ public bool IsReadOnly
277279
/// <param name="obj">The object.</param>
278280
public void ApplyDefaultValue(object obj)
279281
{
280-
this.Setter(obj, _defaultValue);
282+
if (_defaultValueSpecified)
283+
{
284+
this.Setter(obj, _defaultValue);
285+
}
281286
}
282287

283288
/// <summary>
@@ -321,6 +326,7 @@ public IBsonSerializer GetSerializer(Type actualType)
321326
public BsonMemberMap SetDefaultValue(object defaultValue)
322327
{
323328
_defaultValue = defaultValue;
329+
_defaultValueSpecified = true;
324330
return this;
325331
}
326332

BsonUnitTests/Jira/CSharp467Tests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright 2010-2012 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Linq;
18+
using NUnit.Framework;
19+
20+
using MongoDB.Bson;
21+
using MongoDB.Bson.Serialization;
22+
using MongoDB.Bson.Serialization.Attributes;
23+
24+
namespace MongoDB.BsonUnitTests.Jira
25+
{
26+
[TestFixture]
27+
public class CSharp467Tests
28+
{
29+
public class A
30+
{
31+
public string Value1 { get; set; }
32+
public string Value2 { get; set; }
33+
[BsonDefaultValue("Three")]
34+
public string Value3 { get; set; }
35+
36+
public A()
37+
{
38+
Value1 = "Default";
39+
Value2 = "Default";
40+
}
41+
}
42+
43+
[Test]
44+
public void TestOnlySpecifiedValuesAndSpecifiedDefaultValuesAreWrittenUponDeserialization()
45+
{
46+
var doc = new BsonDocument
47+
{
48+
new BsonElement("Value1", "One")
49+
};
50+
51+
var bson = doc.ToBson();
52+
var rehydrated = BsonSerializer.Deserialize<A>(bson);
53+
54+
Assert.AreEqual(rehydrated.Value1, "One");
55+
Assert.AreEqual(rehydrated.Value2, "Default");
56+
Assert.AreEqual(rehydrated.Value3, "Three");
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)