Skip to content

Commit 33f13f4

Browse files
[8.19] Allow Esql.QueryAsObjects() to work with nested types (#8729) (#8730)
Co-authored-by: Florian Bernd <[email protected]>
1 parent c5bea54 commit 33f13f4

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/Elastic.Clients.Elasticsearch/_Shared/Client/ElasticsearchClient.Esql.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Text.Json;
1010
using System.Text.Json.Nodes;
11+
using System.Text.Json.Serialization;
1112
using System.Threading.Tasks;
1213
using System.Threading;
1314
using Elastic.Transport;
@@ -16,6 +17,15 @@ namespace Elastic.Clients.Elasticsearch.Esql;
1617

1718
public partial class EsqlNamespacedClient
1819
{
20+
#pragma warning disable IL2026, IL3050
21+
22+
private static readonly JsonSerializerOptions EsqlJsonSerializerOptions = new JsonSerializerOptions(JsonSerializerOptions.Default)
23+
{
24+
TypeInfoResolver = EsqlJsonSerializerContext.Default
25+
};
26+
27+
#pragma warning restore IL2026, IL3050
28+
1929
/// <summary>
2030
/// Executes an ES|QL request and returns the response as a stream.
2131
/// </summary>
@@ -71,9 +81,8 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
7181
{
7282
// TODO: Improve performance
7383

74-
// TODO: fixme
7584
#pragma warning disable IL2026, IL3050
76-
using var doc = JsonSerializer.Deserialize<JsonDocument>(response.Data) ?? throw new JsonException();
85+
using var doc = JsonSerializer.Deserialize<JsonDocument>(response.Data, EsqlJsonSerializerOptions) ?? throw new JsonException();
7786
#pragma warning restore IL2026, IL3050
7887

7988
if (!doc.RootElement.TryGetProperty("columns"u8, out var columns) || (columns.ValueKind is not JsonValueKind.Array))
@@ -107,9 +116,17 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
107116
writer.Reset();
108117

109118
var properties = names.Zip(document.EnumerateArray(),
110-
(key, value) => new KeyValuePair<string, JsonNode?>(key, JsonValue.Create(value)));
119+
(key, value) => new KeyValuePair<string, JsonNode?>(key, value.ValueKind switch
120+
{
121+
JsonValueKind.Object => JsonObject.Create(value),
122+
JsonValueKind.Array => JsonArray.Create(value),
123+
_ => JsonValue.Create(value)
124+
}));
125+
111126
foreach (var property in properties)
127+
{
112128
obj.Add(property);
129+
}
113130

114131
obj.WriteTo(writer);
115132
writer.Flush();
@@ -120,4 +137,8 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
120137
yield return result;
121138
}
122139
}
140+
141+
[JsonSerializable(typeof(JsonDocument))]
142+
internal sealed partial class EsqlJsonSerializerContext :
143+
JsonSerializerContext;
123144
}

0 commit comments

Comments
 (0)