Custom ODataSerializerProvider never instanciated and its GetEdmTypeSerializer method not called
@xuzhg is already working on this.
Since Mar 18, 2025.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.