Skip to content

Fix code-gen for single or many types. #7244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 27, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
new ExtraSerializationData(settings),
new DictionaryResponseConverterFactory(settings)
},
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals
};

ElasticsearchClient.SettingsTable.Add(Options, settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public DefaultSourceSerializer(IElasticsearchClientSettings settings, JsonSerial
new LazyJsonConverter(settings),
new IdsConverter(settings)
},
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals
};
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

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

namespace Elastic.Clients.Elasticsearch.Serialization;

[AttributeUsage(AttributeTargets.Property)]
internal class SingleOrManyCollectionConverterAttribute : JsonConverterAttribute
{
public SingleOrManyCollectionConverterAttribute(Type convertType) => ConvertType = convertType;

public Type ConvertType { get; }

public override JsonConverter? CreateConverter(Type typeToConvert) => (JsonConverter)Activator.CreateInstance(typeof(SingleOrManyCollectionConverter<>).MakeGenericType(ConvertType));
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Elastic.Clients.Elasticsearch.Serialization;

internal abstract class ICollectionSingleOrManyConverter<TItem> : JsonConverter<ICollection<TItem>>
internal class SingleOrManyCollectionConverter<TItem> : JsonConverter<ICollection<TItem>>
{
public override ICollection<TItem>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
SingleOrManySerializationHelper.Deserialize<TItem>(ref reader, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,49 @@ public override void Write(Utf8JsonWriter writer, TType value,
private class UnionConverterInner<TItem1, TItem2> : JsonConverter<Union<TItem1, TItem2>>
{
public override Union<TItem1, TItem2>? Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options) => null;
JsonSerializerOptions options)
{
var readerCopy = reader;

try
{
var itemOne = JsonSerializer.Deserialize<TItem1>(ref readerCopy, options);

if (itemOne is IUnionVerifiable verifiable)
{
if (verifiable.IsSuccessful)
{
reader = readerCopy;
return (TItem1)Activator.CreateInstance(typeof(TItem1), itemOne);
}
}
else if (itemOne is not null)
{
reader = readerCopy;
return (TItem1)Activator.CreateInstance(typeof(TItem1), itemOne);
}
}
catch
{
// TODO - Store for aggregate exception
}

try
{
var itemTwo = JsonSerializer.Deserialize<TItem2>(ref reader, options);

if (itemTwo is not null)
{
return (TItem2)Activator.CreateInstance(typeof(TItem2), itemTwo);
}
}
catch
{
// TODO - Store for aggregate exception
}

throw new JsonException("Unable to deserialize union."); // TODO - Add inner aggregate exception.
}

public override void Write(Utf8JsonWriter writer, Union<TItem1, TItem2> value,
JsonSerializerOptions options)
Expand Down
11 changes: 0 additions & 11 deletions src/Elastic.Clients.Elasticsearch/Types/SortConverter.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,30 @@
namespace Elastic.Clients.Elasticsearch.AsyncSearch;
public sealed partial class AsyncSearchStatusResponse : ElasticsearchResponse
{
[JsonInclude]
[JsonPropertyName("completion_status")]
[JsonInclude, JsonPropertyName("completion_status")]
public int? CompletionStatus { get; init; }

[JsonInclude]
[JsonPropertyName("expiration_time")]
[JsonInclude, JsonPropertyName("expiration_time")]
public DateTimeOffset? ExpirationTime { get; init; }

[JsonInclude]
[JsonPropertyName("expiration_time_in_millis")]
[JsonInclude, JsonPropertyName("expiration_time_in_millis")]
public long ExpirationTimeInMillis { get; init; }

[JsonInclude]
[JsonPropertyName("id")]
[JsonInclude, JsonPropertyName("id")]
public string? Id { get; init; }

[JsonInclude]
[JsonPropertyName("is_partial")]
[JsonInclude, JsonPropertyName("is_partial")]
public bool IsPartial { get; init; }

[JsonInclude]
[JsonPropertyName("is_running")]
[JsonInclude, JsonPropertyName("is_running")]
public bool IsRunning { get; init; }

[JsonInclude]
[JsonPropertyName("_shards")]
[JsonInclude, JsonPropertyName("_shards")]
public Elastic.Clients.Elasticsearch.ShardStatistics Shards { get; init; }

[JsonInclude]
[JsonPropertyName("start_time")]
[JsonInclude, JsonPropertyName("start_time")]
public DateTimeOffset? StartTime { get; init; }

[JsonInclude]
[JsonPropertyName("start_time_in_millis")]
[JsonInclude, JsonPropertyName("start_time_in_millis")]
public long StartTimeInMillis { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
namespace Elastic.Clients.Elasticsearch.AsyncSearch;
public sealed partial class DeleteAsyncSearchResponse : ElasticsearchResponse
{
[JsonInclude]
[JsonPropertyName("acknowledged")]
[JsonInclude, JsonPropertyName("acknowledged")]
public bool Acknowledged { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,27 @@
namespace Elastic.Clients.Elasticsearch.AsyncSearch;
public sealed partial class GetAsyncSearchResponse<TDocument> : ElasticsearchResponse
{
[JsonInclude]
[JsonPropertyName("expiration_time")]
[JsonInclude, JsonPropertyName("expiration_time")]
public DateTimeOffset? ExpirationTime { get; init; }

[JsonInclude]
[JsonPropertyName("expiration_time_in_millis")]
[JsonInclude, JsonPropertyName("expiration_time_in_millis")]
public long ExpirationTimeInMillis { get; init; }

[JsonInclude]
[JsonPropertyName("id")]
[JsonInclude, JsonPropertyName("id")]
public string? Id { get; init; }

[JsonInclude]
[JsonPropertyName("is_partial")]
[JsonInclude, JsonPropertyName("is_partial")]
public bool IsPartial { get; init; }

[JsonInclude]
[JsonPropertyName("is_running")]
[JsonInclude, JsonPropertyName("is_running")]
public bool IsRunning { get; init; }

[JsonInclude]
[JsonPropertyName("response")]
[JsonInclude, JsonPropertyName("response")]
public Elastic.Clients.Elasticsearch.AsyncSearch.AsyncSearch<TDocument> Response { get; init; }

[JsonInclude]
[JsonPropertyName("start_time")]
[JsonInclude, JsonPropertyName("start_time")]
public DateTimeOffset? StartTime { get; init; }

[JsonInclude]
[JsonPropertyName("start_time_in_millis")]
[JsonInclude, JsonPropertyName("start_time_in_millis")]
public long StartTimeInMillis { get; init; }
}
Loading