Odata 4 - How to serialize a property based on custom attribute in ODataResourceSerializer

api attributes entity-framework-6 odata serialization

Question

Can I put an attribute on a model being serialized with OData, and choose to serialize based on that?

I have successfully extended ODataResourceSerializer, and overridden CreateStructuralProperty. This gives me the ability to check the name of a property, and choose whether or not to serialize it.

Here's a summary of what I've got set up (which is working). This is where I'm registering the service in Startup...

private static void ConfigureODataServices(IContainerBuilder builder)
    {
        // add custom odata serializer
        builder.AddService<ODataSerializerProvider>(Singleton, s => new CustomODataSerializerProvider(s));
    }

And here's my custom formatter, where I'm currently just using the property name to choose whether or not to serialize this property:

public class CustomODataSerializerProvider : DefaultODataSerializerProvider
{
    private AnnotatingEntitySerializer _annotatingEntitySerializer;

    public CustomODataSerializerProvider(IServiceProvider container) : base(container)
    {
        _annotatingEntitySerializer = new AnnotatingEntitySerializer(this);
    }

    public override ODataEdmTypeSerializer GetEdmTypeSerializer(IEdmTypeReference edmType)
    {
        if (edmType.Definition.TypeKind == EdmTypeKind.Entity)
            return new AnnotatingEntitySerializer(this);

        return base.GetEdmTypeSerializer(edmType);
    }
}


public class AnnotatingEntitySerializer : ODataResourceSerializer
{
    public AnnotatingEntitySerializer(ODataSerializerProvider serializerProvider)
        : base(serializerProvider)
    {
    }

    public override Microsoft.OData.ODataProperty CreateStructuralProperty(IEdmStructuralProperty structuralProperty, ResourceContext resourceContext)
    {
        Microsoft.OData.ODataProperty property = base.CreateStructuralProperty(structuralProperty, resourceContext);

        if (resourceContext.StructuredType.FullTypeName() == typeof(Site).FullName)
        {
            if (property.Name == "IsDeleted") // NOTE: here, rather than checking for the property name, I want to check if it has an attribute of "ExcludeMe"
            {
                return null;
            }
        }

        return property;
    }

    public override void AppendDynamicProperties(ODataResource resource, System.Web.OData.Formatter.Serialization.SelectExpandNode selectExpandNode,
        ResourceContext resourceContext)
    {
        base.AppendDynamicProperties(resource, selectExpandNode, resourceContext);
    }
}

But of course, I want to use an attribute to choose whether it's serialized, not a hard-coded property name. Possible? Thanks.

1
0
10/17/2017 9:06:55 PM

Popular Answer

Rockyourteeth

It looks https://github.com/xuzhg/WebApiSample/tree/master/WebApiComplexTypeSkipPropertyTest is what you are looking for.

My sample is for complex type under 5.x version, but it can be used for the entity (resource) under 6.x version.

Please let me know if it can't work for your scenario.

0
10/20/2017 11:21:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow