OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
855 stars 473 forks source link

How to get property info and entity Type from ODataPrimitiveSerializer #2419

Open komdil opened 3 years ago

komdil commented 3 years ago

I am using custom ODataPrimitiveTypeSerializer. I need to do some logic based on property info and entity type and return some value. But I couldn't find solution

Assemblies affected

Microsoft.AspNetCore.OData Version="5.0.3"

Reproduce steps

Create custom primitive type serializer like:

 public class PrimitiveSerializer : ODataPrimitiveSerializer
    {
        /// <summary>
        /// Rewriting value
        /// </summary>
        public override ODataPrimitiveValue CreateODataPrimitiveValue(object graph, IEdmPrimitiveTypeReference primitiveType, ODataSerializerContext writeContext)
        {
            if (graph is string stringValue && primitiveType is EdmStringTypeReference typeReference)
            {
                graph = GetCalculatedValue(stringValue);
            }
            var value = base.CreateODataPrimitiveValue(graph, primitiveType, writeContext);

            return value;
        }
}

Expected result

I want to get property info and entity Type in CreateODataPrimitiveValue method

Actual result

I couldn't find solution

xuzhg commented 3 years ago

Can you go to ODataResourceSerializer to get the information that you want?

komdil commented 3 years ago

Can you go to ODataResourceSerializer to get the information that you want?

@xuzhg I tried, but coudn't find a solution. ODataResourceSerializer has CreateODataValue also, but it is the same with ODataPrimitiveSerializer. I am able to get only some property meta data like MaxLenght, IsUnicode.... Can I define my own property meta data in configuration? For example,

            var cardNumberProperty = entity.Property(s => s.CardNumber);
            cardNumberProperty.SetAttribute("IsEncrypted", true);

If so, it will solve my problem

komdil commented 3 years ago

@xuzhg Do you have any suggestion?