From 85dce730b6d6b5cb6ef7219a471f750e1c672025 Mon Sep 17 00:00:00 2001 From: "DECIDEWARE\\mperdeck" Date: Thu, 27 Oct 2022 12:56:00 +1100 Subject: [PATCH] OData#277 change contract of IPropertyMapper.MapProperty, so it can now return null if the field should not be serialized at all (as in, ignored) --- .../Microsoft.AspNetCore.OData.xml | 5 ++++- .../Properties/SRResources.Designer.cs | 2 +- .../Properties/SRResources.resx | 2 +- .../Query/Container/IPropertyMapper.cs | 3 +++ .../Query/Container/NamedPropertyOfT.cs | 11 +++++++---- .../Query/Wrapper/SelectExpandWrapper.cs | 11 +++++++---- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml b/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml index 4d0e931a0..18808b539 100644 --- a/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml +++ b/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml @@ -6822,7 +6822,7 @@ - Looks up a localized string similar to The key mapping for the property '{0}' can't be null or empty.. + Looks up a localized string similar to The key mapping for the property '{0}' can't be empty.. @@ -7792,6 +7792,9 @@ properties in the that will be used during the serialization of the $select and $expand projection by a given formatter. For example, to support custom serialization attributes of a particular formatter. + + It also allows you to ignore a field (ensure that the returned + does not have a key for that field), by mapping the property name to null. diff --git a/src/Microsoft.AspNetCore.OData/Properties/SRResources.Designer.cs b/src/Microsoft.AspNetCore.OData/Properties/SRResources.Designer.cs index 31011acb1..38e667b70 100644 --- a/src/Microsoft.AspNetCore.OData/Properties/SRResources.Designer.cs +++ b/src/Microsoft.AspNetCore.OData/Properties/SRResources.Designer.cs @@ -880,7 +880,7 @@ internal static string InvalidPropertyMapper { } /// - /// Looks up a localized string similar to The key mapping for the property '{0}' can't be null or empty.. + /// Looks up a localized string similar to The key mapping for the property '{0}' can't be empty.. /// internal static string InvalidPropertyMapping { get { diff --git a/src/Microsoft.AspNetCore.OData/Properties/SRResources.resx b/src/Microsoft.AspNetCore.OData/Properties/SRResources.resx index e43617960..91a0110b8 100644 --- a/src/Microsoft.AspNetCore.OData/Properties/SRResources.resx +++ b/src/Microsoft.AspNetCore.OData/Properties/SRResources.resx @@ -496,7 +496,7 @@ A binary operator with incompatible types was detected. Found operand types '{0}' and '{1}' for operator kind '{2}'. - The key mapping for the property '{0}' can't be null or empty. + The key mapping for the property '{0}' can't be empty. Unknown function '{0}'. diff --git a/src/Microsoft.AspNetCore.OData/Query/Container/IPropertyMapper.cs b/src/Microsoft.AspNetCore.OData/Query/Container/IPropertyMapper.cs index 2f349f28d..2ad722ac7 100644 --- a/src/Microsoft.AspNetCore.OData/Query/Container/IPropertyMapper.cs +++ b/src/Microsoft.AspNetCore.OData/Query/Container/IPropertyMapper.cs @@ -23,6 +23,9 @@ namespace Microsoft.AspNetCore.OData.Query.Container /// properties in the that will be used during the serialization of the $select /// and $expand projection by a given formatter. For example, to support custom serialization attributes of a /// particular formatter. + /// + /// It also allows you to ignore a field (ensure that the returned + /// does not have a key for that field), by mapping the property name to null. /// public interface IPropertyMapper { diff --git a/src/Microsoft.AspNetCore.OData/Query/Container/NamedPropertyOfT.cs b/src/Microsoft.AspNetCore.OData/Query/Container/NamedPropertyOfT.cs index 1fc96e4f0..a8de825cd 100644 --- a/src/Microsoft.AspNetCore.OData/Query/Container/NamedPropertyOfT.cs +++ b/src/Microsoft.AspNetCore.OData/Query/Container/NamedPropertyOfT.cs @@ -27,12 +27,15 @@ public override void ToDictionaryCore(Dictionary dictionary, IPr if (Name != null && (includeAutoSelected || !AutoSelected)) { string mappedName = propertyMapper.MapProperty(Name); - if (String.IsNullOrEmpty(mappedName)) + if (mappedName != null) { - throw Error.InvalidOperation(SRResources.InvalidPropertyMapping, Name); - } + if (String.IsNullOrEmpty(mappedName)) + { + throw Error.InvalidOperation(SRResources.InvalidPropertyMapping, Name); + } - dictionary.Add(mappedName, GetValue()); + dictionary.Add(mappedName, GetValue()); + } } } diff --git a/src/Microsoft.AspNetCore.OData/Query/Wrapper/SelectExpandWrapper.cs b/src/Microsoft.AspNetCore.OData/Query/Wrapper/SelectExpandWrapper.cs index d74171d1a..194155a62 100644 --- a/src/Microsoft.AspNetCore.OData/Query/Wrapper/SelectExpandWrapper.cs +++ b/src/Microsoft.AspNetCore.OData/Query/Wrapper/SelectExpandWrapper.cs @@ -144,12 +144,15 @@ public IDictionary ToDictionary(Func