Skip to content

Commit 9e467cd

Browse files
author
rstam
committed
Fixed CSHARP-418. The GetMemberSerializationInfo method of BsonClassMapSerializer was not working if the memberName was for an inherited member.
1 parent bf261da commit 9e467cd

File tree

3 files changed

+101
-6
lines changed

3 files changed

+101
-6
lines changed

Bson/Serialization/BsonClassMapSerializer.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,26 @@ public BsonSerializationInfo GetItemSerializationInfo()
249249
/// <returns>The serialization info for the member.</returns>
250250
public BsonSerializationInfo GetMemberSerializationInfo(string memberName)
251251
{
252-
var memberMap = _classMap.GetMemberMap(memberName);
253-
var elementName = memberMap.ElementName;
254-
var serializer = memberMap.GetSerializer(memberMap.MemberType);
255-
var nominalType = memberMap.MemberType;
256-
var serializationOptions = memberMap.SerializationOptions;
257-
return new BsonSerializationInfo(elementName, serializer, nominalType, serializationOptions);
252+
var classMap = _classMap;
253+
while (classMap != null)
254+
{
255+
var memberMap = classMap.GetMemberMap(memberName);
256+
if (memberMap != null)
257+
{
258+
var elementName = memberMap.ElementName;
259+
var serializer = memberMap.GetSerializer(memberMap.MemberType);
260+
var nominalType = memberMap.MemberType;
261+
var serializationOptions = memberMap.SerializationOptions;
262+
return new BsonSerializationInfo(elementName, serializer, nominalType, serializationOptions);
263+
}
264+
classMap = classMap.BaseClassMap;
265+
}
266+
267+
var message = string.Format(
268+
"Class {0} does not have a member called {1}.",
269+
BsonUtils.GetFriendlyTypeName(_classMap.ClassType),
270+
memberName);
271+
throw new ArgumentOutOfRangeException("memberName", message);
258272
}
259273

260274
/// <summary>

DriverOnlineTests/DriverOnlineTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<Compile Include="Jira\CSharp361Tests.cs" />
127127
<Compile Include="Jira\CSharp365Tests.cs" />
128128
<Compile Include="Jira\CSharp378Tests.cs" />
129+
<Compile Include="Jira\CSharp418Tests.cs" />
129130
<Compile Include="Jira\CSharp77Tests.cs" />
130131
<Compile Include="Jira\CSharp92Tests.cs" />
131132
<Compile Include="Jira\CSharp93Tests.cs" />
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
using NUnit.Framework;
21+
22+
using MongoDB.Bson;
23+
using MongoDB.Driver;
24+
using MongoDB.Driver.Linq;
25+
26+
namespace MongoDB.DriverOnlineTests.Jira.CSharp418
27+
{
28+
[TestFixture]
29+
public class CSharp418Tests
30+
{
31+
public class C
32+
{
33+
public ObjectId Id;
34+
public int X;
35+
}
36+
37+
public class D : C
38+
{
39+
public int Y;
40+
}
41+
42+
private MongoServer _server;
43+
private MongoDatabase _database;
44+
private MongoCollection<D> _collection;
45+
46+
[TestFixtureSetUp]
47+
public void TestFixtureSetup()
48+
{
49+
_server = Configuration.TestServer;
50+
_database = Configuration.TestDatabase;
51+
_collection = Configuration.GetTestCollection<D>();
52+
}
53+
54+
[Test]
55+
public void TestQueryAgainstInheritedField()
56+
{
57+
_collection.Drop();
58+
_collection.Insert(new D { X = 1, Y = 2 });
59+
60+
var query = from d in _collection.AsQueryable<D>()
61+
where d.X == 1
62+
select d;
63+
64+
var translatedQuery = MongoQueryTranslator.Translate(query);
65+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
66+
Assert.AreSame(_collection, translatedQuery.Collection);
67+
Assert.AreSame(typeof(D), translatedQuery.DocumentType);
68+
69+
var selectQuery = (SelectQuery)translatedQuery;
70+
Assert.AreEqual("(D d) => (d.X == 1)", ExpressionFormatter.ToString(selectQuery.Where));
71+
Assert.IsNull(selectQuery.OrderBy);
72+
Assert.IsNull(selectQuery.Projection);
73+
Assert.IsNull(selectQuery.Skip);
74+
Assert.IsNull(selectQuery.Take);
75+
76+
Assert.AreEqual("{ \"X\" : 1 }", selectQuery.BuildQuery().ToJson());
77+
Assert.AreEqual(1, query.ToList().Count);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)