Skip to content

Commit 7f57b3d

Browse files
committed
CSHARP-598: corrected issue with not evaluating member expressions that contain variables and not constants.
1 parent 0be5ae3 commit 7f57b3d

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

Driver/Builders/UpdateBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using MongoDB.Bson.Serialization;
2525
using MongoDB.Driver.Linq.Utils;
2626
using MongoDB.Driver.Wrappers;
27+
using MongoDB.Driver.Linq;
2728

2829
namespace MongoDB.Driver.Builders
2930
{

Driver/Linq/Utils/BsonSerializationInfoHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public BsonSerializationInfoHelper()
4545
/// <returns>The serialization info.</returns>
4646
public BsonSerializationInfo GetSerializationInfo(Expression node)
4747
{
48-
return BsonSerializationInfoFinder.GetSerializationInfo(node, _serializationInfoCache);
48+
var evaluatedNode = PartialEvaluator.Evaluate(node);
49+
return BsonSerializationInfoFinder.GetSerializationInfo(evaluatedNode, _serializationInfoCache);
4950
}
5051

5152
/// <summary>

DriverUnitTests/DriverUnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Compile Include="GridFS\MongoGridFSSettingsTests.cs" />
129129
<Compile Include="GridFS\MongoGridFSStreamTests.cs" />
130130
<Compile Include="GridFS\MongoGridFSTests.cs" />
131+
<Compile Include="Jira\CSharp598Tests.cs" />
131132
<Compile Include="Jira\CSharp538Tests.cs" />
132133
<Compile Include="Jira\CSharp532Tests.cs" />
133134
<Compile Include="Jira\CSharp475Tests.cs" />
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
using MongoDB.Bson.Serialization.Attributes;
26+
using MongoDB.Driver.Builders;
27+
28+
namespace MongoDB.DriverUnitTests.Jira.CSharp598
29+
{
30+
[TestFixture]
31+
public class CSharp598Tests
32+
{
33+
[Test]
34+
public void TestVariableWorksForQuery()
35+
{
36+
int index = 10;
37+
IMongoQuery query = null;
38+
Assert.DoesNotThrow(() =>
39+
{
40+
query = Query<TestClass>.EQ(x => x.List[index].Name, "Blah");
41+
});
42+
43+
Assert.AreEqual("{ \"List.10.Name\" : \"Blah\" }", query.ToString());
44+
}
45+
46+
[Test]
47+
public void TestVariableWorksForUpdate()
48+
{
49+
int index = 10;
50+
IMongoUpdate update = null;
51+
Assert.DoesNotThrow(() =>
52+
{
53+
update = Update<TestClass>.Set(x => x.List[index].Name, "Blah");
54+
});
55+
56+
Assert.AreEqual("{ \"$set\" : { \"List.10.Name\" : \"Blah\" } }", update.ToString());
57+
}
58+
59+
[Test]
60+
public void TestVariableWorksForQueryWithVariableChange()
61+
{
62+
int index = 10;
63+
IMongoQuery query = null;
64+
var queryBuilder = new QueryBuilder<TestClass>();
65+
Assert.DoesNotThrow(() =>
66+
{
67+
query = queryBuilder.EQ(x => x.List[index].Name, "Blah");
68+
index = 11;
69+
query = queryBuilder.EQ(x => x.List[index].Name, "Blah");
70+
});
71+
72+
Assert.AreEqual("{ \"List.11.Name\" : \"Blah\" }", query.ToString());
73+
}
74+
75+
private class TestClass
76+
{
77+
public ObjectId Id { get; set; }
78+
79+
public List<SubTestClass> List { get; set; }
80+
}
81+
private class SubTestClass
82+
{
83+
public string Name { get; set; }
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)