Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public sealed partial class GetAsyncResponseConverter : System.Text.Json.Seriali
private static readonly System.Text.Json.JsonEncodedText PropId = System.Text.Json.JsonEncodedText.Encode("id");
private static readonly System.Text.Json.JsonEncodedText PropIsPartial = System.Text.Json.JsonEncodedText.Encode("is_partial");
private static readonly System.Text.Json.JsonEncodedText PropIsRunning = System.Text.Json.JsonEncodedText.Encode("is_running");
private static readonly System.Text.Json.JsonEncodedText PropRows = System.Text.Json.JsonEncodedText.Encode("rows");

public override Elastic.Clients.Elasticsearch.Sql.GetAsyncResponse Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
Expand All @@ -39,6 +40,7 @@ public override Elastic.Clients.Elasticsearch.Sql.GetAsyncResponse Read(ref Syst
LocalJsonValue<string> propId = default;
LocalJsonValue<bool> propIsPartial = default;
LocalJsonValue<bool> propIsRunning = default;
LocalJsonValue<System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow>> propRows = default;
while (reader.Read() && reader.TokenType is System.Text.Json.JsonTokenType.PropertyName)
{
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)))
Expand Down Expand Up @@ -66,6 +68,11 @@ public override Elastic.Clients.Elasticsearch.Sql.GetAsyncResponse Read(ref Syst
continue;
}

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)!))
{
continue;
}

if (options.UnmappedMemberHandling is System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip)
{
reader.SafeSkip();
Expand All @@ -82,7 +89,8 @@ public override Elastic.Clients.Elasticsearch.Sql.GetAsyncResponse Read(ref Syst
Cursor = propCursor.Value,
Id = propId.Value,
IsPartial = propIsPartial.Value,
IsRunning = propIsRunning.Value
IsRunning = propIsRunning.Value,
Rows = propRows.Value
};
}

Expand All @@ -94,6 +102,7 @@ public override void Write(System.Text.Json.Utf8JsonWriter writer, Elastic.Clien
writer.WriteProperty(options, PropId, value.Id, null, null);
writer.WriteProperty(options, PropIsPartial, value.IsPartial, null, null);
writer.WriteProperty(options, PropIsRunning, value.IsRunning, null, null);
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));
writer.WriteEndObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Elastic.Clients.Elasticsearch.Sql;

[System.Text.Json.Serialization.JsonConverter(typeof(Elastic.Clients.Elasticsearch.Sql.Json.GetAsyncResponseConverter))]
public partial class GetAsyncResponse : Elastic.Transport.Products.Elasticsearch.ElasticsearchResponse
public sealed partial class GetAsyncResponse : Elastic.Transport.Products.Elasticsearch.ElasticsearchResponse
{
[System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
public GetAsyncResponse()
Expand Down Expand Up @@ -93,4 +93,15 @@ internal GetAsyncResponse(Elastic.Clients.Elasticsearch.Serialization.JsonConstr
required
#endif
bool IsRunning { get; set; }

/// <summary>
/// <para>
/// The values for the search results.
/// </para>
/// </summary>
public
#if NET7_0_OR_GREATER
required
#endif
System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow> Rows { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public sealed partial class QueryResponseConverter : System.Text.Json.Serializat
private static readonly System.Text.Json.JsonEncodedText PropId = System.Text.Json.JsonEncodedText.Encode("id");
private static readonly System.Text.Json.JsonEncodedText PropIsPartial = System.Text.Json.JsonEncodedText.Encode("is_partial");
private static readonly System.Text.Json.JsonEncodedText PropIsRunning = System.Text.Json.JsonEncodedText.Encode("is_running");
private static readonly System.Text.Json.JsonEncodedText PropRows = System.Text.Json.JsonEncodedText.Encode("rows");

public override Elastic.Clients.Elasticsearch.Sql.QueryResponse Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
Expand All @@ -39,6 +40,7 @@ public override Elastic.Clients.Elasticsearch.Sql.QueryResponse Read(ref System.
LocalJsonValue<string?> propId = default;
LocalJsonValue<bool?> propIsPartial = default;
LocalJsonValue<bool?> propIsRunning = default;
LocalJsonValue<System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow>> propRows = default;
while (reader.Read() && reader.TokenType is System.Text.Json.JsonTokenType.PropertyName)
{
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)))
Expand Down Expand Up @@ -66,6 +68,11 @@ public override Elastic.Clients.Elasticsearch.Sql.QueryResponse Read(ref System.
continue;
}

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)!))
{
continue;
}

if (options.UnmappedMemberHandling is System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip)
{
reader.SafeSkip();
Expand All @@ -82,7 +89,8 @@ public override Elastic.Clients.Elasticsearch.Sql.QueryResponse Read(ref System.
Cursor = propCursor.Value,
Id = propId.Value,
IsPartial = propIsPartial.Value,
IsRunning = propIsRunning.Value
IsRunning = propIsRunning.Value,
Rows = propRows.Value
};
}

