Skip to content

Fix dictionary key serialization for Name type #6786

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 2 commits into from
Oct 17, 2022
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
@@ -0,0 +1,37 @@
// 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.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serialization;

internal sealed class EnumStructConverter<T> : JsonConverter<T> where T : new()
{
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();

var instance = (T)Activator.CreateInstance(
typeof(T),
BindingFlags.Instance | BindingFlags.NonPublic,
args: new object[] { value }, // TODO: Perf - Review use of ArrayPool
binder: null,
culture: null)!;

return instance;
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
var enumValue = value.ToString();

if (!string.IsNullOrEmpty(enumValue))
writer.WriteStringValue(value.ToString());
else
writer.WriteNullValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

namespace Elastic.Clients.Elasticsearch.Serialization;

// TODO - In .NET 7 we could review supporting IParsable as a type constraint?
internal sealed class StringAliasConverter<T> : JsonConverter<T>
{
public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => Read(ref reader, typeToConvert, options);

public override void WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options) => writer.WritePropertyName(value.ToString());

public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
Expand All @@ -37,30 +42,3 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
}
}
}

internal sealed class EnumStructConverter<T> : JsonConverter<T> where T : new()
{
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();

var instance = (T)Activator.CreateInstance(
typeof(T),
BindingFlags.Instance | BindingFlags.NonPublic,
args: new object[] { value }, // TODO: Perf - Review use of ArrayPool
binder: null,
culture: null)!;

return instance;
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
var enumValue = value.ToString();

if (!string.IsNullOrEmpty(enumValue))
writer.WriteStringValue(value.ToString());
else
writer.WriteNullValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#nullable restore
namespace Elastic.Clients.Elasticsearch;
public sealed partial class SearchResponse<TDocument> : ElasticsearchResponseBase
public partial class SearchResponse<TDocument> : ElasticsearchResponseBase
{
[JsonInclude]
[JsonPropertyName("aggregations")]
Expand Down Expand Up @@ -76,4 +76,4 @@ public sealed partial class SearchResponse<TDocument> : ElasticsearchResponseBas
[JsonInclude]
[JsonPropertyName("took")]
public long Took { get; init; }
}
}
43 changes: 43 additions & 0 deletions tests/Tests/IndexManagement/CreateIndexSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 Elastic.Clients.Elasticsearch.IndexManagement;
using Elastic.Clients.Elasticsearch.QueryDsl;
using System.Threading.Tasks;
using Tests.Serialization;
using VerifyXunit;

namespace Tests.IndexManagement;

[UsesVerify]
public class CreateIndexSerializationTests : SerializerTestBase
{
[U]
public async Task CreateIndexWithAliases_SerializesCorrectly()
{
var alias1 = new Alias();
var alias2 = new Alias { Filter = QueryContainer.Term(new TermQuery("username") { Value = "stevegordon" }), Routing = "shard-1" };

var descriptor = new CreateRequestDescriptor("test")
.Aliases(aliases => aliases
.Add("alias_1", alias1)
.Add("alias_2", alias2));

var json = await SerializeAndGetJsonStringAsync(descriptor);

await Verifier.VerifyJson(json);

var createRequest = new CreateRequest("test")
{
Aliases = new()
{
{ "alias_1", alias1 },
{ "alias_2", alias2 }
}
};

var objectJson = await SerializeAndGetJsonStringAsync(createRequest);
objectJson.Should().Be(json);
}
}
20 changes: 10 additions & 10 deletions tests/Tests/IndexManagement/IndexSettingsSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public async Task CanSerializerIndexSettingsWithCustomAnalyzer()
// Resolved after improved code-generation of internally-tagged untions to include
// converters for the variant interfaces.

var descriptor = new IndexSettingsDescriptor<Project>();
descriptor.Analysis(a => a
.Analyzer(a => a
.Custom("whitespace_lowercase", wl => wl
.Tokenizer("whitespace")
.Filter(Enumerable.Repeat("lowercase", 1))
)
)
var descriptor = new IndexSettingsDescriptor<Project>()
.Analysis(a => a
.Analyzer(a => a
.Custom("whitespace_lowercase", wl => wl
.Tokenizer("whitespace")
.Filter(Enumerable.Repeat("lowercase", 1))
)
)
);

var json = SerializeAndGetJsonString(descriptor);
var json = await SerializeAndGetJsonStringAsync(descriptor);
await Verifier.VerifyJson(json);

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

var objectJson = SerializeAndGetJsonString(indexSettings);
var objectJson = await SerializeAndGetJsonStringAsync(indexSettings);
objectJson.Should().Be(json);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
aliases: {
alias_1: {},
alias_2: {
filter: {
term: {
username: {
value: stevegordon
}
}
},
routing: shard-1
}
}
}