Skip to content

Commit 8e6afad

Browse files
authored
Honour IPropertyMappingProvider on custom IElastisearchSerializer (#3942)
This commit fixes a bug introduced with Diagnostics Source support. DiagnosticsSerializerProxy does not implement IPropertyMappingProvider, so if it wraps and delegates to a serializer that does implement IPropertyMappingProvider, such as JsonNetSerialzer, this is not honoured when assigning to _propertyMappingProvider. This commit uses the sourceSerializer when determining _propertyMappingProvider, and not the DiagnosticsSerializerProxy. Fixes #3926
1 parent 11a2ab3 commit 8e6afad

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ IPropertyMappingProvider propertyMappingProvider
8787
var formatterResolver = new NestFormatterResolver(this);
8888
var defaultSerializer = new DefaultHighLevelSerializer(formatterResolver);
8989
var sourceSerializer = sourceSerializerFactory?.Invoke(defaultSerializer, this) ?? defaultSerializer;
90-
91-
//We wrap these in an internal proxy to facilitate diagnostics
92-
_sourceSerializer = new DiagnosticsSerializerProxy(sourceSerializer, "source");
93-
UseThisRequestResponseSerializer = new DiagnosticsSerializerProxy(defaultSerializer);
94-
95-
var serializerAsMappingProvider = _sourceSerializer as IPropertyMappingProvider;
90+
var serializerAsMappingProvider = sourceSerializer as IPropertyMappingProvider;
91+
9692
_propertyMappingProvider = propertyMappingProvider ?? serializerAsMappingProvider ?? new PropertyMappingProvider();
9793

94+
//We wrap these in an internal proxy to facilitate serialization diagnostics
95+
_sourceSerializer = new DiagnosticsSerializerProxy(sourceSerializer, "source");
96+
UseThisRequestResponseSerializer = new DiagnosticsSerializerProxy(defaultSerializer);
9897
_defaultFieldNameInferrer = p => p.ToCamelCase();
9998
_defaultIndices = new FluentDictionary<Type, string>();
10099
_defaultRelationNames = new FluentDictionary<Type, string>();
@@ -294,14 +293,14 @@ public TConnectionSettings DefaultMappingFor(IEnumerable<IClrTypeMapping> typeMa
294293

295294
return (TConnectionSettings)this;
296295
}
297-
296+
298297
/// <summary>
299298
/// NEST handles 404 in its <see cref="ResponseBase.IsValid"/>, we do not want the low level client throwing exceptions
300299
/// when <see cref="IConnectionConfigurationValues.ThrowExceptions"/> is enabled for 404's. The client is in charge of composing paths
301300
/// so a 404 never signals a wrong url but a missing entity.
302301
/// </summary>
303-
protected override bool HttpStatusCodeClassifier(HttpMethod method, int statusCode) =>
302+
protected override bool HttpStatusCodeClassifier(HttpMethod method, int statusCode) =>
304303
statusCode >= 200 && statusCode < 300
305-
|| statusCode == 404;
304+
|| statusCode == 404;
306305
}
307306
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Text;
3+
using Elastic.Xunit.XunitPlumbing;
4+
using FluentAssertions;
5+
using Newtonsoft.Json;
6+
using Tests.Core.Client;
7+
8+
namespace Tests.Reproduce
9+
{
10+
public class GitHubIssue3926
11+
{
12+
public class DocumentModel
13+
{
14+
[JsonProperty("@timestamp")]
15+
public DateTime Timestamp { get; set; }
16+
17+
[JsonProperty("document_name")]
18+
public string DocumentName { get; set; }
19+
}
20+
21+
[U]
22+
public void FieldInferenceUsesJsonPropertyName()
23+
{
24+
var client = TestClient.InMemoryWithJsonNetSerializer;
25+
26+
var searchResponse = client.Search<DocumentModel>(descriptor => descriptor
27+
.Index("my-index")
28+
.Query(q => q
29+
.DateRange(range => range
30+
.Field(model => model.Timestamp)
31+
.GreaterThan("1970-01-01"))
32+
)
33+
);
34+
35+
Encoding.UTF8.GetString(searchResponse.ApiCall.RequestBodyInBytes).Should().Contain("@timestamp");
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)