From b6aa017ece27109267b791435c46580d69882bb1 Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Mon, 30 Sep 2019 11:54:18 +1000 Subject: [PATCH] Honour PropertyNameAttribute Ignore property in mappings Fixes #4105 --- .../SerializationBehavior/PropertyMapping.cs | 5 ++++- .../HighLevel/Mapping/IgnoringProperties.doc.cs | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Nest/CommonAbstractions/SerializationBehavior/PropertyMapping.cs b/src/Nest/CommonAbstractions/SerializationBehavior/PropertyMapping.cs index e78e23fccab..f3d0db0c97c 100644 --- a/src/Nest/CommonAbstractions/SerializationBehavior/PropertyMapping.cs +++ b/src/Nest/CommonAbstractions/SerializationBehavior/PropertyMapping.cs @@ -65,7 +65,10 @@ private static IPropertyMapping PropertyMappingFromAttributes(MemberInfo memberI if (ignore == null && propertyName == null && dataMemberProperty == null) return null; return new PropertyMapping - { Name = propertyName?.Name ?? dataMemberProperty?.Name, Ignore = ignore != null }; + { + Name = propertyName?.Name ?? dataMemberProperty?.Name, + Ignore = ignore != null || propertyName != null && propertyName.Ignore + }; } } } diff --git a/src/Tests/Tests/ClientConcepts/HighLevel/Mapping/IgnoringProperties.doc.cs b/src/Tests/Tests/ClientConcepts/HighLevel/Mapping/IgnoringProperties.doc.cs index fb4bc8242e8..ce05b649591 100644 --- a/src/Tests/Tests/ClientConcepts/HighLevel/Mapping/IgnoringProperties.doc.cs +++ b/src/Tests/Tests/ClientConcepts/HighLevel/Mapping/IgnoringProperties.doc.cs @@ -16,9 +16,11 @@ namespace Tests.ClientConcepts.HighLevel.Mapping * === Ignoring properties * Properties on a POCO can be ignored for mapping purposes in a few ways: * - * - Using the `Ignore` property on a derived `ElasticsearchPropertyAttribute` type applied to + * - Using the `Ignore` property on a derived `ElasticsearchPropertyAttributeBase` type, such as `TextAttribute`, applied to * the property that should be ignored on the POCO * + * - Using the `Ignore` property on `PropertyNameAttribute` applied to a property that should be ignored on the POCO + * * - Using the `.DefaultMappingFor(Func, IClrTypeMapping> * selector)` on `ConnectionSettings` * @@ -41,8 +43,11 @@ public class CompanyWithAttributesAndPropertiesToIgnore [Text(Ignore = true)] public string PropertyToIgnore { get; set; } + [PropertyName("anotherPropertyToIgnore", Ignore = true)] public string AnotherPropertyToIgnore { get; set; } + public string FluentMappingPropertyToIgnore { get; set; } + [Ignore, JsonIgnore] public string JsonIgnoredProperty { get; set; } } @@ -54,7 +59,7 @@ public void Ignoring() var connectionSettings = new ConnectionSettings(new InMemoryConnection()) // <1> we're using an in-memory connection, but in your application, you'll want to use an `IConnection` that actually sends a request. .DisableDirectStreaming() // <2> we disable direct streaming here to capture the request and response bytes. In a production application, you would likely not call this as it adds overhead to each call. .DefaultMappingFor(m => m - .Ignore(p => p.AnotherPropertyToIgnore) + .Ignore(p => p.FluentMappingPropertyToIgnore) ); var client = new ElasticClient(connectionSettings);