Skip to content

Commit 9b52e0a

Browse files
committed
Merge pull request #1321 from elastic/fix/1317
Fix #1317: FieldValues shouldn't throw when no values are present
2 parents a796f8e + eb11237 commit 9b52e0a

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/Nest/Domain/Paths/FieldSelection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ public K[] FieldValues<K>(Expression<Func<T, K>> objectPath)
7070
/// <typeparam name="K">The type to return the value as, remember that if your field is a string K should be string[]</typeparam>
7171
private K FieldArray<K>(string path)
7272
{
73+
var fieldValues = ((IFieldSelection<T>)this).FieldValuesDictionary;
7374
object o;
74-
if (((IFieldSelection<T>)this).FieldValuesDictionary.TryGetValue(path, out o))
75+
if (fieldValues != null && fieldValues.TryGetValue(path, out o))
7576
{
7677
var t = typeof(K);
7778
if (o is JArray && t.GetInterfaces().Contains(typeof(IEnumerable)))

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
<Compile Include="Reproduce\Reproduce1236Tests.cs" />
176176
<Compile Include="Reproduce\Reproduce1278Tests.cs" />
177177
<Compile Include="Reproduce\Reproduce1304Tests.cs" />
178+
<Compile Include="Reproduce\Reproduce1317Tests.cs" />
178179
<Compile Include="Reproduce\Reproduce769Tests.cs" />
179180
<Compile Include="Reproduce\Reproduce945Tests.cs" />
180181
<Compile Include="Reproduce\Reproduce953Tests.cs" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
using NUnit.Framework;
8+
using Nest.Tests.MockData.Domain;
9+
10+
namespace Nest.Tests.Integration.Reproduce
11+
{
12+
[TestFixture]
13+
public class Reproduce1317Tests : IntegrationTests
14+
{
15+
[Test]
16+
public void FieldValuesShouldNotThrowOnNonExistentFields()
17+
{
18+
// With no field values returned
19+
var result = this.Client.Get<ElasticsearchProject>(g => g
20+
.Id(4)
21+
.Fields("fieldthatdoesntexist")
22+
);
23+
24+
result.Fields.FieldValues<string[]>("fieldthatdoesntexist").Should().BeNull();
25+
26+
// With field values returned
27+
result = this.Client.Get<ElasticsearchProject>(g => g
28+
.Id(4)
29+
.Fields("fieldthatdoesntexist", "name")
30+
);
31+
32+
result.Fields.FieldValues<string[]>("name").Count().Should().BeGreaterThan(0);
33+
result.Fields.FieldValues<string[]>("fieldthatdoesntexist").Should().BeNull();
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)