Skip to content

Fix for covariant search date parsing #2643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ private static void PopulateHit(JsonSerializer serializer, JsonReader reader, ob

private static JObject CreateIntermediateJObject(JsonReader reader)
{
var original = reader.DateParseHandling;
// Temporarily turn off DateTime parsing since we deserialize
// to a dynamic object and the date could be stored as a string
// in the users POCO and we need to preserve its format. This is
// side-effect free since we read the reader to completion using
// JObject.Load before handing off a new one to the deserialized
// hit object.
reader.DateParseHandling = DateParseHandling.None;
var jObject = JObject.Load(reader);
reader.DateParseHandling = original;
return jObject;
}

Expand Down
56 changes: 56 additions & 0 deletions src/Tests/Reproduce/CovariantSearchDateParsing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elasticsearch.Net;
using Nest;
using FluentAssertions;
using Newtonsoft.Json;
using Tests.Framework;
using Tests.Framework.ManagedElasticsearch.Clusters;
using Xunit;

namespace Tests.Reproduce
{
public class CovariantSearchDateParsing : IClusterFixture<WritableCluster>
{
public interface ISearchResult
{
DateTime Date { get; set; }
string DateAsString { get; set; }
}

public class SearchResult : ISearchResult
{
public DateTime Date { get; set; }
public string DateAsString { get; set; }
}

private readonly WritableCluster _cluster;

public CovariantSearchDateParsing(WritableCluster cluster)
{
_cluster = cluster;
}

[I] public void CovariantSearchDateParsingTest()
{
var client = _cluster.Client;
var index = "convariantsearchconverstiontest";
if (client.IndexExists(index).Exists)
client.DeleteIndex(index);

var date = "2013-06-24T00:00:00.000";
var indexResult = client.Index(new SearchResult { DateAsString = date, Date = DateTime.Parse(date) }, i => i.Id(1).Index(index));
indexResult.IsValid.Should().BeTrue();
client.Refresh(Nest.Indices.All);
var response = client.Search<ISearchResult>(new SearchRequest<ISearchResult>(index, typeof(SearchResult)));
response.IsValid.Should().BeTrue();
response.Documents.Count.Should().Be(1);
var document = response.Documents.SingleOrDefault();
document.Should().NotBeNull();
document.DateAsString.Should().Be(date);
document.Date.ShouldBeEquivalentTo(DateTime.Parse(date));
}
}
}
1 change: 1 addition & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@
<Compile Include="QueryDsl\NotConditionlessWhen.cs" />
<Compile Include="QueryDsl\Specialized\Percolate\PercolateQueryUsageTests.cs" />
<Compile Include="Reproduce\ConnectionReuseAndBalancing.cs" />
<Compile Include="Reproduce\CovariantSearchDateParsing.cs" />
<Compile Include="Reproduce\DateSerialization.cs" />
<Compile Include="Reproduce\GithubIssue2152.cs" />
<Compile Include="Reproduce\GithubIssue2306.cs" />
Expand Down