OData Query parameters are not working in 8.2.3

160 Views Asked by At

We have migrated our API from 2.2 to 6.0 and we are upgraded OData also to 8.2.3. Before update custom methods with query parameters are working well but after update no luck. Basic OData methods and functionalities are working well and good after update too.

Below is my controller code.

public partial class DepartmentController : ODataController
 {
    [HttpGet, Route("api/v1/department/getDepartmentsByCompany")]
    [Microsoft.AspNetCore.OData.Query.EnableQuery]
    public Task<BaseResponse<IEnumerable<Department>>> GetDepartmentByCompany([FromQuery] int id, [FromQuery] FunctionalType? functionalType)
    {
        try
        {
            return "success data";
        }
        catch (Exception ex)
        {
            LogHelper.LogException(ex, _hostingEnvironment);
            return "Failed";
        }
    }
    
    [Microsoft.AspNetCore.Mvc.HttpGet] 
    [Microsoft.AspNetCore.OData.Query.EnableQuery]
    public System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Department>> Get()
    {
        return _implementation.GetAllAsync();
    }
    
    [Microsoft.AspNetCore.Mvc.HttpGet]
    [Microsoft.AspNetCore.OData.Query.EnableQuery] 
    public Microsoft.AspNetCore.OData.Results.SingleResult<Department> Get(int key)
    {
        return _implementation.GetAsync(key);
    }
 }

And Program.cs looks like

builder.Services.AddControllers(g =>
{
    g.EnableEndpointRouting = false;
}).AddOData(opt =>
    {
        var batchHandler = new DefaultODataBatchHandler();
        opt.AddRouteComponents("api/v1", DIRegister.GetEdmModels(), batchHandler);
        opt.Count().Select().Filter().OrderBy().Expand().SetMaxTop(int.MaxValue);
        opt.EnableQueryFeatures(int.MaxValue);
        opt.EnableAttributeRouting = false;
    }).AddJsonOptions(g =>
    {
        g.AllowInputFormatterExceptionMessages = true;
    });

I am getting below exception

The key value (getComapnyByDepartment) from request is not valid. The key value should be format of type 'Edm.Int32'

and inner exception as Expected literal type token but found token getComapnyByDepartment

Am i missing anything here or configuration issues. I have gone through the MS article no luck here too

Sample code is uploaded here

We have raised bug in OData you can see here

0

There are 0 best solutions below