Skip to content

Commit c9c524b

Browse files
authored
Fix duplicate object serialisation for aggs (#6479)
1 parent 4bd468d commit c9c524b

File tree

5 files changed

+130
-7
lines changed

5 files changed

+130
-7
lines changed

src/Nest/Aggregations/AggregationContainer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,16 @@ public class AggregationContainer : IAggregationContainer
306306
{
307307
public IAdjacencyMatrixAggregation AdjacencyMatrix { get; set; }
308308

309+
private AggregationDictionary _aggs;
310+
309311
// This is currently used to support deserializing the response from SQL Translate,
310312
// which forms a response which uses "aggregations", rather than "aggs". Longer term
311313
// it would be preferred to address that in Elasticsearch itself.
312314
[DataMember(Name = "aggregations")]
313-
private AggregationDictionary _aggs;
314-
315+
#pragma warning disable IDE0051 // Remove unused private members
316+
private AggregationDictionary AggregationsProxy { set => _aggs = value; }
317+
#pragma warning restore IDE0051 // Remove unused private members
318+
315319
// ReSharper disable once ConvertToAutoProperty
316320
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }
317321

src/Nest/Search/Search/SearchRequest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,17 @@ public partial interface ISearchRequest<TInferDocument> : ISearchRequest { }
194194
[DataContract]
195195
public partial class SearchRequest
196196
{
197+
private AggregationDictionary _aggs;
198+
197199
// This is currently used to support deserializing the response from SQL Translate,
198200
// which forms a response which uses "aggregations", rather than "aggs". Longer term
199201
// it would be preferred to address that in Elasticsearch itself.
200202
[DataMember(Name = "aggregations")]
201-
private AggregationDictionary _aggs;
203+
#pragma warning disable IDE0051 // Remove unused private members
204+
private AggregationDictionary AggregationsProxy { set => _aggs = value; }
205+
#pragma warning restore IDE0051 // Remove unused private members
202206

203207
/// <inheritdoc />
204-
// ReSharper disable once ConvertToAutoProperty
205208
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }
206209
/// <inheritdoc />
207210
public IFieldCollapse Collapse { get; set; }

src/Nest/XPack/AsyncSearch/Submit/AsyncSearchSubmitRequest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,18 @@ public partial interface IAsyncSearchSubmitRequest<TInferDocument> : IAsyncSearc
121121
[DataContract]
122122
public partial class AsyncSearchSubmitRequest
123123
{
124+
private AggregationDictionary _aggs;
125+
126+
// This is currently used to support deserializing the response from SQL Translate,
127+
// which forms a response which uses "aggregations", rather than "aggs". Longer term
128+
// it would be preferred to address that in Elasticsearch itself.
129+
[DataMember(Name = "aggregations")]
130+
#pragma warning disable IDE0051 // Remove unused private members
131+
private AggregationDictionary AggregationsProxy { set => _aggs = value; }
132+
#pragma warning restore IDE0051 // Remove unused private members
133+
124134
/// <inheritdoc />
125-
public AggregationDictionary Aggregations { get; set; }
135+
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }
126136
/// <inheritdoc />
127137
public IFieldCollapse Collapse { get; set; }
128138
/// <inheritdoc />

tests/Tests.Configuration/tests.default.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# tracked by git).
66

77
# mode either u (unit test), i (integration test) or m (mixed mode)
8-
mode: i
8+
mode: u
99

1010
# the elasticsearch version that should be started
1111
# Can be a snapshot version of sonatype or "latest" to get the latest snapshot of sonatype
@@ -18,7 +18,7 @@ force_reseed: true
1818
# this is opt in during development in CI we never want to see our tests running against an already running node
1919
test_against_already_running_elasticsearch: true
2020

21-
random_source_serializer: true
21+
#random_source_serializer: true
2222
#random_old_connection: true
2323
#random_http_compresssion: true
2424
#random_api_versioning: true
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.IO;
6+
using System.Text;
7+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
8+
using FluentAssertions;
9+
using Nest;
10+
11+
namespace Tests.Aggregations
12+
{
13+
public class SearchAggregationSerializationTests
14+
{
15+
private const string ExpectedAggsJson = @"{""aggs"":{""test-agg"":{""terms"":{""field"":""my-field""}}}}";
16+
private const string AggregationsJson = @"{""aggregations"":{""test-agg"":{""terms"":{""field"":""my-field""}}}}";
17+
18+
[U] public void SerializesAsExpected()
19+
{
20+
var client = new ElasticClient();
21+
22+
var request = new SearchRequest
23+
{
24+
Aggregations = new TermsAggregation("test-agg")
25+
{
26+
Field = "my-field"
27+
}
28+
};
29+
30+
var stream = new MemoryStream();
31+
client.RequestResponseSerializer.Serialize(request, stream);
32+
33+
stream.Position = 0;
34+
var reader = new StreamReader(stream);
35+
var json = reader.ReadToEnd();
36+
37+
json.Should().Be(ExpectedAggsJson);
38+
}
39+
40+
[U]
41+
public void DeserializesAggsAsExpected()
42+
{
43+
var client = new ElasticClient();
44+
var stream = new MemoryStream(Encoding.UTF8.GetBytes(ExpectedAggsJson));
45+
var request = client.RequestResponseSerializer.Deserialize<SearchRequest>(stream);
46+
request.Aggregations.Should().HaveCount(1);
47+
}
48+
49+
[U]
50+
public void DeserializesAggregationsAsExpected()
51+
{
52+
var client = new ElasticClient();
53+
var stream = new MemoryStream(Encoding.UTF8.GetBytes(AggregationsJson));
54+
var request = client.RequestResponseSerializer.Deserialize<SearchRequest>(stream);
55+
request.Aggregations.Should().HaveCount(1);
56+
}
57+
}
58+
59+
public class AggregationContainer_AggregationSerializationTests
60+
{
61+
private const string ExpectedAggsJson = @"{""aggs"":{""sub-agg"":{""terms"":{""field"":""my-field""}}},""terms"":{""field"":""my-field""}}";
62+
private const string AggregationsJson = @"{""aggregations"":{""sub-agg"":{""terms"":{""field"":""my-field""}}},""terms"":{""field"":""my-field""}}";
63+
64+
[U]
65+
public void SerializesAsExpected()
66+
{
67+
var client = new ElasticClient();
68+
69+
AggregationContainer aggsDictionary = new TermsAggregation("test-agg")
70+
{
71+
Field = "my-field",
72+
Aggregations = new TermsAggregation("sub-agg")
73+
{
74+
Field = "my-field"
75+
}
76+
};
77+
78+
var stream = new MemoryStream();
79+
client.RequestResponseSerializer.Serialize(aggsDictionary, stream);
80+
81+
stream.Position = 0;
82+
var reader = new StreamReader(stream);
83+
var json = reader.ReadToEnd();
84+
85+
json.Should().Be(ExpectedAggsJson);
86+
}
87+
88+
[U]
89+
public void DeserializesAggsAsExpected()
90+
{
91+
var client = new ElasticClient();
92+
var stream = new MemoryStream(Encoding.UTF8.GetBytes(ExpectedAggsJson));
93+
var request = client.RequestResponseSerializer.Deserialize<AggregationContainer>(stream);
94+
request.Aggregations.Should().HaveCount(1);
95+
}
96+
97+
[U]
98+
public void DeserializesAggregationsAsExpected()
99+
{
100+
var client = new ElasticClient();
101+
var stream = new MemoryStream(Encoding.UTF8.GetBytes(AggregationsJson));
102+
var request = client.RequestResponseSerializer.Deserialize<AggregationContainer>(stream);
103+
request.Aggregations.Should().HaveCount(1);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)