Skip to content

Commit 66d59f3

Browse files
committed
Serialize null values in composite aggregation key (#3605)
Fixes #3575
1 parent 3853ed4 commit 66d59f3

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Nest/Aggregations/Bucket/Composite/CompositeBucket.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Newtonsoft.Json;
34

45
namespace Nest
56
{
@@ -25,6 +26,7 @@ public CompositeBucket(IReadOnlyDictionary<string, IAggregate> dict, CompositeKe
2526
/// <summary>
2627
/// A key for a <see cref="CompositeBucket" />
2728
/// </summary>
29+
[JsonConverter(typeof(VerbatimDictionaryKeysPreservingNullJsonConverter<string, object>))]
2830
public class CompositeKey : IsAReadOnlyDictionaryBase<string, object>
2931
{
3032
private static readonly DateTimeOffset Epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
using Elastic.Xunit.XunitPlumbing;
5+
using Elasticsearch.Net;
6+
using FluentAssertions;
7+
using Nest;
8+
using Tests.Core.Client;
9+
using Tests.Core.Extensions;
10+
11+
namespace Tests.Framework.SerializationTests
12+
{
13+
public class CompositeKeySerializationTests
14+
{
15+
[U] public void NullValuesAreSerialized()
16+
{
17+
var compositeKey = new CompositeKey(new Dictionary<string, object>
18+
{
19+
{ "key_1", "value_1" },
20+
{ "key_2", null },
21+
});
22+
23+
var serializer = TestClient.Default.RequestResponseSerializer;
24+
var json = serializer.SerializeToString(compositeKey, SerializationFormatting.None);
25+
json.Should().Be("{\"key_1\":\"value_1\",\"key_2\":null}");
26+
27+
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
28+
{
29+
stream.Position = 0;
30+
var dictionary = serializer.Deserialize<IReadOnlyDictionary<string, object>>(stream);
31+
var deserializedCompositeKey = new CompositeKey(dictionary);
32+
compositeKey.Should().Equal(deserializedCompositeKey);
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)