OData WebApi V4 .net - Custom Serialization
I found the solution. On the new versions all the serialization and deserialization customization is enabled only through dependency injection.
First we need to override the serialization provider:
/// <summary>
/// Provider that selects the IgnoreNullEntityPropertiesSerializer that omits null properties on resources from the response
/// </summary>
public class MySerializerProvider : DefaultODataSerializerProvider
{
private readonly IgnoreNullsSerializer _propertiesSerializer;
private readonly IgnoreEmptyListsResourceSetSerializer _ignoreEmptyListsSerializer;
private readonly IgnoreEmptyListsCollectionSerializer _ignoreEmptyListsCollectionSerializer;
/// <summary>
/// constructor
/// </summary>
/// <param name="rootContainer"></param>
public MySerializerProvider(IServiceProvider rootContainer)
: base(rootContainer)
{
_ignoreEmptyListsSerializer = new IgnoreEmptyListsResourceSetSerializer(this);
_propertiesSerializer = new IgnoreNullsSerializer(this);
_ignoreEmptyListsCollectionSerializer = new IgnoreEmptyListsCollectionSerializer(this);
}
/// <summary>
/// Mark edmtype to apply the serialization on
/// </summary>
/// <param name="edmType"></param>
/// <returns></returns>
public override ODataEdmTypeSerializer GetEdmTypeSerializer(Microsoft.OData.Edm.IEdmTypeReference edmType)
{
// Support for Entity types AND Complex types
if (edmType.Definition.TypeKind == EdmTypeKind.Entity || edmType.Definition.TypeKind == EdmTypeKind.Complex)
{
return _propertiesSerializer;
}
if (edmType.Definition.TypeKind == EdmTypeKind.Collection)
{
if(edmType.Definition.AsElementType().IsDecimal() || edmType.Definition.AsElementType().IsString())
return _ignoreEmptyListsCollectionSerializer;
return _ignoreEmptyListsSerializer;
}
var result = base.GetEdmTypeSerializer(edmType);
return result;
}
}
You might need to override different serializers based on the EdmType you want to override it's behavior.
I'm adding an example of a serializer that ignore empty lists from entities based on "HideEmptyLists" header on the request...
/// <inheritdoc />
/// <summary>
/// OData Entity Serializer that omits empty listss properties from the response
/// </summary>
public class IgnoreEmptyListsResourceSetSerializer : ODataResourceSetSerializer
{
/// <summary>
/// constructor
/// </summary>
/// <param name="provider"></param>
public IgnoreEmptyListsResourceSetSerializer(ODataSerializerProvider provider) : base(provider) { }
/// <inheritdoc />
public override void WriteObjectInline(object graph, IEdmTypeReference expectedType, ODataWriter writer,
ODataSerializerContext writeContext)
{
var shouldHideEmptyLists = writeContext.Request.GetHeader("HideEmptyLists");
if (shouldHideEmptyLists != null)
{
IEnumerable enumerable = graph as IEnumerable; // Data to serialize
if (enumerable.IsNullOrEmpty())
{
return;
//ignore
}
}
base.WriteObjectInline(graph, expectedType, writer, writeContext);
}
}
And another one to ignore empty list for collections...
/// <inheritdoc />
/// <summary>
/// OData Entity Serilizer that omits null properties from the response
/// </summary>
public class IgnoreEmptyListsCollectionSerializer : ODataCollectionSerializer
{
/// <summary>
/// constructor
/// </summary>
/// <param name="provider"></param>
public IgnoreEmptyListsCollectionSerializer(ODataSerializerProvider provider)
: base(provider) { }
/// <summary>
/// Creates an <see cref="ODataCollectionValue"/> for the enumerable represented by <paramref name="enumerable"/>.
/// </summary>
/// <param name="enumerable">The value of the collection to be created.</param>
/// <param name="elementType">The element EDM type of the collection.</param>
/// <param name="writeContext">The serializer context to be used while creating the collection.</param>
/// <returns>The created <see cref="ODataCollectionValue"/>.</returns>
public override ODataCollectionValue CreateODataCollectionValue(IEnumerable enumerable, IEdmTypeReference elementType,
ODataSerializerContext writeContext)
{
var shouldHideEmptyLists = writeContext.Request.GetHeader("HideEmptyLists");
if (shouldHideEmptyLists != null)
{
if (enumerable.IsNullOrEmpty())
{
return null;
//ignore
}
}
var result = base.CreateODataCollectionValue(enumerable, elementType, writeContext);
return result;
}
}
And to finalize I'll show how to inject the Serialization Provider into our OData pipeline:
config.MapODataServiceRoute(odata, odata, builder => builder
.AddService<ODataSerializerProvider>(ServiceLifetime.Scoped, sp => new MySerializerProvider(sp)));
That should wrap it up. cheers.