Skip to content

Commit 236c83b

Browse files
committed
Don't sanitize the keys for Filters aggregation named buckets #4656
This commit fixes a bug where the keys in the dictionary passed to filters aggregation are sanitized when copying to the backing dictionary. Keys in this scenario are the keys for named buckets which should never be sanitized. Fixes #4582
1 parent 4891909 commit 236c83b

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

src/Nest/Aggregations/Bucket/Filters/FiltersAggregate.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public FiltersBucketItem(IReadOnlyDictionary<string, IAggregate> dict) : base(di
1010
public long DocCount { get; set; }
1111
}
1212

13-
//TODO this is mapped rather odly we always deserialize as if this is
13+
//TODO this is mapped rather oddly we always deserialize as if this is
1414
// {
1515
// "agg1" : { ...},
1616
// "agg2" : { ... }
@@ -27,6 +27,9 @@ public FiltersAggregate() : base(EmptyReadOnly<string, IAggregate>.Dictionary) {
2727

2828
public FiltersAggregate(IReadOnlyDictionary<string, IAggregate> aggregations) : base(aggregations) { }
2929

30+
// Don't sanitize the keys as these are the keys for named buckets
31+
protected override string Sanitize(string key) => key;
32+
3033
public IReadOnlyCollection<FiltersBucketItem> Buckets { get; set; } = EmptyReadOnly<FiltersBucketItem>.Collection;
3134

3235
public SingleBucketAggregate NamedBucket(string key) => Global(key);

src/Tests/Reproduce/GithubIssue3286.cs renamed to src/Tests/Tests.Reproduce/GithubIssue3286.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
using Elasticsearch.Net;
66
using FluentAssertions;
77
using Nest;
8-
using Tests.Framework.ManagedElasticsearch.Clusters;
8+
using Tests.Core.ManagedElasticsearch.Clusters;
99

1010
namespace Tests.Reproduce
1111
{
1212
public class GithubIssue3286 : IClusterFixture<WritableCluster>
1313
{
1414
private readonly WritableCluster _cluster;
1515

16-
public GithubIssue3286(WritableCluster cluster)
17-
{
18-
_cluster = cluster;
19-
}
16+
public GithubIssue3286(WritableCluster cluster) => _cluster = cluster;
2017

2118
[I]
2219
public void AggregationsShouldNotBeNullOnNestedAggregation()

src/Tests/Reproduce/GithubIssue3311.cs renamed to src/Tests/Tests.Reproduce/GithubIssue3311.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using Elastic.Xunit.XunitPlumbing;
5-
using Elasticsearch.Net;
64
using FluentAssertions;
75
using Nest;
8-
using Tests.Framework.ManagedElasticsearch.Clusters;
6+
using Tests.Core.ManagedElasticsearch.Clusters;
97

108
namespace Tests.Reproduce
119
{
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using Elastic.Xunit.XunitPlumbing;
5+
using Elasticsearch.Net;
6+
using FluentAssertions;
7+
using Nest;
8+
9+
namespace Tests.Reproduce
10+
{
11+
public class GithubIssue4582
12+
{
13+
[U]
14+
public void DeserializeBucketKeyWithHash()
15+
{
16+
var json = @"
17+
{
18+
""hits"": {
19+
},
20+
""aggregations"":
21+
{
22+
""some_agg"" : {
23+
""buckets"" : {
24+
""value1"" : {
25+
""doc_count"" : 0
26+
},
27+
""value2"" : {
28+
""doc_count"" : 0
29+
},
30+
""value3#something else"" : {
31+
""doc_count"" : 0
32+
}
33+
}
34+
}
35+
}
36+
}
37+
";
38+
39+
var bytes = Encoding.UTF8.GetBytes(json);
40+
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
41+
var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection(bytes));
42+
var client = new ElasticClient(connectionSettings);
43+
var response = client.Search<object>(s => s.Index("index"));
44+
45+
var filters = response.Aggregations
46+
.Filters("some_agg")
47+
.Select(x => x.Key)
48+
.ToList();
49+
50+
filters[2].Should().Be("value3#something else");
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)