AutoFilterer icon indicating copy to clipboard operation
AutoFilterer copied to clipboard

FromQuery Error: InvalidOperationException: No public static bool GenericFilter.TryParse(string, out GenericFilter) method found for filter.

Open gpproton opened this issue 2 years ago • 5 comments

I'm Currently trying out AutoFilterer and it works well with using FromBody but not with FromQuery

Below are the full error message generated, i'm sticking to using FromBody for not just wanted to report the issue.

Microsoft.AspNetCore.Http.RequestDelegateFactory.BindParameterFromValue(ParameterInfo parameter, Expression valueExpression, RequestDelegateFactoryContext factoryContext, string source) Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArgument(ParameterInfo parameter, RequestDelegateFactoryContext factoryContext) Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArguments(ParameterInfo[] parameters, RequestDelegateFactoryContext factoryContext) Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArgumentsAndInferMetadata(MethodInfo methodInfo, RequestDelegateFactoryContext factoryContext) Microsoft.AspNetCore.Http.RequestDelegateFactory.InferMetadata(MethodInfo methodInfo, RequestDelegateFactoryOptions options) Microsoft.AspNetCore.Routing.RouteEndpointDataSource.CreateRouteEndpointBuilder(RouteEntry entry, RoutePattern groupPrefix, IReadOnlyList<Action<EndpointBuilder>> groupConventions, IReadOnlyList<Action<EndpointBuilder>> groupFinallyConventions) Microsoft.AspNetCore.Routing.RouteEndpointDataSource.GetGroupedEndpoints(RouteGroupContext context) Microsoft.AspNetCore.Routing.RouteGroupBuilder+GroupEndpointDataSource.GetGroupedEndpointsWithNullablePrefix(RoutePattern prefix, IReadOnlyList<Action<EndpointBuilder>> conventions, IReadOnlyList<Action<EndpointBuilder>> finallyConventions, IServiceProvider applicationServices) Microsoft.AspNetCore.Routing.RouteGroupBuilder+GroupEndpointDataSource.get_Endpoints() Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.CreateEndpointsUnsynchronized() Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.EnsureEndpointsInitialized() Microsoft.AspNetCore.Routing.DataSourceDependentCache<T>.Initialize() System.Threading.LazyInitializer.EnsureInitializedCore<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory) Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource dataSource, Lifetime lifetime, Func<MatcherBuilder> matcherBuilderFactory) Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource) Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync() Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatcher|8_0(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task<Matcher> matcherTask) Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

gpproton avatar Jan 07 '23 22:01 gpproton

It seems it's an AspNetCore binding error. Your value might have some special characters or something that can break the binding

enisn avatar Jan 07 '23 22:01 enisn

I thought so initially, below is the sample filter used.

// Sample filter
public class GenericFilter : PaginationFilterBase{ }

// Sample DTO
public class SampleDto : FilterBase {
public Guid Id { get; set; }
public string Name { get; set; }
}

gpproton avatar Jan 08 '23 10:01 gpproton

I understand the problem but nothing to do in autofilterer library. Autofilterer generates only LINQ expressions and it doesn't know about your aspnetcore application or how to bind object properties.

There is no aspnetcore dependency of autofilterer. https://github.com/enisn/AutoFilterer/blob/5e55bbf819bc1640ba2286d8fc7c8fc10a8dfdf1/src/AutoFilterer/AutoFilterer.csproj#L5-L22

You may ask your question at dotnet/aspnetcore repositroy

enisn avatar Jan 08 '23 11:01 enisn

I was able to use fine on dotnet 7 with the new AsParameters which is better for minimal API

Here

group.MapGet("/", async ([FromServices] CategoryService sv, [AsParameters] CategoryFilter filter) => await sv.GetAllAsync(filter))
            .WithName($"GetAll{name}")
            .WithOpenApi();

gpproton avatar Jan 08 '23 16:01 gpproton

Might be similar with https://github.com/dotnet/aspnetcore/issues/40752


Most probably this is the reason https://stackoverflow.com/questions/70260354/net-6-minimal-api-no-public-static-bool-request-tryparsestring-out-request

enisn avatar Jan 08 '23 19:01 enisn