Skip to content
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

Correctly deserialize JoinField #3357

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions src/Tests/Tests.Reproduce/GithubIssue3356.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;
using System.Text;
using Elastic.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Nest.JsonNetSerializer;

namespace Tests.Reproduce
{
public class GithubIssue3356
{
[U]
public void JoinFieldDeserializedCorrectly()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM small suggestion though is to use the recently centralized json serialization assertions.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SerializationTester.DefaultWithSourceSerializer;

Would need to be added though

Copy link
Member

@Mpdreamz Mpdreamz Aug 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could even use only AssertRoundTrip<MyDocument>() rather than AssertSerialize and AssertDeserialize separately

var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection(), JsonNetSerializer.Default)
.DisableDirectStreaming()
.DefaultIndex("docs");
var client = new ElasticClient(connectionSettings);

var doc = new MyDocument
{
Join = JoinField.Root("parent")
};

var response = client.IndexDocument(doc);

Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes).Should().Be("{\"join\":\"parent\"}");
using (var stream = new MemoryStream(response.ApiCall.RequestBodyInBytes))
{
doc = client.SourceSerializer.Deserialize<MyDocument>(stream);
doc.Join.Match(p =>
{
p.Name.Should().Be("parent");
}, c => { });
}
}

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