Skip to content

Commit 788a8e3

Browse files
committed
Dictionary lookups in property expressions now end up in the stringified paths, but collection lookups do NOT end up in the path by design. fix #324
1 parent c967b3b commit 788a8e3

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/Nest.Tests.Unit/Internals/Inferno/PropertyNameResolverTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using NUnit.Framework;
34
using System.Linq.Expressions;
45
using Nest.Resolvers;
@@ -12,6 +13,9 @@ public class PropertyNameResolverTests : BaseJsonTests
1213
internal class SomeClass
1314
{
1415
public MyCustomClass MyCustomClass { get; set; }
16+
public Dictionary<string, SomeOtherClass> StringDict { get; set; }
17+
public Dictionary<int, MyCustomClass> IntDict { get; set; }
18+
public IList<MyCustomClass> ListOfCustomClasses { get; set; }
1519
}
1620
[ElasticType(IdProperty = "Guid")]
1721
internal class SomeOtherClass
@@ -44,6 +48,7 @@ public override string ToString()
4448
return "static id ftw";
4549
}
4650
}
51+
4752
[Test]
4853
public void TestUsesElasticProperty()
4954
{
@@ -52,6 +57,7 @@ public void TestUsesElasticProperty()
5257
var expected = "myCustomClass.MID";
5358
Assert.AreEqual(expected, propertyName);
5459
}
60+
5561
[Test]
5662
public void TestUsesOtherElasticProperty()
5763
{
@@ -60,6 +66,7 @@ public void TestUsesOtherElasticProperty()
6066
var expected = "custom.MID";
6167
Assert.AreEqual(expected, propertyName);
6268
}
69+
6370
[Test]
6471
public void TestUsesOtherElasticTypePropertyIsIgnored()
6572
{
@@ -68,6 +75,7 @@ public void TestUsesOtherElasticTypePropertyIsIgnored()
6875
var expected = "myCustomOtherClass.MID";
6976
Assert.AreEqual(expected, propertyName);
7077
}
78+
7179
[Test]
7280
public void TestCreatedDate()
7381
{
@@ -76,6 +84,34 @@ public void TestCreatedDate()
7684
var expected = "CreateDate";
7785
Assert.AreEqual(expected, propertyName);
7886
}
87+
88+
[Test]
89+
public void TestDictionaryStringExpression()
90+
{
91+
Expression<Func<SomeClass, object>> exp = (m) => m.StringDict["someValue"].CreateDate;
92+
var propertyName = new PropertyNameResolver().Resolve(exp);
93+
var expected = "stringDict.someValue.CreateDate";
94+
Assert.AreEqual(expected, propertyName);
95+
}
96+
97+
[Test]
98+
public void TestDictionaryIntExpression()
99+
{
100+
Expression<Func<SomeClass, object>> exp = (m) => m.IntDict[101].MyProperty;
101+
var propertyName = new PropertyNameResolver().Resolve(exp);
102+
var expected = "intDict.101.MID";
103+
Assert.AreEqual(expected, propertyName);
104+
}
105+
106+
[Test]
107+
public void TestCollectionIndexExpressionDoesNotEndUpInPath()
108+
{
109+
Expression<Func<SomeClass, object>> exp = (m) => m.ListOfCustomClasses[101].MyProperty;
110+
var propertyName = new PropertyNameResolver().Resolve(exp);
111+
var expected = "listOfCustomClasses.MID";
112+
Assert.AreEqual(expected, propertyName);
113+
}
114+
79115
[Test]
80116
public void SearchUsesTheProperResolver()
81117
{

src/Nest/Resolvers/PropertyNameResolver.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -137,6 +138,16 @@ protected override Expression VisitMethodCall(MethodCallExpression m, Stack<stri
137138
if (constantExpression != null)
138139
stack.Push(constantExpression.Value as string);
139140
}
141+
else if (m.Method.Name == "get_Item" && m.Arguments.Any())
142+
{
143+
if (!typeof(IDictionary).IsAssignableFrom(m.Object.Type))
144+
{
145+
return base.VisitMethodCall(m, stack, properties);
146+
}
147+
var constantExpression = m.Arguments.Last() as ConstantExpression;
148+
if (constantExpression != null)
149+
stack.Push(constantExpression.Value.ToString());
150+
}
140151
if (IsLinqOperator(m.Method))
141152
{
142153
for (int i = 1; i < m.Arguments.Count; i++)

0 commit comments

Comments
 (0)