Skip to content

Commit 185e711

Browse files
authored
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
1 parent 3ac2e39 commit 185e711

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;
@@ -563,5 +564,42 @@ public void CodeBasedConfigurationInherits()
563564
});
564565

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

0 commit comments

Comments
 (0)