File tree Expand file tree Collapse file tree 3 files changed +62
-2
lines changed
MongoDB.Bson/Serialization/Conventions Expand file tree Collapse file tree 3 files changed +62
-2
lines changed Original file line number Diff line number Diff 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 number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" />
You can’t perform that action at this time.
0 commit comments