Skip to content

Commit af468f9

Browse files
committed
Return default(K) when requesting a single value from Hits fields
If the hits fields contain no values, then null is returned for the typed array, causing a null reference exception when attempting to access the first value or default. Check for this and return default(K) when array is null
1 parent c72df55 commit af468f9

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Nest/CommonAbstractions/Fields/FieldValues.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@ namespace Nest
1313
[JsonConverter(typeof(FieldValuesJsonConverter))]
1414
public class FieldValues : IsADictionaryBase<string, object>
1515
{
16-
private Inferrer _inferrer;
16+
private readonly Inferrer _inferrer;
1717

18-
public FieldValues(Inferrer inferrer, IDictionary<string, object> container)
18+
public FieldValues(Inferrer inferrer, IDictionary<string, object> container)
1919
: base(container)
2020
{
2121
_inferrer = inferrer;
2222
}
2323

24-
public K Value<K>(Field field) => ValuesOf<K>(field).FirstOrDefault();
24+
public K Value<K>(Field field)
25+
{
26+
var values = ValuesOf<K>(field);
27+
return values != null
28+
? values.FirstOrDefault()
29+
: default(K);
30+
}
2531

26-
public K ValueOf<T, K>(Expression<Func<T, K>> objectPath)
27-
where T : class => Values<T, K>(objectPath).FirstOrDefault();
32+
public K ValueOf<T, K>(Expression<Func<T, K>> objectPath)
33+
where T : class
34+
{
35+
var values = Values(objectPath);
36+
return values != null
37+
? values.FirstOrDefault()
38+
: default(K);
39+
}
2840

2941
public K[] ValuesOf<K>(Field field)
3042
{

0 commit comments

Comments
 (0)