Skip to content

Commit 9f65cb5

Browse files
committed
fix #3544 JsonNetSerializer now also implements IPropertyMappingProvi… (#3545)
* fix #3544 JsonNetSerializer now also implements IPropertyMappingProvider making it easier to bootstrap * extended unit test for property resolving with custom source serializer with a Field resolution assertion for completeness sake (cherry picked from commit 185e711)
1 parent 10ecf29 commit 9f65cb5

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ IPropertyMappingProvider propertyMappingProvider
8888
var defaultSerializer = new InternalSerializer(this);
8989
_sourceSerializer = sourceSerializerFactory?.Invoke(defaultSerializer, this) ?? defaultSerializer;
9090
UseThisRequestResponseSerializer = defaultSerializer;
91-
_propertyMappingProvider = propertyMappingProvider ?? new PropertyMappingProvider();
91+
var serializerAsMappingProvider = _sourceSerializer as IPropertyMappingProvider;
92+
_propertyMappingProvider = propertyMappingProvider ?? serializerAsMappingProvider ?? new PropertyMappingProvider();
9293

9394
_defaultTypeNameInferrer = t => !_defaultTypeName.IsNullOrEmpty() ? _defaultTypeName : t.Name.ToLowerInvariant();
9495
_defaultFieldNameInferrer = p => p.ToCamelCase();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.IO;
4+
using System.Reflection;
5+
using System.Runtime.Serialization;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Elasticsearch.Net;
10+
using Newtonsoft.Json;
11+
12+
namespace Nest.JsonNetSerializer
13+
{
14+
public abstract partial class ConnectionSettingsAwareSerializerBase : IPropertyMappingProvider
15+
{
16+
protected readonly ConcurrentDictionary<string, IPropertyMapping> Properties = new ConcurrentDictionary<string, IPropertyMapping>();
17+
18+
public IPropertyMapping CreatePropertyMapping(MemberInfo memberInfo)
19+
{
20+
var memberInfoString = $"{memberInfo.DeclaringType?.FullName}.{memberInfo.Name}";
21+
if (Properties.TryGetValue(memberInfoString, out var mapping)) return mapping;
22+
mapping = FromAttributes(memberInfo);
23+
24+
Properties.TryAdd(memberInfoString, mapping);
25+
return mapping;
26+
}
27+
28+
private static IPropertyMapping FromAttributes(MemberInfo memberInfo)
29+
{
30+
var jsonProperty = memberInfo.GetCustomAttribute<JsonPropertyAttribute>(true);
31+
var dataMemberProperty = memberInfo.GetCustomAttribute<DataMemberAttribute>(true);
32+
var propertyName = memberInfo.GetCustomAttribute<PropertyNameAttribute>(true);
33+
var ignore = memberInfo.GetCustomAttribute<IgnoreAttribute>(true);
34+
var jsonIgnore = memberInfo.GetCustomAttribute<JsonIgnoreAttribute>(true);
35+
if (jsonProperty == null && ignore == null && propertyName == null && dataMemberProperty == null && jsonIgnore == null) return null;
36+
37+
return new PropertyMapping
38+
{ Name = propertyName?.Name ?? jsonProperty?.PropertyName ?? dataMemberProperty?.Name, Ignore = ignore != null || jsonIgnore != null };
39+
}
40+
}
41+
}

src/Tests/Tests/ClientConcepts/HighLevel/Inference/FieldInference.doc.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Elasticsearch.Net;
99
using FluentAssertions;
1010
using Nest;
11+
using Nest.JsonNetSerializer;
1112
using Newtonsoft.Json;
1213
using Tests.Core.Client;
1314
using Tests.Core.Client.Settings;
@@ -564,5 +565,42 @@ public void CodeBasedConfigurationInherits()
564565
});
565566

566567
}
568+
569+
570+
public class SourceModel
571+
{
572+
[PropertyName("gexo")]
573+
public GeoModel Geo { get; set; }
574+
}
575+
576+
public class GeoModel
577+
{
578+
[JsonProperty("country_iso_code")]
579+
public string CountryIsoCode { get; set; }
580+
}
581+
582+
/// this tests runs both during development as wel as during canary builds which reference
583+
/// all the artifacts as nuget packages instead of project references
584+
[U]
585+
public void JsonPropertyIsPickedWhenSerializerIsInternalized()
586+
{
587+
var usingSettings = WithConnectionSettings(s => s)
588+
.WithSourceSerializer(JsonNetSerializer.Default);
589+
590+
usingSettings.Expect("gexo").ForField(Field<SourceModel>(p=>p.Geo));
591+
usingSettings.Expect("country_iso_code").ForField(Field<GeoModel>(p=>p.CountryIsoCode));
592+
usingSettings.Expect(new []
593+
{
594+
"country_iso_code",
595+
}).AsPropertiesOf(new GeoModel { CountryIsoCode = "nl" });
596+
usingSettings.Expect(new []
597+
{
598+
"gexo",
599+
}).AsPropertiesOf(new SourceModel { Geo = new GeoModel { CountryIsoCode = "nl" } });
600+
601+
usingSettings.Expect("gexo.country_iso_code").ForField(Field<SourceModel>(p=>p.Geo.CountryIsoCode));
602+
}
603+
604+
567605
}
568606
}

0 commit comments

Comments
 (0)