Skip to content

Commit f6622d5

Browse files
committed
Deserialize aggregation metadata to Dictionary<string,object> (#3073)
This commit uses the JsonSerializer to deserialize the aggregation metadata to a Dictionary<string,object> from the JsonReader. Add integration test to assert behaviour. Closes #3072 (cherry picked from commit 188d2d7)
1 parent b5f9112 commit f6622d5

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

.paket/Paket.Restore.targets

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
<PaketExePath Condition=" '$(PaketExePath)' == '' ">$(PaketToolsPath)paket.exe</PaketExePath>
1919
<PaketCommand Condition=" '$(OS)' == 'Windows_NT'">"$(PaketExePath)"</PaketCommand>
2020
<PaketCommand Condition=" '$(OS)' != 'Windows_NT' ">$(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)"</PaketCommand>
21+
22+
<!-- .net core fdd -->
23+
<_PaketExeExtension>$([System.IO.Path]::GetExtension("$(PaketExePath)"))</_PaketExeExtension>
24+
<PaketCommand Condition=" '$(_PaketExeExtension)' == '.dll' ">dotnet "$(PaketExePath)"</PaketCommand>
25+
26+
<!-- no extension is a shell script -->
27+
<PaketCommand Condition=" '$(_PaketExeExtension)' == '' ">"$(PaketExePath)"</PaketCommand>
28+
2129
<PaketBootStrapperExePath Condition=" '$(PaketBootStrapperExePath)' == '' AND Exists('$(PaketRootPath)paket.bootstrapper.exe')">$(PaketRootPath)paket.bootstrapper.exe</PaketBootStrapperExePath>
2230
<PaketBootStrapperExePath Condition=" '$(PaketBootStrapperExePath)' == '' ">$(PaketToolsPath)paket.bootstrapper.exe</PaketBootStrapperExePath>
2331
<PaketBootStrapperCommand Condition=" '$(OS)' == 'Windows_NT'">"$(PaketBootStrapperExePath)"</PaketBootStrapperCommand>
@@ -114,9 +122,11 @@
114122
<PaketReferencesFileLinesInfo Include="@(PaketReferencesFileLines)" >
115123
<PackageName>$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[0])</PackageName>
116124
<PackageVersion>$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[1])</PackageVersion>
125+
<AllPrivateAssets>$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[4])</AllPrivateAssets>
117126
</PaketReferencesFileLinesInfo>
118127
<PackageReference Include="%(PaketReferencesFileLinesInfo.PackageName)">
119128
<Version>%(PaketReferencesFileLinesInfo.PackageVersion)</Version>
129+
<PrivateAssets Condition="%(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'true'">All</PrivateAssets>
120130
</PackageReference>
121131
</ItemGroup>
122132

@@ -138,9 +148,10 @@
138148
</DotNetCliToolReference>
139149
</ItemGroup>
140150

151+
<!-- Disabled for now until we know what to do with runtime deps - https://github.com/fsprojects/Paket/issues/2964
141152
<PropertyGroup>
142153
<RestoreConfigFile>$(MSBuildProjectDirectory)/obj/$(MSBuildProjectFile).NuGet.Config</RestoreConfigFile>
143-
</PropertyGroup>
154+
</PropertyGroup> -->
144155

145156
</Target>
146157

src/Nest/Aggregations/AggregateJsonConverter.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private IAggregate ReadAggregate(JsonReader reader, JsonSerializer serializer)
105105
aggregate = GetPercentilesAggregate(reader, serializer, oldFormat: true);
106106

107107
var meta = propertyName == Parser.Meta
108-
? GetMetadata(reader)
108+
? GetMetadata(serializer, reader)
109109
: null;
110110

111111
if (aggregate != null)
@@ -183,19 +183,12 @@ private IBucket ReadBucket(JsonReader reader, JsonSerializer serializer)
183183
return item;
184184
}
185185

186-
private Dictionary<string, object> GetMetadata(JsonReader reader)
186+
private Dictionary<string, object> GetMetadata(JsonSerializer serializer, JsonReader reader)
187187
{
188-
var meta = new Dictionary<string, object>();
188+
// read past "meta" property name to start of object
189189
reader.Read();
190-
reader.Read();
191-
while (reader.TokenType != JsonToken.EndObject)
192-
{
193-
var key = (string)reader.Value;
194-
reader.Read();
195-
var value = reader.Value;
196-
meta.Add(key, value);
197-
reader.Read();
198-
}
190+
var meta = serializer.Deserialize<Dictionary<string, object>>(reader);
191+
// read past the closing end object of "meta" object
199192
reader.Read();
200193
return meta;
201194
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using FluentAssertions;
4+
using Nest;
5+
using Tests.Framework;
6+
using Tests.Framework.Integration;
7+
using Tests.Framework.ManagedElasticsearch.Clusters;
8+
using Tests.Framework.MockData;
9+
10+
namespace Tests.Aggregations
11+
{
12+
/**
13+
*=== Aggregation Metadata
14+
* Metadata can be provided per aggregation, and will be returned in the aggregation response
15+
*/
16+
public class AggregationMetaUsageTests : AggregationUsageTestBase
17+
{
18+
public AggregationMetaUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
19+
20+
protected override object ExpectJson => new
21+
{
22+
size = 0,
23+
aggs = new {
24+
min_last_activity = new
25+
{
26+
min = new
27+
{
28+
field = "lastActivity"
29+
},
30+
meta = new
31+
{
32+
meta_1 = "value_1",
33+
meta_2 = 2,
34+
meta_3 = new
35+
{
36+
meta_3 = "value_3"
37+
}
38+
}
39+
}
40+
}
41+
};
42+
43+
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
44+
.Size(0)
45+
.Aggregations(a => a
46+
.Min("min_last_activity", m => m
47+
.Field(p => p.LastActivity)
48+
.Meta(d => d
49+
.Add("meta_1", "value_1")
50+
.Add("meta_2", 2)
51+
.Add("meta_3", new { meta_3 = "value_3" })
52+
)
53+
)
54+
);
55+
56+
protected override SearchRequest<Project> Initializer => new SearchRequest<Project>()
57+
{
58+
Size = 0,
59+
Aggregations = new MinAggregation("min_last_activity", Infer.Field<Project>(p => p.LastActivity))
60+
{
61+
Meta = new Dictionary<string,object>
62+
{
63+
{ "meta_1", "value_1" },
64+
{ "meta_2", 2 },
65+
{ "meta_3", new { meta_3 = "value_3" } }
66+
}
67+
}
68+
};
69+
70+
protected override void ExpectResponse(ISearchResponse<Project> response)
71+
{
72+
response.ShouldBeValid();
73+
var min = response.Aggs.Min("min_last_activity");
74+
min.Meta.Should().NotBeNull().And.ContainKeys("meta_1", "meta_2", "meta_3");
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)