Skip to content

Commit 276a1f0

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Fix dictionary key serialization for Name type (#6786)
* Fix dictionary key serialization for Name type * Fix BOM
1 parent 4f55044 commit 276a1f0

File tree

6 files changed

+112
-39
lines changed

6 files changed

+112
-39
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.Reflection;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
10+
namespace Elastic.Clients.Elasticsearch.Serialization;
11+
12+
internal sealed class EnumStructConverter<T> : JsonConverter<T> where T : new()
13+
{
14+
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
15+
{
16+
var value = reader.GetString();
17+
18+
var instance = (T)Activator.CreateInstance(
19+
typeof(T),
20+
BindingFlags.Instance | BindingFlags.NonPublic,
21+
args: new object[] { value }, // TODO: Perf - Review use of ArrayPool
22+
binder: null,
23+
culture: null)!;
24+
25+
return instance;
26+
}
27+
28+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
29+
{
30+
var enumValue = value.ToString();
31+
32+
if (!string.IsNullOrEmpty(enumValue))
33+
writer.WriteStringValue(value.ToString());
34+
else
35+
writer.WriteNullValue();
36+
}
37+
}

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

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
namespace Elastic.Clients.Elasticsearch.Serialization;
1111

12+
// TODO - In .NET 7 we could review supporting IParsable as a type constraint?
1213
internal sealed class StringAliasConverter<T> : JsonConverter<T>
1314
{
15+
public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => Read(ref reader, typeToConvert, options);
16+
17+
public override void WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options) => writer.WritePropertyName(value.ToString());
18+
1419
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1520
{
1621
var value = reader.GetString();
@@ -37,30 +42,3 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
3742
}
3843
}
3944
}
40-
41-
internal sealed class EnumStructConverter<T> : JsonConverter<T> where T : new()
42-
{
43-
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
44-
{
45-
var value = reader.GetString();
46-
47-
var instance = (T)Activator.CreateInstance(
48-
typeof(T),
49-
BindingFlags.Instance | BindingFlags.NonPublic,
50-
args: new object[] { value }, // TODO: Perf - Review use of ArrayPool
51-
binder: null,
52-
culture: null)!;
53-
54-
return instance;
55-
}
56-
57-
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
58-
{
59-
var enumValue = value.ToString();
60-
61-
if (!string.IsNullOrEmpty(enumValue))
62-
writer.WriteStringValue(value.ToString());
63-
else
64-
writer.WriteNullValue();
65-
}
66-
}

src/Elastic.Clients.Elasticsearch/_Generated/Api/SearchResponse.g.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#nullable restore
2525
namespace Elastic.Clients.Elasticsearch;
26-
public sealed partial class SearchResponse<TDocument> : ElasticsearchResponseBase
26+
public partial class SearchResponse<TDocument> : ElasticsearchResponseBase
2727
{
2828
[JsonInclude]
2929
[JsonPropertyName("aggregations")]
@@ -76,4 +76,4 @@ public sealed partial class SearchResponse<TDocument> : ElasticsearchResponseBas
7676
[JsonInclude]
7777
[JsonPropertyName("took")]
7878
public long Took { get; init; }
79-
}
79+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Elastic.Clients.Elasticsearch.IndexManagement;
6+
using Elastic.Clients.Elasticsearch.QueryDsl;
7+
using System.Threading.Tasks;
8+
using Tests.Serialization;
9+
using VerifyXunit;
10+
11+
namespace Tests.IndexManagement;
12+
13+
[UsesVerify]
14+
public class CreateIndexSerializationTests : SerializerTestBase
15+
{
16+
[U]
17+
public async Task CreateIndexWithAliases_SerializesCorrectly()
18+
{
19+
var alias1 = new Alias();
20+
var alias2 = new Alias { Filter = QueryContainer.Term(new TermQuery("username") { Value = "stevegordon" }), Routing = "shard-1" };
21+
22+
var descriptor = new CreateRequestDescriptor("test")
23+
.Aliases(aliases => aliases
24+
.Add("alias_1", alias1)
25+
.Add("alias_2", alias2));
26+
27+
var json = await SerializeAndGetJsonStringAsync(descriptor);
28+
29+
await Verifier.VerifyJson(json);
30+
31+
var createRequest = new CreateRequest("test")
32+
{
33+
Aliases = new()
34+
{
35+
{ "alias_1", alias1 },
36+
{ "alias_2", alias2 }
37+
}
38+
};
39+
40+
var objectJson = await SerializeAndGetJsonStringAsync(createRequest);
41+
objectJson.Should().Be(json);
42+
}
43+
}

tests/Tests/IndexManagement/IndexSettingsSerializationTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public async Task CanSerializerIndexSettingsWithCustomAnalyzer()
2525
// Resolved after improved code-generation of internally-tagged untions to include
2626
// converters for the variant interfaces.
2727

28-
var descriptor = new IndexSettingsDescriptor<Project>();
29-
descriptor.Analysis(a => a
30-
.Analyzer(a => a
31-
.Custom("whitespace_lowercase", wl => wl
32-
.Tokenizer("whitespace")
33-
.Filter(Enumerable.Repeat("lowercase", 1))
34-
)
35-
)
28+
var descriptor = new IndexSettingsDescriptor<Project>()
29+
.Analysis(a => a
30+
.Analyzer(a => a
31+
.Custom("whitespace_lowercase", wl => wl
32+
.Tokenizer("whitespace")
33+
.Filter(Enumerable.Repeat("lowercase", 1))
34+
)
35+
)
3636
);
3737

38-
var json = SerializeAndGetJsonString(descriptor);
38+
var json = await SerializeAndGetJsonStringAsync(descriptor);
3939
await Verifier.VerifyJson(json);
4040

4141
var indexSettings = DeserializeJsonString<IndexSettings>(json);
@@ -44,7 +44,7 @@ public async Task CanSerializerIndexSettingsWithCustomAnalyzer()
4444
customAnalyzer.Tokenizer.Should().Be("whitespace");
4545
customAnalyzer.Filter.Should().ContainSingle("lowercase");
4646

47-
var objectJson = SerializeAndGetJsonString(indexSettings);
47+
var objectJson = await SerializeAndGetJsonStringAsync(indexSettings);
4848
objectJson.Should().Be(json);
4949
}
5050

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
aliases: {
3+
alias_1: {},
4+
alias_2: {
5+
filter: {
6+
term: {
7+
username: {
8+
value: stevegordon
9+
}
10+
}
11+
},
12+
routing: shard-1
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)