diff --git a/src/Nest/Aggregations/Bucket/Composite/CompositeBucket.cs b/src/Nest/Aggregations/Bucket/Composite/CompositeBucket.cs index 2eb228e7435..b3e809ec2d5 100644 --- a/src/Nest/Aggregations/Bucket/Composite/CompositeBucket.cs +++ b/src/Nest/Aggregations/Bucket/Composite/CompositeBucket.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Newtonsoft.Json; namespace Nest { @@ -25,6 +26,7 @@ public CompositeBucket(IReadOnlyDictionary dict, CompositeKe /// /// A key for a /// + [JsonConverter(typeof(VerbatimDictionaryKeysPreservingNullJsonConverter))] public class CompositeKey : IsAReadOnlyDictionaryBase { private static readonly DateTimeOffset Epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero); diff --git a/src/Tests/Tests/Framework/SerializationTests/CompositeKeySerializationTests.cs b/src/Tests/Tests/Framework/SerializationTests/CompositeKeySerializationTests.cs new file mode 100644 index 00000000000..565eb995853 --- /dev/null +++ b/src/Tests/Tests/Framework/SerializationTests/CompositeKeySerializationTests.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.IO; +using System.Text; +using Elastic.Xunit.XunitPlumbing; +using Elasticsearch.Net; +using FluentAssertions; +using Nest; +using Tests.Core.Client; +using Tests.Core.Extensions; + +namespace Tests.Framework.SerializationTests +{ + public class CompositeKeySerializationTests + { + [U] public void NullValuesAreSerialized() + { + var compositeKey = new CompositeKey(new Dictionary + { + { "key_1", "value_1" }, + { "key_2", null }, + }); + + var serializer = TestClient.Default.RequestResponseSerializer; + var json = serializer.SerializeToString(compositeKey, SerializationFormatting.None); + json.Should().Be("{\"key_1\":\"value_1\",\"key_2\":null}"); + + using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) + { + stream.Position = 0; + var dictionary = serializer.Deserialize>(stream); + var deserializedCompositeKey = new CompositeKey(dictionary); + compositeKey.Should().Equal(deserializedCompositeKey); + } + } + } +}