Skip to content

Commit de66bed

Browse files
Sridhar Nanjundeswaranrstam
Sridhar Nanjundeswaran
authored and
rstam
committed
CSHARP-803 - Ignore overriden id members when applying NamedIdMemberConvention
1 parent 0bcca2e commit de66bed

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

MongoDB.Bson/Serialization/Conventions/NamedIdMemberConvention.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,28 @@ public void Apply(BsonClassMap classMap)
109109

110110
if (member != null)
111111
{
112-
classMap.MapIdMember(member);
113-
return;
112+
if (IsValidIdMember(classMap, member))
113+
{
114+
classMap.MapIdMember(member);
115+
return;
116+
}
114117
}
115118
}
116119
}
117120

121+
private bool IsValidIdMember(BsonClassMap classMap, MemberInfo member)
122+
{
123+
if (member.MemberType == MemberTypes.Property)
124+
{
125+
var getMethodInfo = ((PropertyInfo)member).GetGetMethod(true);
126+
if (getMethodInfo.IsVirtual && getMethodInfo.GetBaseDefinition().DeclaringType != classMap.ClassType)
127+
{
128+
return false;
129+
}
130+
}
131+
return true;
132+
}
133+
118134
/// <summary>
119135
/// Finds the Id member of a class.
120136
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright 2010-2013 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 MongoDB.Bson;
17+
using NUnit.Framework;
18+
19+
namespace MongoDB.BsonUnitTests.Jira
20+
{
21+
[TestFixture]
22+
public class CSharp803Tests
23+
{
24+
private abstract class BaseClassWithProperty
25+
{
26+
public abstract int Id { get; set; }
27+
}
28+
29+
private class PropertyImpl : BaseClassWithProperty
30+
{
31+
public override int Id { get; set; }
32+
}
33+
34+
[Test]
35+
public void TestSerialization()
36+
{
37+
var impl = new PropertyImpl { Id = 1 };
38+
var doc = impl.ToBsonDocument();
39+
var expected = new BsonDocument("_id", 1);
40+
Assert.AreEqual(expected, doc);
41+
}
42+
}
43+
}

MongoDB.BsonUnitTests/MongoDB.BsonUnitTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="Jira\CSharp624Tests.cs" />
9191
<Compile Include="Jira\CSharp637Tests.cs" />
9292
<Compile Include="Jira\CSharp648Tests.cs" />
93+
<Compile Include="Jira\CSharp803Tests.cs" />
9394
<Compile Include="ObjectModel\LazyBsonArrayTests.cs" />
9495
<Compile Include="ObjectModel\LazyBsonDocumentTests.cs" />
9596
<Compile Include="ObjectModel\RawBsonArrayTests.cs" />

0 commit comments

Comments
 (0)