Skip to content

Commit 1abce28

Browse files
[9.2] Fix deserialization of SqlRow values (#8767) (#8769)
Co-authored-by: Florian Bernd <[email protected]>
1 parent 4d106be commit 1abce28

File tree

9 files changed

+72
-75
lines changed

9 files changed

+72
-75
lines changed

src/Elastic.Clients.Elasticsearch/_Generated/Api/Sql/GetAsyncResponse.Converters.g.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public sealed partial class GetAsyncResponseConverter : System.Text.Json.Seriali
3030
private static readonly System.Text.Json.JsonEncodedText PropId = System.Text.Json.JsonEncodedText.Encode("id");
3131
private static readonly System.Text.Json.JsonEncodedText PropIsPartial = System.Text.Json.JsonEncodedText.Encode("is_partial");
3232
private static readonly System.Text.Json.JsonEncodedText PropIsRunning = System.Text.Json.JsonEncodedText.Encode("is_running");
33+
private static readonly System.Text.Json.JsonEncodedText PropRows = System.Text.Json.JsonEncodedText.Encode("rows");
3334

3435
public override Elastic.Clients.Elasticsearch.Sql.GetAsyncResponse Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
3536
{
@@ -39,6 +40,7 @@ public override Elastic.Clients.Elasticsearch.Sql.GetAsyncResponse Read(ref Syst
3940
LocalJsonValue<string> propId = default;
4041
LocalJsonValue<bool> propIsPartial = default;
4142
LocalJsonValue<bool> propIsRunning = default;
43+
LocalJsonValue<System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow>> propRows = default;
4244
while (reader.Read() && reader.TokenType is System.Text.Json.JsonTokenType.PropertyName)
4345
{
4446
if (propColumns.TryReadProperty(ref reader, options, PropColumns, static System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.Column>? (ref System.Text.Json.Utf8JsonReader r, System.Text.Json.JsonSerializerOptions o) => r.ReadCollectionValue<Elastic.Clients.Elasticsearch.Sql.Column>(o, null)))
@@ -66,6 +68,11 @@ public override Elastic.Clients.Elasticsearch.Sql.GetAsyncResponse Read(ref Syst
6668
continue;
6769
}
6870

71+
if (propRows.TryReadProperty(ref reader, options, PropRows, static System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow> (ref System.Text.Json.Utf8JsonReader r, System.Text.Json.JsonSerializerOptions o) => r.ReadCollectionValue<Elastic.Clients.Elasticsearch.Sql.SqlRow>(o, null)!))
72+
{
73+
continue;
74+
}
75+
6976
if (options.UnmappedMemberHandling is System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip)
7077
{
7178
reader.SafeSkip();
@@ -82,7 +89,8 @@ public override Elastic.Clients.Elasticsearch.Sql.GetAsyncResponse Read(ref Syst
8289
Cursor = propCursor.Value,
8390
Id = propId.Value,
8491
IsPartial = propIsPartial.Value,
85-
IsRunning = propIsRunning.Value
92+
IsRunning = propIsRunning.Value,
93+
Rows = propRows.Value
8694
};
8795
}
8896

@@ -94,6 +102,7 @@ public override void Write(System.Text.Json.Utf8JsonWriter writer, Elastic.Clien
94102
writer.WriteProperty(options, PropId, value.Id, null, null);
95103
writer.WriteProperty(options, PropIsPartial, value.IsPartial, null, null);
96104
writer.WriteProperty(options, PropIsRunning, value.IsRunning, null, null);
105+
writer.WriteProperty(options, PropRows, value.Rows, null, static (System.Text.Json.Utf8JsonWriter w, System.Text.Json.JsonSerializerOptions o, System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow> v) => w.WriteCollectionValue<Elastic.Clients.Elasticsearch.Sql.SqlRow>(o, v, null));
97106
writer.WriteEndObject();
98107
}
99108
}

src/Elastic.Clients.Elasticsearch/_Generated/Api/Sql/GetAsyncResponse.g.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace Elastic.Clients.Elasticsearch.Sql;
2525

2626
[System.Text.Json.Serialization.JsonConverter(typeof(Elastic.Clients.Elasticsearch.Sql.Json.GetAsyncResponseConverter))]
27-
public partial class GetAsyncResponse : Elastic.Transport.Products.Elasticsearch.ElasticsearchResponse
27+
public sealed partial class GetAsyncResponse : Elastic.Transport.Products.Elasticsearch.ElasticsearchResponse
2828
{
2929
[System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
3030
public GetAsyncResponse()
@@ -93,4 +93,15 @@ internal GetAsyncResponse(Elastic.Clients.Elasticsearch.Serialization.JsonConstr
9393
required
9494
#endif
9595
bool IsRunning { get; set; }
96+
97+
/// <summary>
98+
/// <para>
99+
/// The values for the search results.
100+
/// </para>
101+
/// </summary>
102+
public
103+
#if NET7_0_OR_GREATER
104+
required
105+
#endif
106+
System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow> Rows { get; set; }
96107
}

src/Elastic.Clients.Elasticsearch/_Generated/Api/Sql/QueryResponse.Converters.g.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public sealed partial class QueryResponseConverter : System.Text.Json.Serializat
3030
private static readonly System.Text.Json.JsonEncodedText PropId = System.Text.Json.JsonEncodedText.Encode("id");
3131
private static readonly System.Text.Json.JsonEncodedText PropIsPartial = System.Text.Json.JsonEncodedText.Encode("is_partial");
3232
private static readonly System.Text.Json.JsonEncodedText PropIsRunning = System.Text.Json.JsonEncodedText.Encode("is_running");
33+
private static readonly System.Text.Json.JsonEncodedText PropRows = System.Text.Json.JsonEncodedText.Encode("rows");
3334

3435
public override Elastic.Clients.Elasticsearch.Sql.QueryResponse Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
3536
{
@@ -39,6 +40,7 @@ public override Elastic.Clients.Elasticsearch.Sql.QueryResponse Read(ref System.
3940
LocalJsonValue<string?> propId = default;
4041
LocalJsonValue<bool?> propIsPartial = default;
4142
LocalJsonValue<bool?> propIsRunning = default;
43+
LocalJsonValue<System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow>> propRows = default;
4244
while (reader.Read() && reader.TokenType is System.Text.Json.JsonTokenType.PropertyName)
4345
{
4446
if (propColumns.TryReadProperty(ref reader, options, PropColumns, static System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.Column>? (ref System.Text.Json.Utf8JsonReader r, System.Text.Json.JsonSerializerOptions o) => r.ReadCollectionValue<Elastic.Clients.Elasticsearch.Sql.Column>(o, null)))
@@ -66,6 +68,11 @@ public override Elastic.Clients.Elasticsearch.Sql.QueryResponse Read(ref System.
6668
continue;
6769
}
6870

71+
if (propRows.TryReadProperty(ref reader, options, PropRows, static System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow> (ref System.Text.Json.Utf8JsonReader r, System.Text.Json.JsonSerializerOptions o) => r.ReadCollectionValue<Elastic.Clients.Elasticsearch.Sql.SqlRow>(o, null)!))
72+
{
73+
continue;
74+
}
75+
6976
if (options.UnmappedMemberHandling is System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip)
7077
{
7178
reader.SafeSkip();
@@ -82,7 +89,8 @@ public override Elastic.Clients.Elasticsearch.Sql.QueryResponse Read(ref System.
8289
Cursor = propCursor.Value,
8390
Id = propId.Value,
8491
IsPartial = propIsPartial.Value,
85-
IsRunning = propIsRunning.Value
92+
IsRunning = propIsRunning.Value,
93+
Rows = propRows.Value
8694
};
8795
}
8896

@@ -94,6 +102,7 @@ public override void Write(System.Text.Json.Utf8JsonWriter writer, Elastic.Clien
94102
writer.WriteProperty(options, PropId, value.Id, null, null);
95103
writer.WriteProperty(options, PropIsPartial, value.IsPartial, null, static (System.Text.Json.Utf8JsonWriter w, System.Text.Json.JsonSerializerOptions o, bool? v) => w.WriteNullableValue<bool>(o, v));
96104
writer.WriteProperty(options, PropIsRunning, value.IsRunning, null, static (System.Text.Json.Utf8JsonWriter w, System.Text.Json.JsonSerializerOptions o, bool? v) => w.WriteNullableValue<bool>(o, v));
105+
writer.WriteProperty(options, PropRows, value.Rows, null, static (System.Text.Json.Utf8JsonWriter w, System.Text.Json.JsonSerializerOptions o, System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow> v) => w.WriteCollectionValue<Elastic.Clients.Elasticsearch.Sql.SqlRow>(o, v, null));
97106
writer.WriteEndObject();
98107
}
99108
}

src/Elastic.Clients.Elasticsearch/_Generated/Api/Sql/QueryResponse.g.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace Elastic.Clients.Elasticsearch.Sql;
2525

2626
[System.Text.Json.Serialization.JsonConverter(typeof(Elastic.Clients.Elasticsearch.Sql.Json.QueryResponseConverter))]
27-
public partial class QueryResponse : Elastic.Transport.Products.Elasticsearch.ElasticsearchResponse
27+
public sealed partial class QueryResponse : Elastic.Transport.Products.Elasticsearch.ElasticsearchResponse
2828
{
2929
[System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
3030
public QueryResponse()
@@ -81,4 +81,15 @@ internal QueryResponse(Elastic.Clients.Elasticsearch.Serialization.JsonConstruct
8181
/// </para>
8282
/// </summary>
8383
public bool? IsRunning { get; set; }
84+
85+
/// <summary>
86+
/// <para>
87+
/// The values for the search results.
88+
/// </para>
89+
/// </summary>
90+
public
91+
#if NET7_0_OR_GREATER
92+
required
93+
#endif
94+
System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow> Rows { get; set; }
8495
}

src/Elastic.Clients.Elasticsearch/_Shared/Api/Sql/GetAsyncResponse.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Elastic.Clients.Elasticsearch/_Shared/Api/Sql/QueryResponse.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Elastic.Clients.Elasticsearch/_Shared/Core/LazyJson.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Text.Json;
67
using System.Text.Json.Serialization;
78

9+
using Elastic.Transport.Extensions;
10+
811
namespace Elastic.Clients.Elasticsearch;
912

1013
/// <summary>
@@ -14,13 +17,13 @@ namespace Elastic.Clients.Elasticsearch;
1417
[JsonConverter(typeof(Json.LazyJsonConverter))]
1518
public readonly struct LazyJson
1619
{
17-
internal LazyJson(byte[] bytes, IElasticsearchClientSettings settings)
20+
internal LazyJson(JsonElement json, IElasticsearchClientSettings? settings)
1821
{
19-
Bytes = bytes;
22+
Json = json;
2023
Settings = settings;
2124
}
2225

23-
internal byte[]? Bytes { get; }
26+
internal JsonElement Json { get; }
2427
internal IElasticsearchClientSettings? Settings { get; }
2528

2629
/// <summary>
@@ -30,10 +33,11 @@ internal LazyJson(byte[] bytes, IElasticsearchClientSettings settings)
3033
/// <typeparam name="T">The type</typeparam>
3134
public T? As<T>()
3235
{
33-
if (Bytes is null || Settings is null || Bytes.Length == 0)
34-
return default;
36+
if (Settings is null)
37+
{
38+
throw new InvalidOperationException($"Can not deserialize value without '{nameof(Settings)}'.");
39+
}
3540

36-
using var ms = Settings.MemoryStreamFactory.Create(Bytes);
37-
return Settings.SourceSerializer.Deserialize<T>(ms);
41+
return Settings.SourceSerializer.Deserialize<T>(Json);
3842
}
3943
}

src/Elastic.Clients.Elasticsearch/_Shared/Core/LazyJsonConverter.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,32 @@ namespace Elastic.Clients.Elasticsearch.Json;
1212

1313
public sealed class LazyJsonConverter : JsonConverter<LazyJson>
1414
{
15-
private IElasticsearchClientSettings _settings;
15+
private IElasticsearchClientSettings? _settings;
1616

1717
public override LazyJson Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1818
{
1919
InitializeSettings(options);
2020

21-
// TODO: fixme
22-
#pragma warning disable IL2026, IL3050
23-
using var jsonDoc = JsonSerializer.Deserialize<JsonDocument>(ref reader);
21+
#pragma warning disable IL2026, IL3050 // The `TypeInfoResolver` for `RequestResponseConverter` knows how to handle `JsonElement`.
22+
return new LazyJson(JsonSerializer.Deserialize<JsonElement>(ref reader, options), _settings!);
2423
#pragma warning restore IL2026, IL3050
25-
using var stream = _settings.MemoryStreamFactory.Create();
26-
27-
var writer = new Utf8JsonWriter(stream);
28-
jsonDoc.WriteTo(writer);
29-
writer.Flush();
30-
31-
return new LazyJson(stream.ToArray(), _settings);
3224
}
3325

34-
public override void Write(Utf8JsonWriter writer, LazyJson value, JsonSerializerOptions options) => throw new NotImplementedException("We only ever expect to deserialize LazyJson on responses.");
35-
3626
private void InitializeSettings(JsonSerializerOptions options)
3727
{
38-
if (_settings is null)
28+
if (_settings is not null)
3929
{
40-
if (!options.TryGetClientSettings(out var settings))
41-
ThrowHelper.ThrowJsonExceptionForMissingSettings();
30+
return;
31+
}
4232

43-
_settings = settings;
33+
if (!ContextProvider<IElasticsearchClientSettings>.TryGetContext(options, out _settings))
34+
{
35+
ThrowHelper.ThrowJsonExceptionForMissingSettings();
4436
}
4537
}
38+
39+
public override void Write(Utf8JsonWriter writer, LazyJson value, JsonSerializerOptions options)
40+
{
41+
throw new NotImplementedException("We only ever expect to deserialize LazyJson on responses.");
42+
}
4643
}

src/Elastic.Clients.Elasticsearch/_Shared/Types/Sql/SqlRowConverter.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
76
using System.Text.Json;
87
using System.Text.Json.Serialization;
98

@@ -15,26 +14,13 @@ public sealed class SqlRowConverter : JsonConverter<SqlRow>
1514
{
1615
public override SqlRow? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1716
{
18-
if (reader.TokenType == JsonTokenType.Null)
17+
var values = reader.ReadCollectionValue<SqlValue>(options, null);
18+
if (values is null)
1919
{
20-
reader.Read();
2120
return null;
2221
}
2322

24-
if (reader.TokenType == JsonTokenType.StartArray)
25-
{
26-
var values = new List<SqlValue>();
27-
28-
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
29-
{
30-
var value = reader.ReadValue<SqlValue>(options);
31-
values.Add(value);
32-
}
33-
34-
return new SqlRow(values);
35-
}
36-
37-
throw new JsonException($"Unexpected JSON token when deserializing {nameof(SqlRow)}.");
23+
return new SqlRow(values);
3824
}
3925

4026
public override void Write(Utf8JsonWriter writer, SqlRow value, JsonSerializerOptions options) => throw new NotImplementedException();

0 commit comments

Comments
 (0)