Skip to content

Commit

Permalink
OData#277 change contract of IPropertyMapper.MapProperty, so it can n…
Browse files Browse the repository at this point in the history
…ow return null if the field should not be serialized at all (as in, ignored)
  • Loading branch information
mattperdeck committed Oct 27, 2022
1 parent 128b5b4 commit 85dce73
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6822,7 +6822,7 @@
</member>
<member name="P:Microsoft.AspNetCore.OData.SRResources.InvalidPropertyMapping">
<summary>
Looks up a localized string similar to The key mapping for the property &apos;{0}&apos; can&apos;t be null or empty..
Looks up a localized string similar to The key mapping for the property &apos;{0}&apos; can&apos;t be empty..
</summary>
</member>
<member name="P:Microsoft.AspNetCore.OData.SRResources.InvalidSegmentInSelectExpandPath">
Expand Down Expand Up @@ -7792,6 +7792,9 @@
properties in the <see cref="T:Microsoft.OData.Edm.IEdmStructuredType"/> 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 <see cref="T:System.Collections.Generic.IDictionary`2"/>
does not have a key for that field), by mapping the property name to null.
</summary>
</member>
<member name="M:Microsoft.AspNetCore.OData.Query.Container.IPropertyMapper.MapProperty(System.String)">
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.OData/Properties/SRResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@
<value>A binary operator with incompatible types was detected. Found operand types '{0}' and '{1}' for operator kind '{2}'.</value>
</data>
<data name="InvalidPropertyMapping" xml:space="preserve">
<value>The key mapping for the property '{0}' can't be null or empty.</value>
<value>The key mapping for the property '{0}' can't be empty.</value>
</data>
<data name="ODataFunctionNotSupported" xml:space="preserve">
<value>Unknown function '{0}'.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace Microsoft.AspNetCore.OData.Query.Container
/// properties in the <see cref="IEdmStructuredType"/> 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 <see cref="IDictionary{TKey,TValue}"/>
/// does not have a key for that field), by mapping the property name to null.
/// </summary>
public interface IPropertyMapper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ public override void ToDictionaryCore(Dictionary<string, object> 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());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,15 @@ public IDictionary<string, object> ToDictionary(Func<IEdmModel, IEdmStructuredTy
if (TryGetPropertyValue(property.Name, out propertyValue))
{
string mappingName = mapper.MapProperty(property.Name);
if (String.IsNullOrWhiteSpace(mappingName))
if (mappingName != null)
{
throw Error.InvalidOperation(SRResources.InvalidPropertyMapping, property.Name);
}
if (String.IsNullOrWhiteSpace(mappingName))
{
throw Error.InvalidOperation(SRResources.InvalidPropertyMapping, property.Name);
}

dictionary[mappingName] = propertyValue;
dictionary[mappingName] = propertyValue;
}
}
}
}
Expand Down

0 comments on commit 85dce73

Please sign in to comment.