5

I am creating a new C# OData4 Web API with a class named Call that has dynamic properties, which OData 4 allows via "Open Types". I believe I've set everything up and configured it correctly but the serialized response does not include the dynamic properties.

Did I configure something wrong?

public partial class Call 
{
    public int Id { get; set; }
    public string Email { get; set; }
    public IDictionary<string, object> DynamicProperties { get; }
}

public class CallController : ODataController
{
    [EnableQuery]
    public IQueryable<Call> GetCall([FromODataUri] int key)
    {
        return _context.Call.GetAll();
    }
}

public static partial class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        AllowUriOperations(config);

        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.ComplexType<Call>();
        var model = builder.GetEdmModel();

        config.MapODataServiceRoute(RoutePrefix.OData4, RoutePrefix.OData4, model);    
    }

    private static void AllowUriOperations(HttpConfiguration config)
    {
        config.Count();
        config.Filter();
        config.OrderBy();
        config.Expand();
        config.Select();
    }
}

4 Answers 4

1

You can enable serialization of null valued dynamic properties in OData open types by adding the following line (in my code in WebApiConfig.cs in method Register(HttpConfiguration config))

    config.Properties.AddOrUpdate("System.Web.OData.NullDynamicPropertyKey", val=>true, (oldVal,newVal)=>true);

Then my dynamic properties with nulls start to serialize.

Sign up to request clarification or add additional context in comments.

Comments

1

Or configuration.SetSerializeNullDynamicProperty(true); can be used, which is more cleaner and self-explanatory.

Comments

0

Can you check in your metadata, on the type Call do you have OpenType="true"? If not, try making it an EntitySet changing this line:

builder.ComplexType<Call>();

to this

builder.EntitySet<Call>("Calls");

If you do have OpenType="true" in your metadata, check that you definitely have some entries in your DynamicProperties collection

7 Comments

I am debugging through can confirm that are properties in the dictionary. I have tried both options on the builder, but neither works. The class is simply a class, it's not layed out in XML, so I'm not sure where I could set the OpenType=true. Do you have any ideas?
It should appear in the metadata which would be at a url like localhost/$metadata
I get an error page when I try that, regardless, where would I set the metadata? I don't understand where that data comes from.
The metadata is a document that defines how you have setup your system, it will be created automatically by the system. What url do you use to access the system with?
localhost:11011, I tired the URL with the correct port
|
0

If the value in a key pair is null the property is simply not serialized. I was expecting it to be serialized to

"key" : null

Here are some additional examples

DynamicProperties.Add("somekey", 1);

"somekey" : 1


DynamicProperties.Add("somekey", "1");

"somekey" : "1"


DynamicProperties.Add("somekey", null);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.