Skip to content

Commit 8e8cae8

Browse files
committed
Handle a null string implicit conversion to DateMath
Use null conditional operator when attempting to cast terms params to object Closes #1766
1 parent 8427ba0 commit 8e8cae8

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

Diff for: src/Nest/CommonOptions/DateMath/DateMath.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ protected DateMath() { }
3232
public static DateMathExpression Anchored(DateTime anchor) => new DateMathExpression(anchor);
3333
public static DateMathExpression Anchored(string dateAnchor) => new DateMathExpression(dateAnchor);
3434

35-
private static Regex _dateMathRe =
35+
private static readonly Regex _dateMathRe =
3636
new Regex(@"^(?<anchor>now|.+(?:\|\||$))(?<ranges>(?:(?:\+|\-)[^\/]*))?(?<rounding>\/(?:y|M|w|d|h|m|s))?$");
3737

3838
public static implicit operator DateMath(DateTime dateTime) => DateMath.Anchored(dateTime);
3939
public static implicit operator DateMath(string dateMath) => DateMath.FromString(dateMath);
4040

4141
public static DateMath FromString(string dateMath)
4242
{
43+
if (dateMath == null) return null;
44+
4345
var match = _dateMathRe.Match(dateMath);
44-
if (!match.Success) throw new ArgumentException($"Can not create a DateMathExpression out of '{dateMath}'");
46+
if (!match.Success) throw new ArgumentException($"Cannot create a DateMathExpression out of '{dateMath}'");
4547

4648
var math = new DateMathExpression(match.Groups["anchor"].Value);
4749

Diff for: src/Nest/QueryDsl/TermLevel/Terms/TermsQuery.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ internal static bool IsConditionless(ITermsQuery q)
2828
{
2929
return q.Field.IsConditionless()
3030
|| (
31-
(!q.Terms.HasAny() || q.Terms.All(t=>t == null || ((t as string)?.IsNullOrEmpty()).GetValueOrDefault(false))
31+
(q.Terms == null
32+
|| !q.Terms.HasAny()
33+
|| q.Terms.All(t=>t == null
34+
|| ((t as string)?.IsNullOrEmpty()).GetValueOrDefault(false))
3235
)
3336
&&
3437
(q.TermsLookup == null
@@ -63,9 +66,9 @@ public TermsQueryDescriptor<T, TValue> TermsLookup<TOther>(Func<FieldLookupDescr
6366

6467
public TermsQueryDescriptor<T, TValue> DisableCoord(bool? disable = true) => Assign(a => a.DisableCoord = disable);
6568

66-
public TermsQueryDescriptor<T, TValue> Terms(IEnumerable<TValue> terms) => Assign(a => a.Terms = terms.Cast<object>());
69+
public TermsQueryDescriptor<T, TValue> Terms(IEnumerable<TValue> terms) => Assign(a => a.Terms = terms?.Cast<object>());
6770

68-
public TermsQueryDescriptor<T, TValue> Terms(params TValue[] terms) => Assign(a => a.Terms = terms.Cast<object>());
71+
public TermsQueryDescriptor<T, TValue> Terms(params TValue[] terms) => Assign(a => a.Terms = terms?.Cast<object>());
6972

7073
}
7174
}

Diff for: src/Tests/CommonOptions/DateMath/DateMathTests.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using FluentAssertions;
3+
using Tests.Framework;
4+
5+
namespace Tests.CommonOptions.DateMath
6+
{
7+
public class DateMathTests
8+
{
9+
[U]
10+
public void ImplicitConversionFromNullString()
11+
{
12+
string nullString = null;
13+
Nest.DateMath dateMath = nullString;
14+
dateMath.Should().BeNull();
15+
}
16+
17+
[U]
18+
public void ImplicitConversionFromNullNullableDateTime()
19+
{
20+
DateTime? nullableDateTime = null;
21+
Nest.DateMath dateMath = nullableDateTime;
22+
dateMath.Should().BeNull();
23+
}
24+
25+
[U]
26+
public void ImplicitConversionFromDateMathString()
27+
{
28+
string nullString = "now+3d";
29+
Nest.DateMath dateMath = nullString;
30+
dateMath.Should().NotBeNull();
31+
}
32+
33+
[U]
34+
public void ImplicitConversionFromNullableDateTimeWithValue()
35+
{
36+
DateTime? nullableDateTime = DateTime.Now;
37+
Nest.DateMath dateMath = nullableDateTime;
38+
dateMath.Should().NotBeNull();
39+
}
40+
}
41+
}

Diff for: src/Tests/Document/Single/Index/IndexApiTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ public void Index()
194194
{
195195
var anonymousType = new
196196
{
197-
id = "id",
198197
name = "name",
199198
value = 3,
200199
date = new DateTime(2016, 1, 1),
@@ -206,7 +205,7 @@ public void Index()
206205
};
207206

208207
var indexResult = this.Client.Index(anonymousType, f => f
209-
.Id(anonymousType.id)
208+
.Id(anonymousType.name)
210209
);
211210

212211
indexResult.IsValid.Should().BeTrue();

0 commit comments

Comments
 (0)