From 44e532a255cbbb1fd8f9905807fa035c454c2456 Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Fri, 9 Aug 2019 18:37:07 +1000 Subject: [PATCH] Serialize GeoLocation with internal serializer (#3997) This commit adds GeoLocation to types handled by the internal serializer so that a property on a document POCO of type GeoLocation is serialized by the internal serializer. It's not possible with the current test configuration to test this scenario, because IL rewriting happens after unit tests are run, so the Json.NET constructs used on Nest types are recognised by the JsonNetSerializer. Have added test that _would_ test behaviour, should tests be run after IL-merging in the future. Fixes #3981 (cherry picked from commit 623c724a3205b21ffca9cf88338f94697efef677) --- .../HandleNestTypesOnSourceJsonConverter.cs | 3 +- src/Tests/Tests.Reproduce/GitHubIssue3981.cs | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/Tests/Tests.Reproduce/GitHubIssue3981.cs diff --git a/src/Serializers/Nest.JsonNetSerializer/Converters/HandleNestTypesOnSourceJsonConverter.cs b/src/Serializers/Nest.JsonNetSerializer/Converters/HandleNestTypesOnSourceJsonConverter.cs index cccfc5481cb..7a1d0cef5bc 100644 --- a/src/Serializers/Nest.JsonNetSerializer/Converters/HandleNestTypesOnSourceJsonConverter.cs +++ b/src/Serializers/Nest.JsonNetSerializer/Converters/HandleNestTypesOnSourceJsonConverter.cs @@ -17,7 +17,8 @@ public class HandleNestTypesOnSourceJsonConverter : JsonConverter typeof(Attachment), typeof(ILazyDocument), typeof(LazyDocument), - typeof(GeoCoordinate) + typeof(GeoCoordinate), + typeof(GeoLocation) }; private readonly IElasticsearchSerializer _builtInSerializer; diff --git a/src/Tests/Tests.Reproduce/GitHubIssue3981.cs b/src/Tests/Tests.Reproduce/GitHubIssue3981.cs new file mode 100644 index 00000000000..c811ad21f93 --- /dev/null +++ b/src/Tests/Tests.Reproduce/GitHubIssue3981.cs @@ -0,0 +1,31 @@ +using System.Text; +using Elastic.Xunit.XunitPlumbing; +using FluentAssertions; +using Nest; +using Tests.Core.Client; + +namespace Tests.Reproduce +{ + public class GitHubIssue3981 + { + // This test always passes because the JsonPropertyAttribute on + // the GeoLocation type are recognized by the JsonNetSerializer when used in tests, because the + // IL rewriting to internalize Json.NET in the client happens *after* tests are run. + [U] + public void JsonNetSerializerSerializesGeoLocation() + { + var document = new Document + { + Location = new GeoLocation(45, 45) + }; + + var indexResponse = TestClient.InMemoryWithJsonNetSerializer.IndexDocument(document); + Encoding.UTF8.GetString(indexResponse.ApiCall.RequestBodyInBytes).Should().Contain("\"lat\"").And.Contain("\"lon\""); + } + + private class Document + { + public GeoLocation Location { get; set; } + } + } +}