Skip to content

Commit 925ba84

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Fix field name query converter (#6569)
* Add ConditionalWeakTable to client * Fix FieldNameQueryConverterBase serialization
1 parent da10ca8 commit 925ba84

File tree

7 files changed

+81
-13
lines changed

7 files changed

+81
-13
lines changed

src/Elastic.Clients.Elasticsearch/Client/ElasticsearchClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
using System;
66
using System.Linq;
7+
using System.Runtime.CompilerServices;
8+
using System.Text.Json;
79
using System.Threading;
810
using System.Threading.Tasks;
911
using Elastic.Transport;
@@ -15,6 +17,8 @@ public sealed partial class ElasticsearchClient
1517
{
1618
private readonly ITransport<IElasticsearchClientSettings> _transport;
1719

20+
internal static ConditionalWeakTable<JsonSerializerOptions, IElasticsearchClientSettings> SettingsTable { get; } = new();
21+
1822
/// <summary>
1923
/// Creates a client configured to connect to localhost:9200.
2024
/// </summary>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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;
7+
8+
namespace Elastic.Clients.Elasticsearch;
9+
10+
internal class CustomizedNamingPolicy : JsonNamingPolicy
11+
{
12+
private readonly Func<string, string> _namingAction;
13+
14+
public CustomizedNamingPolicy(Func<string, string> namingAction) => _namingAction = namingAction;
15+
16+
public override string ConvertName(string name) => _namingAction(name);
17+
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313

1414
namespace Elastic.Clients.Elasticsearch;
1515

16-
internal class CustomizedNamingPolicy : JsonNamingPolicy
17-
{
18-
private readonly Func<string, string> _namingAction;
19-
20-
public CustomizedNamingPolicy(Func<string, string> namingAction) => _namingAction = namingAction;
21-
22-
public override string ConvertName(string name) => _namingAction(name);
23-
}
24-
2516
/// <summary>
2617
/// The built in internal serializer that the high level client Elastic.Clients.Elasticsearch uses.
2718
/// </summary>
@@ -65,6 +56,8 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
6556
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
6657
};
6758

59+
ElasticsearchClient.SettingsTable.Add(Options, settings);
60+
6861
_settings = settings;
6962
}
7063

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
4141
if (value.Field is null)
4242
writer.WriteNullValue();
4343

44-
writer.WriteStartObject();
45-
writer.WritePropertyName(value.Field.ToString());
46-
WriteInternal(writer, value, options);
47-
writer.WriteEndObject();
44+
if (options.TryGetClientSettings(out var settings))
45+
{
46+
writer.WriteStartObject();
47+
writer.WritePropertyName(settings.Inferrer.Field(value.Field));
48+
WriteInternal(writer, value, options);
49+
writer.WriteEndObject();
50+
return;
51+
}
52+
53+
throw new JsonException("Unable to retrieve client settings to infer field.");
4854
}
4955

5056
internal abstract T? ReadInternal(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.Text.Json;
6+
7+
namespace Elastic.Clients.Elasticsearch;
8+
9+
internal static class JsonSerializerOptionsExtensions
10+
{
11+
public static bool TryGetClientSettings(this JsonSerializerOptions options, out IElasticsearchClientSettings settings) =>
12+
ElasticsearchClient.SettingsTable.TryGetValue(options, out settings);
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.Threading.Tasks;
6+
using Elastic.Clients.Elasticsearch.QueryDsl;
7+
using Tests.Domain;
8+
using VerifyXunit;
9+
10+
namespace Tests.Serialization.Queries;
11+
12+
[UsesVerify]
13+
public class SearchSerializationTests : SerializerTestBase
14+
{
15+
[U]
16+
public async Task Search_WithMatchQuery_SerializesInferredField_ForObjectInitializer()
17+
{
18+
var container = QueryContainer.Match(new MatchQuery
19+
{
20+
Field = Infer.Field<Project>(d => d.Description),
21+
Query = "testing"
22+
});
23+
24+
var json = SerializeAndGetJsonString(container);
25+
26+
await Verifier.VerifyJson(json);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
match: {
3+
description: {
4+
query: testing
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)