Skip to content
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
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Nest
Expand All @@ -7,11 +8,42 @@ internal class TermsIncludeExcludeJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => true;

public override bool CanRead => false;

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
if (reader.TokenType == JsonToken.Null) return null;

var termsInclude = new TermsIncludeExclude();

switch (reader.TokenType)
{
case JsonToken.StartArray:
termsInclude.Values = serializer.Deserialize<IEnumerable<string>>(reader);
break;
case JsonToken.StartObject:
while (reader.TokenType != JsonToken.EndObject)
{
reader.Read();

if (reader.TokenType == JsonToken.PropertyName)
{
var propertyName = (string)reader.Value;
switch (propertyName)
{
case "pattern":
termsInclude.Pattern = reader.ReadAsString();
break;
case "flags":
termsInclude.Flags = reader.ReadAsString();
break;
}
}
}
break;
default:
throw new JsonSerializationException($"Unexpected token {reader.TokenType} when deserializing {nameof(TermsIncludeExclude)}");
}

return termsInclude;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Expand Down
21 changes: 21 additions & 0 deletions src/Tests/Framework/Roundtrip.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Tests.Framework
Expand All @@ -28,6 +31,24 @@ internal RoundTripper(
this._serializerFactory = _serializerFactory;
}

public virtual void DeserializesTo<T>(Action<string, T> assert)
{
var json = (this.ExpectJson is string) ? (string) ExpectJson : JsonConvert.SerializeObject(this.ExpectJson);

T sut;
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
sut = this.Client.Serializer.Deserialize<T>(stream);
sut.Should().NotBeNull();
assert("first deserialization", sut);

var serialized = this.Client.Serializer.SerializeToString(sut);
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(serialized)))
sut = this.Client.Serializer.Deserialize<T>(stream);
sut.Should().NotBeNull();
assert("second deserialization", sut);
}


public virtual RoundTripper<T> WhenSerializing<T>(T actual)
{
var sut = this.AssertSerializesAndRoundTrips(actual);
Expand Down
64 changes: 64 additions & 0 deletions src/Tests/Reproduce/GithubIssue2503.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.IO;
using System.Text;
using FluentAssertions;
using Nest;
using Tests.Framework;
using static Tests.Framework.RoundTripper;

namespace Tests.Reproduce
{
public class GithubIssue2503
{
[U] public void DeserializeTermsIncludeExcludeValues()
{
Expect(new
{
aggs = new
{
sizes = new
{
terms = new
{
field= "size",
size= 20,
include= new [] { "35", "50", "70", "75", "100" }
}
}
}
}).DeserializesTo<SearchRequest>((message, request) =>
{
request.Should().NotBeNull(message);
request.Aggregations.Should().NotBeNull(message).And.HaveCount(1, message);
var termsAggregation = request.Aggregations["sizes"].Terms;
termsAggregation.Should().NotBeNull(message);
termsAggregation.Include.Should().NotBeNull(message);
termsAggregation.Include.Values.Should().NotBeNull(message).And.HaveCount(5, message);
});
}

[U] public void DeserializeTermsIncludeExcludePattern()
{
Expect(@"{
""aggs"": {
""sizes"": {
""terms"": {
""field"": ""size"",
""size"": 20,
""include"": {
""pattern"" : ""\\d+""
}
}
}
}
}").DeserializesTo<SearchRequest>((message, request) =>
{
request.Should().NotBeNull(message);
request.Aggregations.Should().NotBeNull(message).And.HaveCount(1, message);
var termsAggregation = request.Aggregations["sizes"].Terms;
termsAggregation.Should().NotBeNull(message);
termsAggregation.Include.Should().NotBeNull(message);
termsAggregation.Include.Pattern.Should().NotBeNull(message);
});
}
}
}
5 changes: 2 additions & 3 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@
<Compile Include="Reproduce\GithubIssue2173.cs" />
<Compile Include="Reproduce\DateSerialization.cs" />
<Compile Include="Reproduce\GithubIssue2323.cs" />
<Compile Include="Reproduce\GithubIssue2503.cs" />
<Compile Include="Search\Hits\HitsSerializationTests.cs" />
<Compile Include="Search\Search\Rescoring\RescoreUsageTests.cs" />
<Compile Include="Search\Search\InvalidSearchApiTests.cs" />
Expand Down Expand Up @@ -707,9 +708,7 @@
<Content Include="ClientConcepts\LowLevel\pipeline.xml" />
<Content Include="QueryDsl\BoolDsl\hadouken-indentation.jpg" />
</ItemGroup>
<ItemGroup>
<Folder Include="Reproduce" />
</ItemGroup>
<ItemGroup />
<Import Project="..\outputpath.props" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Choose>
Expand Down