Skip to content

Commit be40b2a

Browse files
Fix code-gen for single or many types. (#7244) (#7245)
* Add test for GetIndex response deserialization * Add missing implementation for union converter * Regenerate after code generator improvements JSON serializer attributes are now included on a single line after some recent codegenerator changes to support conditional attributes. * Formatting updates after updating Roslyn packages * Add number handling options to serializers * After fixed codegen for DynamicTemplate * Remove special handling for sort converter in generator Co-authored-by: Steve Gordon <[email protected]>
1 parent 4f6a96d commit be40b2a

File tree

615 files changed

+12762
-6784
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

615 files changed

+12762
-6784
lines changed

src/Elastic.Clients.Elasticsearch/Serialization/DefaultRequestResponseSerializer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
5252
new ExtraSerializationData(settings),
5353
new DictionaryResponseConverterFactory(settings)
5454
},
55-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
55+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
56+
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals
5657
};
5758

5859
ElasticsearchClient.SettingsTable.Add(Options, settings);

src/Elastic.Clients.Elasticsearch/Serialization/DefaultSourceSerializer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public DefaultSourceSerializer(IElasticsearchClientSettings settings, JsonSerial
2424
new LazyJsonConverter(settings),
2525
new IdsConverter(settings)
2626
},
27-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
27+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
28+
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals
2829
};
2930
}

src/Elastic.Clients.Elasticsearch/Serialization/FieldNameQueryAttribute.cs

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Text.Json.Serialization;
7+
8+
namespace Elastic.Clients.Elasticsearch.Serialization;
9+
10+
[AttributeUsage(AttributeTargets.Property)]
11+
internal class SingleOrManyCollectionConverterAttribute : JsonConverterAttribute
12+
{
13+
public SingleOrManyCollectionConverterAttribute(Type convertType) => ConvertType = convertType;
14+
15+
public Type ConvertType { get; }
16+
17+
public override JsonConverter? CreateConverter(Type typeToConvert) => (JsonConverter)Activator.CreateInstance(typeof(SingleOrManyCollectionConverter<>).MakeGenericType(ConvertType));
18+
}

src/Elastic.Clients.Elasticsearch/Serialization/IEnumerableSingleOrManyConverter.cs renamed to src/Elastic.Clients.Elasticsearch/Serialization/SingleOrManyCollectionConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Elastic.Clients.Elasticsearch.Serialization;
1111

