Skip to content

Correctly deserialize JoinField #3357

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 2 commits into from
Aug 14, 2018
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 @@ -45,7 +45,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
//even though we pass type JSON.NET won't try the registered converter for that type
//even if it can handle string tokens :(
if (objectType == typeof(JoinField) && token.Type == JTokenType.String)
return JoinField.Root(token.ToString(Formatting.None));
return JoinField.Root(token.Value<string>());

using (var ms = token.ToStream())
return _builtInSerializer.Deserialize(objectType, ms);
Expand Down
3 changes: 3 additions & 0 deletions src/Tests/Tests.Core/Client/TestClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nest;
using Nest.JsonNetSerializer;
using Tests.Configuration;
using Tests.Core.Client.Settings;
using Tests.Domain.Extensions;
Expand All @@ -9,6 +10,8 @@ public static class TestClient
{
public static readonly IElasticClient Default = new ElasticClient(new TestConnectionSettings().ApplyDomainSettings());
public static readonly IElasticClient DefaultInMemoryClient = new ElasticClient(new AlwaysInMemoryConnectionSettings().ApplyDomainSettings());
public static readonly IElasticClient InMemoryWithJsonNetSerializer = new ElasticClient(
new AlwaysInMemoryConnectionSettings(sourceSerializerFactory: JsonNetSerializer.Default).ApplyDomainSettings());
public static readonly IElasticClient DisabledStreaming = new ElasticClient(new TestConnectionSettings().ApplyDomainSettings().DisableDirectStreaming());

public static readonly ITestConfiguration Configuration = TestConfiguration.Instance;
Expand Down
3 changes: 3 additions & 0 deletions src/Tests/Tests.Core/Serialization/SerializationTester.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Elasticsearch.Net;
using FluentAssertions;
Expand Down Expand Up @@ -64,6 +65,8 @@ public class SerializationTester
{
public static SerializationTester Default { get; } = new SerializationTester(TestClient.DefaultInMemoryClient);

public static SerializationTester DefaultWithJsonNetSerializer { get; } = new SerializationTester(TestClient.InMemoryWithJsonNetSerializer);

public SerializationTester(IElasticClient client) => this.Client = client;

public IElasticClient Client { get; }
Expand Down
36 changes: 36 additions & 0 deletions src/Tests/Tests.Reproduce/GithubIssue3356.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Elastic.Xunit.XunitPlumbing;
using FluentAssertions;
using Nest;
using Tests.Core.Extensions;
using Tests.Core.Serialization;

namespace Tests.Reproduce
{
public class GithubIssue3356
{
[U]
public void JoinFieldDeserializedCorrectly()
{
var doc = new MyDocument
{
Join = JoinField.Root("parent")
};

var tester = SerializationTester.DefaultWithJsonNetSerializer;
var response = tester.Client.IndexDocument(doc);

tester.AssertSerialize(response.ApiCall.RequestBodyInBytes, new { join = "parent" });
doc = tester.AssertDeserialize<MyDocument>(response.ApiCall.RequestBodyInBytes);

doc.Join.Match(
p => { p.Name.Should().Be("parent"); },
c => throw new InvalidOperationException("should not be called"));
}

private class MyDocument
{
public JoinField Join { get; set; }
}
}
}