OData / OData/AspNetCoreOData

Custom ODataSerializerProvider never instanciated and its GetEdmTypeSerializer method not called

Open
#1,444 1 comment 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Mar 18, 2025.

wontfix
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Assemblies affected
ASP.NET Core OData 8.2.4

Describe the bug
In a webapi project, I would like to customize ODataSerializerProvider by deriving it in ODataCustomODataSerializerProvider class to serialize a custom type, ie Dictionary<int, List>, in OData, but the ODataCustomODataSerializerProvider is never instanciated.

Reproduce steps

public class ODataCustomODataSerializerProvider : ODataSerializerProvider
{
    public ODataCustomODataSerializerProvider(IServiceProvider serviceProvider):
        base(serviceProvider)
    {
    }
    public override IODataEdmTypeSerializer GetEdmTypeSerializer(IEdmTypeReference edmType)
    {
        if (edmType.FullName() == typeof(Dictionary<int, List<int>>).FullName)
        {
            // return Do something
        }
        return base.GetEdmTypeSerializer(edmType);
    }
} 

To initialize it in OData, the following instructions are used in Startup:

 option.AddRouteComponents(Configuration["Mvc:ApiName"], GetEdmModelV1(), builder =>
 {
     builder.AddSingleton<ODataSerializerProvider, ODataCustomODataSerializerProvider>();
 });

Then in a OData derived controller, I call its method GetTest():

class ODataTestController: ODataController {
        [HttpGet("GetTest()")]
        public IActionResult GetTest()
        {
            var a = new Dictionary<int, List<int>>();
            a.Add(10, new List<int>() { 20, 20, 20 });
            a.Add(20, new List<int>() { 30, 30, 30 });

            return Ok(new TestInfoDto() { GroupTypes = a });
        }
}

With TestInfoDto, complexe type:

    public class TestInfoDto
    {
        public Dictionary<int, List<int>> GroupTypes { get; set; } = new Dictionary<int, List<int>>();
    }

Rregistered in OData by:

builder.AddComplexType(typeof(TestInfoDto));

Expected behavior
The method GetEdmTypeSerializer of the ODataCustomODataSerializerProvider must be called when OData needs to serialize data from the method GetTest() of derived ODataController.

What is wrong? Or what is missing?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.