From 778b9ebad159ef153530b663fcc795c52e0c39ea Mon Sep 17 00:00:00 2001 From: Florian Bernd Date: Wed, 24 Apr 2024 10:26:02 +0200 Subject: [PATCH] Fix `BucketsPath` (de-)serialization (#8164) --- .../Types/Aggregations/BucketsPath.g.cs | 122 ------------------ .../Types/Aggregations/BucketsPath.cs} | 70 ++++++---- 2 files changed, 43 insertions(+), 149 deletions(-) delete mode 100644 src/Elastic.Clients.Elasticsearch.Serverless/_Generated/Types/Aggregations/BucketsPath.g.cs rename src/{Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/BucketsPath.g.cs => Elastic.Clients.Elasticsearch.Shared/Types/Aggregations/BucketsPath.cs} (54%) diff --git a/src/Elastic.Clients.Elasticsearch.Serverless/_Generated/Types/Aggregations/BucketsPath.g.cs b/src/Elastic.Clients.Elasticsearch.Serverless/_Generated/Types/Aggregations/BucketsPath.g.cs deleted file mode 100644 index 63efac1f522..00000000000 --- a/src/Elastic.Clients.Elasticsearch.Serverless/_Generated/Types/Aggregations/BucketsPath.g.cs +++ /dev/null @@ -1,122 +0,0 @@ -// 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. -// -// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗ -// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝ -// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗ -// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝ -// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗ -// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝ -// ------------------------------------------------ -// -// This file is automatically generated. -// Please do not edit these files manually. -// -// ------------------------------------------------ - -#nullable restore - -using Elastic.Clients.Elasticsearch.Serverless.Core; -using Elastic.Clients.Elasticsearch.Serverless.Fluent; -using Elastic.Clients.Elasticsearch.Serverless.Serialization; -using Elastic.Transport; -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq.Expressions; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace Elastic.Clients.Elasticsearch.Serverless.Aggregations; - -/// -/// Buckets path can be expressed in different ways, and an aggregation may accept some or all of these
forms depending on its type. Please refer to each aggregation's documentation to know what buckets
path forms they accept.
-///
-[JsonConverter(typeof(BucketsPathConverter))] -public sealed partial class BucketsPath : IComplexUnion -{ - public enum Kind - { - Single, - Array, - Dictionary - } - - private readonly Kind _kind; - private readonly object _value; - - Kind IComplexUnion.ValueKind => _kind; - - object IComplexUnion.Value => _value; - - private BucketsPath(Kind kind, object value) - { - _kind = kind; - _value = value; - } - - public static BucketsPath Single(string single) => new(Kind.Single, single); - - public bool IsSingle => _kind == Kind.Single; - - public bool TryGetSingle([NotNullWhen(true)] out string? single) - { - single = null; - if (_kind == Kind.Single) - { - single = (string)_value; - return true; - } - - return false; - } - - public static implicit operator BucketsPath(string single) => BucketsPath.Single(single); - - public static BucketsPath Array(string[] array) => new(Kind.Array, array); - - public bool IsArray => _kind == Kind.Array; - - public bool TryGetArray([NotNullWhen(true)] out string[]? array) - { - array = null; - if (_kind == Kind.Array) - { - array = (string[])_value; - return true; - } - - return false; - } - - public static implicit operator BucketsPath(string[] array) => BucketsPath.Array(array); - - public static BucketsPath Dictionary(Dictionary dictionary) => new(Kind.Dictionary, dictionary); - - public bool IsDictionary => _kind == Kind.Dictionary; - - public bool TryGetDictionary([NotNullWhen(true)] out Dictionary? dictionary) - { - dictionary = null; - if (_kind == Kind.Dictionary) - { - dictionary = (Dictionary)_value; - return true; - } - - return false; - } - - public static implicit operator BucketsPath(Dictionary dictionary) => BucketsPath.Dictionary(dictionary); -} - -internal sealed class BucketsPathConverter : MultiItemUnionConverter -{ - public BucketsPathConverter() - { - _types = new Dictionary { { BucketsPath.Kind.Single, typeof(string) }, { BucketsPath.Kind.Array, typeof(IReadOnlyCollection) }, { BucketsPath.Kind.Dictionary, typeof(IReadOnlyDictionary) } }; - _factories = new Dictionary> { { typeof(string), o => BucketsPath.Single((string)o) }, { typeof(string[]), o => BucketsPath.Array((string[])o) }, { typeof(Dictionary), o => BucketsPath.Dictionary((Dictionary)o) } }; - _uniquePropertyToType = new Dictionary { { "", typeof(string) }, { "", typeof(IReadOnlyCollection) }, { "", typeof(IReadOnlyDictionary) } }; - } -} \ No newline at end of file diff --git a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/BucketsPath.g.cs b/src/Elastic.Clients.Elasticsearch.Shared/Types/Aggregations/BucketsPath.cs similarity index 54% rename from src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/BucketsPath.g.cs rename to src/Elastic.Clients.Elasticsearch.Shared/Types/Aggregations/BucketsPath.cs index e5a53d02f40..d01f5260ce8 100644 --- a/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/BucketsPath.g.cs +++ b/src/Elastic.Clients.Elasticsearch.Shared/Types/Aggregations/BucketsPath.cs @@ -1,34 +1,26 @@ // 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. -// -// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗ -// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝ -// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗ -// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝ -// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗ -// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝ -// ------------------------------------------------ -// -// This file is automatically generated. -// Please do not edit these files manually. -// -// ------------------------------------------------ - -#nullable restore +#nullable enable + +#if ELASTICSEARCH_SERVERLESS +using Elastic.Clients.Elasticsearch.Serverless.Core; +#else using Elastic.Clients.Elasticsearch.Core; -using Elastic.Clients.Elasticsearch.Fluent; -using Elastic.Clients.Elasticsearch.Serialization; -using Elastic.Transport; +#endif + using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq.Expressions; using System.Text.Json; using System.Text.Json.Serialization; +#if ELASTICSEARCH_SERVERLESS +namespace Elastic.Clients.Elasticsearch.Serverless.Aggregations; +#else namespace Elastic.Clients.Elasticsearch.Aggregations; +#endif /// /// Buckets path can be expressed in different ways, and an aggregation may accept some or all of these
forms depending on its type. Please refer to each aggregation's documentation to know what buckets
path forms they accept.
@@ -43,8 +35,8 @@ public enum Kind Dictionary } - private readonly Kind _kind; - private readonly object _value; + internal readonly Kind _kind; + internal readonly object _value; Kind IComplexUnion.ValueKind => _kind; @@ -111,12 +103,36 @@ public bool TryGetDictionary([NotNullWhen(true)] out Dictionary? public static implicit operator BucketsPath(Dictionary dictionary) => BucketsPath.Dictionary(dictionary); } -internal sealed class BucketsPathConverter : MultiItemUnionConverter +internal sealed class BucketsPathConverter : JsonConverter { - public BucketsPathConverter() + public override BucketsPath? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => + (reader.TokenType) switch + { + JsonTokenType.Null => null, + JsonTokenType.String => BucketsPath.Single(JsonSerializer.Deserialize(ref reader, options)!), + JsonTokenType.StartArray => BucketsPath.Array(JsonSerializer.Deserialize(ref reader, options)!), + JsonTokenType.StartObject => BucketsPath.Dictionary(JsonSerializer.Deserialize>(ref reader, options)!), + _ => throw new JsonException($"Unexpected token '{reader.TokenType}'.") + }; + + public override void Write(Utf8JsonWriter writer, BucketsPath value, JsonSerializerOptions options) { - _types = new Dictionary { { BucketsPath.Kind.Single, typeof(string) }, { BucketsPath.Kind.Array, typeof(IReadOnlyCollection) }, { BucketsPath.Kind.Dictionary, typeof(IReadOnlyDictionary) } }; - _factories = new Dictionary> { { typeof(string), o => BucketsPath.Single((string)o) }, { typeof(string[]), o => BucketsPath.Array((string[])o) }, { typeof(Dictionary), o => BucketsPath.Dictionary((Dictionary)o) } }; - _uniquePropertyToType = new Dictionary { { "", typeof(string) }, { "", typeof(IReadOnlyCollection) }, { "", typeof(IReadOnlyDictionary) } }; + switch (value._kind) + { + case BucketsPath.Kind.Single: + writer.WriteStringValue((string)value._value); + break; + + case BucketsPath.Kind.Array: + JsonSerializer.Serialize(writer, (string[])value._value, options); + break; + + case BucketsPath.Kind.Dictionary: + JsonSerializer.Serialize(writer, (Dictionary)value._value, options); + break; + + default: + throw new ArgumentOutOfRangeException(); + } } -} \ No newline at end of file +}