Skip to content

Commit 53faea4

Browse files
russcamMpdreamz
authored andcommitted
DateMath has ToString() value for DateTime.MinValue (#4278)
This commit updates the ToString() implementation of DateMath to emit a value when DateTime.MinValue is passed. Update IsValid property to allow any DateTime to be valid. Fixes #4228
1 parent b867fd6 commit 53faea4

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/Nest/CommonOptions/DateMath/DateMath.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static DateMath FromString(string dateMath)
8383

8484
internal static bool IsValidDateMathString(string dateMath) => dateMath != null && DateMathRegex.IsMatch(dateMath);
8585

86-
internal bool IsValid => Self.Anchor.Match(d => d != default, s => !s.IsNullOrEmpty());
86+
internal bool IsValid => Self.Anchor.Match(_ => true, s => !s.IsNullOrEmpty());
8787

8888
public override string ToString()
8989
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Elastic.Xunit.XunitPlumbing;
5+
using FluentAssertions;
6+
using Nest;
7+
using Tests.Core.Client;
8+
using Tests.Core.Extensions;
9+
using Tests.Core.Serialization;
10+
using Tests.Domain;
11+
12+
namespace Tests.Reproduce
13+
{
14+
public class GithubIssue4228
15+
{
16+
[U]
17+
public void CanSerializeDateMathDateTimeMinValue() {
18+
19+
var searchResponse = TestClient.DefaultInMemoryClient.Search<object>(s => s
20+
.AllIndices()
21+
.Query(q => q
22+
.DateRange(d => d
23+
.GreaterThanOrEquals(DateTime.MinValue)
24+
.Field("date")
25+
)
26+
)
27+
);
28+
29+
Encoding.UTF8.GetString(searchResponse.ApiCall.RequestBodyInBytes)
30+
.Should().Be("{\"query\":{\"range\":{\"date\":{\"gte\":\"0001-01-01T00:00:00\"}}}}");
31+
}
32+
}
33+
}

src/Tests/Tests/CommonOptions/DateMath/DateMathTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public void ImplicitConversionFromNullNullableDateTime()
2323
}
2424

2525
[U] // F# backticks would be great in C# :)
26-
public void ImplicitConversionFromDefaultDateTimeIsNotNullButEmptyString()
26+
public void ImplicitConversionFromDefaultDateTimeIsMinValue()
2727
{
2828
// in 6.x DateMath is backed by a DateTime instance
2929
// for 7.x we will adress this
3030
DateTime nullableDateTime = default;
3131
Nest.DateMath dateMath = nullableDateTime;
3232
dateMath.Should().NotBeNull();
33-
dateMath.ToString().Should().BeEmpty();
33+
dateMath.ToString().Should().Be("0001-01-01T00:00:00");
3434
}
3535

3636
[U]

0 commit comments

Comments
 (0)