Skip to content

Commit 8271db0

Browse files
committed
found a bug on propertypaths on IDictionary<,> where Dictionary<,> worked fine
1 parent e08e6d5 commit 8271db0

File tree

2 files changed

+166
-17
lines changed

2 files changed

+166
-17
lines changed

src/Nest/Resolvers/PropertyNameResolver.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ protected override Expression VisitMethodCall(MethodCallExpression m, Stack<stri
142142
}
143143
else if (m.Method.Name == "get_Item" && m.Arguments.Any())
144144
{
145-
if (!typeof(IDictionary).IsAssignableFrom(m.Object.Type))
145+
var t = m.Object.Type;
146+
var isDict =
147+
typeof(IDictionary).IsAssignableFrom(t)
148+
|| typeof(IDictionary<,>).IsAssignableFrom(t)
149+
|| (t.IsGenericType && t.GetGenericTypeDefinition() == typeof (IDictionary<,>));
150+
151+
if (!isDict)
146152
{
147153
return base.VisitMethodCall(m, stack, properties);
148154
}
Lines changed: 159 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,172 @@
1-
using NUnit.Framework;
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
6+
using FluentAssertions;
7+
using Nest.Resolvers;
8+
using NUnit.Framework;
29
using Nest.Tests.MockData.Domain;
310

411
namespace Nest.Tests.Unit.Internals.Inferno
512
{
613
[TestFixture]
714
public class PropertyVisitorTests
815
{
16+
private class CustomDict : Dictionary<string, ElasticsearchProject> { }
17+
18+
private class DomainObject
19+
{
20+
public string Name { get; set; }
21+
public IDictionary<string, ElasticsearchProject> Dictionary { get; set; }
22+
public CustomDict CustomDict { get; set; }
23+
public IList<ElasticsearchProject> Collection { get; set; }
24+
25+
}
26+
27+
private readonly ElasticInferrer _infer;
28+
private readonly string _variable = "vari";
29+
30+
public PropertyVisitorTests()
31+
{
32+
_infer = TestElasticClient.Client.Infer;
33+
}
34+
35+
private string P(Expression<Func<DomainObject, object>> path)
36+
{
37+
return this._infer.PropertyPath(Property.Path(path));
38+
}
39+
40+
[Test]
41+
public void SimpleProperty()
42+
{
43+
P(p => p.Name).Should().Be("name");
44+
}
45+
46+
[Test]
47+
public void SuffixOnPropery()
48+
{
49+
P(p => p.Name.Suffix("sort")).Should().Be("name.sort");
50+
}
51+
52+
[Test]
53+
public void IndexOnCollection()
54+
{
55+
P(p => p.Collection[0]).Should().Be("collection");
56+
}
57+
58+
[Test]
59+
public void IndexOnCollectionProperty()
60+
{
61+
P(p => p.Collection[0].Name).Should().Be("collection.name");
62+
}
63+
64+
[Test]
65+
public void FirstOnCollection()
66+
{
67+
P(p => p.Collection.First()).Should().Be("collection");
68+
}
69+
70+
[Test]
71+
public void FirstOnCollectionProperty()
72+
{
73+
P(p => p.Collection.First().Name).Should().Be("collection.name");
74+
}
75+
76+
[Test]
77+
public void Dictionary()
78+
{
79+
P(p => p.Dictionary["hardcoded"]).Should().Be("dictionary.hardcoded");
80+
}
81+
82+
[Test]
83+
public void DictionaryPropery()
84+
{
85+
P(p => p.Dictionary["hardcoded"].Name).Should().Be("dictionary.hardcoded.name");
86+
}
87+
88+
//Test variables
89+
[Test]
90+
public void DictionaryVariableKey()
91+
{
92+
P(p => p.Dictionary[_variable]).Should().Be("dictionary.vari");
93+
}
94+
95+
[Test]
96+
public void DictionaryVariableKeyProperty()
97+
{
98+
P(p => p.Dictionary[_variable].Name).Should().Be("dictionary.vari.name");
99+
}
100+
101+
[Test]
102+
public void CustomDictionary()
103+
{
104+
P(p => p.CustomDict["hardcoded"]).Should().Be("customDict.hardcoded");
105+
}
106+
107+
[Test]
108+
public void CustomDictionaryPropery()
109+
{
110+
P(p => p.CustomDict["hardcoded"].Name).Should().Be("customDict.hardcoded.name");
111+
}
112+
113+
//Test variables
114+
115+
[Test]
116+
public void CustomDictionaryVariableKey()
117+
{
118+
P(p => p.CustomDict[_variable]).Should().Be("customDict.vari");
119+
}
120+
121+
[Test]
122+
public void CustomDictionaryVariableKeyProperty()
123+
{
124+
P(p => p.CustomDict[_variable].Name).Should().Be("customDict.vari.name");
125+
}
126+
127+
//Test suffixes
128+
[Test]
129+
public void PropertySuffix()
130+
{
131+
P(p => p.Name.Suffix("suffix")).Should().Be("name.suffix");
132+
}
133+
134+
[Test]
135+
public void DictionarySuffix()
136+
{
137+
P(p => p.Dictionary["hardcoded"].Suffix("suffix")).Should().Be("dictionary.hardcoded.suffix");
138+
}
139+
140+
[Test]
141+
public void FirstOnCollectionSuffix()
142+
{
143+
P(p => p.Collection.First().Suffix("suffix")).Should().Be("collection.suffix");
144+
}
145+
9146
[Test]
10-
public void SuffixMakesItIntoPropertyName()
147+
public void IndexOnCollectionSuffix()
11148
{
12-
var s = new SearchDescriptor<ElasticsearchProject>()
13-
.From(0)
14-
.Size(10)
15-
.Query(q => q.Term(f => f.Name.Suffix("sort"), "value"));
16-
var json = TestElasticClient.Serialize(s);
17-
var expected = @"{ from: 0, size: 10,
18-
query: {
19-
term: {
20-
""name.sort"": {
21-
value: ""value""
22-
}
23-
}
149+
P(p => p.Collection[0].Suffix("suffix")).Should().Be("collection.suffix");
24150
}
25-
}";
26-
Assert.True(json.JsonEquals(expected), json);
151+
152+
[Test]
153+
public void CollectionSuffix()
154+
{
155+
P(p => p.Collection.Suffix("suffix")).Should().Be("collection.suffix");
27156
}
157+
158+
159+
160+
161+
162+
163+
164+
165+
166+
167+
168+
169+
170+
28171
}
29172
}

0 commit comments

Comments
 (0)