Expand All @@ -94,6 +102,7 @@ public override void Write(System.Text.Json.Utf8JsonWriter writer, Elastic.Clien
writer.WriteProperty(options, PropId, value.Id, null, null);
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));
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));
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));
writer.WriteEndObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Elastic.Clients.Elasticsearch.Sql;

[System.Text.Json.Serialization.JsonConverter(typeof(Elastic.Clients.Elasticsearch.Sql.Json.QueryResponseConverter))]
public partial class QueryResponse : Elastic.Transport.Products.Elasticsearch.ElasticsearchResponse
public sealed partial class QueryResponse : Elastic.Transport.Products.Elasticsearch.ElasticsearchResponse
{
[System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
public QueryResponse()
Expand Down Expand Up @@ -81,4 +81,15 @@ internal QueryResponse(Elastic.Clients.Elasticsearch.Serialization.JsonConstruct
/// </para>
/// </summary>
public bool? IsRunning { get; set; }

/// <summary>
/// <para>
/// The values for the search results.
/// </para>
/// </summary>
public
#if NET7_0_OR_GREATER
required
#endif
System.Collections.Generic.IReadOnlyCollection<Elastic.Clients.Elasticsearch.Sql.SqlRow> Rows { get; set; }
}

This file was deleted.

15 changes: 0 additions & 15 deletions src/Elastic.Clients.Elasticsearch/_Shared/Api/Sql/QueryResponse.cs

This file was deleted.

18 changes: 11 additions & 7 deletions src/Elastic.Clients.Elasticsearch/_Shared/Core/LazyJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

using Elastic.Transport.Extensions;

namespace Elastic.Clients.Elasticsearch;

/// <summary>
Expand All @@ -14,13 +17,13 @@ namespace Elastic.Clients.Elasticsearch;
[JsonConverter(typeof(Json.LazyJsonConverter))]
public readonly struct LazyJson
{
internal LazyJson(byte[] bytes, IElasticsearchClientSettings settings)
internal LazyJson(JsonElement json, IElasticsearchClientSettings? settings)
{
Bytes = bytes;
Json = json;
Settings = settings;
}

internal byte[]? Bytes { get; }
internal JsonElement Json { get; }
internal IElasticsearchClientSettings? Settings { get; }

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

using var ms = Settings.MemoryStreamFactory.Create(Bytes);
return Settings.SourceSerializer.Deserialize<T>(ms);
return Settings.SourceSerializer.Deserialize<T>(Json);
}
}
31 changes: 14 additions & 17 deletions src/Elastic.Clients.Elasticsearch/_Shared/Core/LazyJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,32 @@ namespace Elastic.Clients.Elasticsearch.Json;

public sealed class LazyJsonConverter : JsonConverter<LazyJson>
{
private IElasticsearchClientSettings _settings;
private IElasticsearchClientSettings? _settings;

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

// TODO: fixme
#pragma warning disable IL2026, IL3050
using var jsonDoc = JsonSerializer.Deserialize<JsonDocument>(ref reader);
#pragma warning disable IL2026, IL3050 // The `TypeInfoResolver` for `RequestResponseConverter` knows how to handle `JsonElement`.
return new LazyJson(JsonSerializer.Deserialize<JsonElement>(ref reader, options), _settings!);
#pragma warning restore IL2026, IL3050
using var stream = _settings.MemoryStreamFactory.Create();

var writer = new Utf8JsonWriter(stream);
jsonDoc.WriteTo(writer);
writer.Flush();

return new LazyJson(stream.ToArray(), _settings);
}

public override void Write(Utf8JsonWriter writer, LazyJson value, JsonSerializerOptions options) => throw new NotImplementedException("We only ever expect to deserialize LazyJson on responses.");

private void InitializeSettings(JsonSerializerOptions options)
{
if (_settings is null)
if (_settings is not null)
{
if (!options.TryGetClientSettings(out var settings))
ThrowHelper.ThrowJsonExceptionForMissingSettings();
return;
}

_settings = settings;
if (!ContextProvider<IElasticsearchClientSettings>.TryGetContext(options, out _settings))
{
ThrowHelper.ThrowJsonExceptionForMissingSettings();
}
}

public override void Write(Utf8JsonWriter writer, LazyJson value, JsonSerializerOptions options)
{
throw new NotImplementedException("We only ever expect to deserialize LazyJson on responses.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -15,26 +14,13 @@ public sealed class SqlRowConverter : JsonConverter<SqlRow>
{
public override SqlRow? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
var values = reader.ReadCollectionValue<SqlValue>(options, null);
if (values is null)
{
reader.Read();
return null;
}

if (reader.TokenType == JsonTokenType.StartArray)
{
var values = new List<SqlValue>();

while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
var value = reader.ReadValue<SqlValue>(options);
values.Add(value);
}

return new SqlRow(values);
}

throw new JsonException($"Unexpected JSON token when deserializing {nameof(SqlRow)}.");
return new SqlRow(values);
}

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