Skip to content

Commit 1ecaf8d

Browse files
authored
Merge pull request #2274 from elastic/fix/aggs-nre
Fix NRE when deserializing a multi bucket aggregate with an empty buc…
2 parents 735a10b + 7293b76 commit 1ecaf8d

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

src/Nest/Aggregations/AggregateJsonConverter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,14 @@ private IAggregate GetMultiBucketAggregate(JsonReader reader, JsonSerializer ser
374374
{
375375
reader.Read();
376376
var aggs = new Dictionary<string, IAggregate>();
377-
do
377+
while (reader.TokenType != JsonToken.EndObject)
378378
{
379379
var name = reader.Value.ToString();
380380
reader.Read();
381381
var innerAgg = this.ReadAggregate(reader, serializer);
382382
aggs.Add(name, innerAgg);
383383
reader.Read();
384-
} while (reader.TokenType != JsonToken.EndObject);
385-
384+
}
386385
reader.Read();
387386
return new FiltersAggregate(aggs);
388387
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Tests.Framework;
7+
using Tests.Framework.Integration;
8+
using Tests.Framework.MockData;
9+
using Xunit;
10+
11+
using Elasticsearch.Net;
12+
using Nest;
13+
using FluentAssertions;
14+
15+
namespace Tests.Reproduce
16+
{
17+
public class AggsDeserialization : IClusterFixture<ReadOnlyCluster>
18+
{
19+
private readonly ReadOnlyCluster _cluster;
20+
21+
public AggsDeserialization(ReadOnlyCluster cluster)
22+
{
23+
_cluster = cluster;
24+
}
25+
26+
[I]
27+
public void EmptyBucketsThrowsNullReferenceException()
28+
{
29+
var client = _cluster.Client;
30+
31+
var response = client.Search<Project>(s => s
32+
.SearchType(SearchType.Count)
33+
.Aggregations(a => a
34+
.Terms("names", ts => ts
35+
.Field(p => p.Name)
36+
.Size(0)
37+
.Order(TermsOrder.TermAscending)
38+
.Aggregations(aa => aa
39+
.Filters("filters", fs => fs
40+
.NamedFilters(nfs => nfs
41+
.Filter("foo", f => f
42+
.Term(t => t
43+
.Field(p => p.State)
44+
.Value(StateOfBeing.Stable)
45+
)
46+
)
47+
)
48+
.Aggregations(aaa => aaa
49+
.ValueCount("counts", vc => vc
50+
.Field(p => p.NumberOfCommits)
51+
)
52+
.BucketSelector("counts_bucket_filter", bs => bs
53+
.BucketsPath(bp => bp
54+
.Add("totalCounts", "counts")
55+
)
56+
.Script("totalCounts < 0") // intentionally filter all documents to cause empty buckets object
57+
)
58+
)
59+
)
60+
)
61+
)
62+
)
63+
);
64+
65+
response.IsValid.Should().BeTrue();
66+
}
67+
}
68+
}

src/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@
589589
<Compile Include="QueryDsl\BoolDsl\Operators\OrAssignManyManualBoolsTests.cs" />
590590
<Compile Include="QueryDsl\Span\FieldMasking\SpanFieldMaskingUsageTests.cs" />
591591
<Compile Include="QueryDsl\Verbatim\VerbatimAndStrictQueryUsageTests.cs" />
592+
<Compile Include="Reproduce\AggsDeserialization.cs" />
592593
<Compile Include="Reproduce\GithubIssue2101.cs" />
593594
<Compile Include="Reproduce\GithubIssue2152.cs" />
594595
<Compile Include="Reproduce\GithubIssue2052.cs" />

src/Tests/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# mode either u (unit test), i (integration test) or m (mixed mode)
2-
mode: u
2+
mode: i
33
# the elasticsearch version that should be started
44
elasticsearch_version: 2.4.0
55
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running

0 commit comments

Comments
 (0)