12-
internal abstract class ICollectionSingleOrManyConverter<TItem> : JsonConverter<ICollection<TItem>>
12+
internal class SingleOrManyCollectionConverter<TItem> : JsonConverter<ICollection<TItem>>
1313
{
1414
public override ICollection<TItem>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
1515
SingleOrManySerializationHelper.Deserialize<TItem>(ref reader, options);

src/Elastic.Clients.Elasticsearch/Serialization/UnionConverter.cs

+43-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,49 @@ public override void Write(Utf8JsonWriter writer, TType value,
147147
private class UnionConverterInner<TItem1, TItem2> : JsonConverter<Union<TItem1, TItem2>>
148148
{
149149
public override Union<TItem1, TItem2>? Read(ref Utf8JsonReader reader, Type typeToConvert,
150-
JsonSerializerOptions options) => null;
150+
JsonSerializerOptions options)
151+
{
152+
var readerCopy = reader;
153+
154+
try
155+
{
156+
var itemOne = JsonSerializer.Deserialize<TItem1>(ref readerCopy, options);
157+
158+
if (itemOne is IUnionVerifiable verifiable)
159+
{
160+
if (verifiable.IsSuccessful)
161+
{
162+
reader = readerCopy;
163+
return (TItem1)Activator.CreateInstance(typeof(TItem1), itemOne);
164+
}
165+
}
166+
else if (itemOne is not null)
167+
{
168+
reader = readerCopy;
169+
return (TItem1)Activator.CreateInstance(typeof(TItem1), itemOne);
170+
}
171+
}
172+
catch
173+
{
174+
// TODO - Store for aggregate exception
175+
}
176+
177+
try
178+
{
179+
var itemTwo = JsonSerializer.Deserialize<TItem2>(ref reader, options);
180+
181+
if (itemTwo is not null)
182+
{
183+
return (TItem2)Activator.CreateInstance(typeof(TItem2), itemTwo);
184+
}
185+
}
186+
catch
187+
{
188+
// TODO - Store for aggregate exception
189+
}
190+
191+
throw new JsonException("Unable to deserialize union."); // TODO - Add inner aggregate exception.
192+
}
151193

152194
public override void Write(Utf8JsonWriter writer, Union<TItem1, TItem2> value,
153195
JsonSerializerOptions options)

src/Elastic.Clients.Elasticsearch/Types/SortConverter.cs

-11
This file was deleted.

src/Elastic.Clients.Elasticsearch/_Generated/Api/AsyncSearch/AsyncSearchStatusResponse.g.cs

+9-18
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,30 @@
2626
namespace Elastic.Clients.Elasticsearch.AsyncSearch;
2727
public sealed partial class AsyncSearchStatusResponse : ElasticsearchResponse
2828
{
29-
[JsonInclude]
30-
[JsonPropertyName("completion_status")]
29+
[JsonInclude, JsonPropertyName("completion_status")]
3130
public int? CompletionStatus { get; init; }
3231

33-
[JsonInclude]
34-
[JsonPropertyName("expiration_time")]
32+
[JsonInclude, JsonPropertyName("expiration_time")]
3533
public DateTimeOffset? ExpirationTime { get; init; }
3634

37-
[JsonInclude]
38-
[JsonPropertyName("expiration_time_in_millis")]
35+
[JsonInclude, JsonPropertyName("expiration_time_in_millis")]
3936
public long ExpirationTimeInMillis { get; init; }
4037

41-
[JsonInclude]
42-
[JsonPropertyName("id")]
38+
[JsonInclude, JsonPropertyName("id")]
4339
public string? Id { get; init; }
4440

45-
[JsonInclude]
46-
[JsonPropertyName("is_partial")]
41+
[JsonInclude, JsonPropertyName("is_partial")]
4742
public bool IsPartial { get; init; }
4843

49-
[JsonInclude]
50-
[JsonPropertyName("is_running")]
44+
[JsonInclude, JsonPropertyName("is_running")]
5145
public bool IsRunning { get; init; }
5246

53-
[JsonInclude]
54-
[JsonPropertyName("_shards")]
47+
[JsonInclude, JsonPropertyName("_shards")]
5548
public Elastic.Clients.Elasticsearch.ShardStatistics Shards { get; init; }
5649

57-
[JsonInclude]
58-
[JsonPropertyName("start_time")]
50+
[JsonInclude, JsonPropertyName("start_time")]
5951
public DateTimeOffset? StartTime { get; init; }
6052

61-
[JsonInclude]
62-
[JsonPropertyName("start_time_in_millis")]
53+
[JsonInclude, JsonPropertyName("start_time_in_millis")]
6354
public long StartTimeInMillis { get; init; }
6455
}

src/Elastic.Clients.Elasticsearch/_Generated/Api/AsyncSearch/DeleteAsyncSearchResponse.g.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
namespace Elastic.Clients.Elasticsearch.AsyncSearch;
2626
public sealed partial class DeleteAsyncSearchResponse : ElasticsearchResponse
2727
{
28-
[JsonInclude]
29-
[JsonPropertyName("acknowledged")]
28+
[JsonInclude, JsonPropertyName("acknowledged")]
3029
public bool Acknowledged { get; init; }
3130
}

src/Elastic.Clients.Elasticsearch/_Generated/Api/AsyncSearch/GetAsyncSearchResponse.g.cs

+8-16
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,27 @@
2626
namespace Elastic.Clients.Elasticsearch.AsyncSearch;
2727
public sealed partial class GetAsyncSearchResponse<TDocument> : ElasticsearchResponse
2828
{
29-
[JsonInclude]
30-
[JsonPropertyName("expiration_time")]
29+
[JsonInclude, JsonPropertyName("expiration_time")]
3130
public DateTimeOffset? ExpirationTime { get; init; }
3231

33-
[JsonInclude]
34-
[JsonPropertyName("expiration_time_in_millis")]
32+
[JsonInclude, JsonPropertyName("expiration_time_in_millis")]
3533
public long ExpirationTimeInMillis { get; init; }
3634

37-
[JsonInclude]
38-
[JsonPropertyName("id")]
35+
[JsonInclude, JsonPropertyName("id")]
3936
public string? Id { get; init; }
4037

41-
[JsonInclude]
42-
[JsonPropertyName("is_partial")]
38+
[JsonInclude, JsonPropertyName("is_partial")]
4339
public bool IsPartial { get; init; }
4440

45-
[JsonInclude]
46-
[JsonPropertyName("is_running")]
41+
[JsonInclude, JsonPropertyName("is_running")]
4742
public bool IsRunning { get; init; }
4843

49-
[JsonInclude]
50-
[JsonPropertyName("response")]
44+
[JsonInclude, JsonPropertyName("response")]
5145
public Elastic.Clients.Elasticsearch.AsyncSearch.AsyncSearch<TDocument> Response { get; init; }
5246

53-
[JsonInclude]
54-
[JsonPropertyName("start_time")]
47+
[JsonInclude, JsonPropertyName("start_time")]
5548
public DateTimeOffset? StartTime { get; init; }
5649

57-
[JsonInclude]
58-
[JsonPropertyName("start_time_in_millis")]
50+
[JsonInclude, JsonPropertyName("start_time_in_millis")]
5951
public long StartTimeInMillis { get; init; }
6052
}

0 commit comments

Comments
 (0)