OData / AspNetCoreOData

ASP.NET Core OData: A server library built upon ODataLib and ASP.NET Core
Other
457 stars 158 forks source link

Wrong error message when a query options is not enabled in the global configuration but enabled in the controller. #832

Open KenitoInc opened 1 year ago

KenitoInc commented 1 year ago

Assemblies affected ASP.NET Core OData 8.x

Describe the bug Wrong error message when a query options is not enabled in the global configuration but enabled in the controller.

Reproduce steps Configure services as follows in the Startup class

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddOData(opt =>
    {
        opt.AddRouteComponents("odata", GetEdmModel()).Count().Expand().Select().OrderBy().SetMaxTop(5); // We don't enable $filter
    });
}

Add a controller method

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Filter)]
public IQueryable<Book> Get()
{
    return _db.Books/*.AsQueryable<Book>()*/;
}

Make the request below

GET ~/Books?$filter=Id eq 1

Response

{
    "error": {
        "code": "",
        "message": "The query specified in the URI is not valid. The property 'Id' cannot be used in the $filter query option.",
        "details": [],
        "innererror": {
            "message": "The property 'Id' cannot be used in the $filter query option.",
            "type": "Microsoft.OData.ODataException",
            "stacktrace": "   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.ValidateSingleValuePropertyAccessNode(SingleValuePropertyAccessNode propertyAccessNode, ODataValidationSettings settings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 389\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.ValidateSingleValueNode(SingleValueNode node, ODataValidationSettings settings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 707\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.ValidateQueryNode(QueryNode node, ODataValidationSettings settings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 565\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.ValidateLogicalOperator(BinaryOperatorNode binaryNode, ODataValidationSettings settings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 217\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.ValidateBinaryOperatorNode(BinaryOperatorNode binaryOperatorNode, ODataValidationSettings settings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 183\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.ValidateSingleValueNode(SingleValueNode node, ODataValidationSettings settings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 683\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.ValidateQueryNode(QueryNode node, ODataValidationSettings settings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 565\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.Validate(FilterClause filterClause, ODataValidationSettings settings, IEdmModel model) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 95\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\FilterQueryValidator.cs:line 63\r\n   at Microsoft.AspNetCore.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Query\\FilterQueryOption.cs:line 174\r\n   at Microsoft.AspNetCore.OData.Query.Validator.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\Validator\\ODataQueryValidator.cs:line 69\r\n   at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\ODataQueryOptions.cs:line 634\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\EnableQueryAttribute.cs:line 707\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuting(ActionExecutingContext actionExecutingContext) in C:\\Users\\kemunga\\source\\repos\\AspNetCoreOData\\src\\Microsoft.AspNetCore.OData\\Query\\EnableQueryAttribute.cs:line 167"
        }
    }
}

I don't think this is the correct error since it focuses on the property being NotFilterable. The correct exception should be $Filter query option not being allowed.

KenitoInc commented 1 year ago

Same behaviour is in WebAPI 7.x