AutoMapper / AutoMapper.Extensions.OData

Creates LINQ expressions from ODataQueryOptions and executes the query.
MIT License
140 stars 38 forks source link

$search Cast Exception #124

Closed CountingSheep02 closed 2 years ago

CountingSheep02 commented 2 years ago

Issue

I am trying to use the new $search feature, but I get the error "Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.MethodCallExpression'."

I uploaded a test project that can reproduce the issue. Test query: https://localhost:7222/students?$search=Norman

Version Info

Target Framework: .NET 6.0 Pacakges: AutoMapper.AspNetCore.OData.EFCore: 3.0.1 AutoMapper.Extensions.Microsoft.DependencyInjection: 11.0.0 Microsoft.EntityFrameworkCore.SqlServer: 6.0.2

Stack trace

System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.MethodCallExpression'. at AutoMapper.AspNet.OData.LinqExtensions.ToSearchExpression[T](SearchQueryOption filterOption, HandleNullPropagationOption handleNullPropagation, TimeZoneInfo timeZone) at AutoMapper.AspNet.OData.LinqExtensions.ToFilterExpression[T](ODataQueryOptions1 options, HandleNullPropagationOption handleNullPropagation, TimeZoneInfo timeZone) at AutoMapper.AspNet.OData.QueryableExtensions.GetQueryAsync[TModel,TData](IQueryable1 query, IMapper mapper, ODataQueryOptions1 options, QuerySettings querySettings) at Sample.Api.Controllers.StudentsController.GetStudentsAsync(ODataQueryOptions1 queryOptions, CancellationToken cancellationToken) in /Users/ryan/Downloads/Sample.Api/Sample.Api/Controllers/StudentsController.cs:line 29 at lambda_method112(Closure , Object ) at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult() at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.gAwaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.gAwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Sample

Sample.zip

BlaiseD commented 2 years ago

Not sure what's goin on here. Your search binder is not being called for some reason. Maybe start with the sample here, add your code and see when it breaks.

I don't see anything in the dev blog which suggests you're missing something.

Maybe post here when you figure out the difference?