Repository navigation
Description
Summary
The ValidationsGenerator performs validation discovery by resolving the endpoint handler method at Map* call sites. The resolver, ResolveMethodFromOperation in InvocationOperationExtensions.cs (shared source, linked into Microsoft.Extensions.Validation.ValidationsGenerator.csproj), handles inline lambdas (IAnonymousFunctionOperation), local functions, method groups, and readonly fields — but has no case for IParameterReferenceOperation or ILocalReferenceOperation. A handler passed as a delegate variable or method parameter falls through to _ => null.
For the RequestDelegateGenerator this is benign (it falls back to the runtime RequestDelegateFactory). For the ValidationsGenerator it is not: TryGetRouteHandlerMethod returns false, ExtractValidatableTypes (ValidationsGenerator.TypesParser.cs) gets an empty parameter list, and validation discovery for that endpoint is silently skipped — no diagnostic, no generated ValidatableInfo, validation just doesn't run.
This affects even completely non-generic handlers.
Reproduction
public static class EndpointExtensions
{
// Handler arrives as a delegate parameter — never resolved by the generator
public static RouteHandlerBuilder MapCommand<TRequest>(
this IEndpointRouteBuilder endpoints,
string pattern,
Func<TRequest, CancellationToken, Task<IResult>> handler)
where TRequest : IValidatable
=> endpoints.MapPost(pattern, handler);
}
// Non-generic variant is equally affected:
Func<CreateOrderRequest, Task<IResult>> handler = async req => Results.Ok();
app.MapPost("/orders", handler); // validation silently skippedExpected Behavior
Either the parameter types are discovered (the ValidationsGenerator only needs the delegate's parameter types, not the method body — when method resolution fails, the delegate type's Invoke signature is sufficient for TryExtractValidatableType), or the generator emits a diagnostic that the handler could not be statically analyzed.
Actual Behavior
Validation discovery is silently skipped for the endpoint.
Notes
Discovered while investigating #65418 / #65419. The repro in #65418 used this delegate-parameter shape; #65419 fixes the ITypeParameterSymbol handling layer (constraint walking + typeof(T) leak into generated code) and is kept scoped to that per review feedback, so the resolver gap is tracked separately here. A fix likely belongs on the Validation generator's side (fallback to the delegate type's Invoke signature) rather than in the shared RDG resolver, to avoid changing RDG behavior.
Activity
Youssef1313 commentedon Jul 15, 2026
For this variant:
We could make it work.
For the other variant of
MapCommand<TRequest>, we could try to either fallback to runtime reflection, or produce a warning. We prefer to try to runtime reflection approach.ANcpLua commentedon Aug 20, 2026
Thanks for picking this up, confirming the first variant is resolved.
Func<CreateOrderRequest, Task<IResult>> handler = ...; app.MapPost("/orders", handler);discovers and validates now, via theDelegateInvokeMethodfallback from #67821.On the
MapCommand<TRequest>variant: there's an existing workaround that may change how much this needs. Annotating the request type with[ValidatableType]routes it through the attribute-discovery path instead of endpoint discovery, so it lands in the generated resolver regardless of how the endpoint was registered, and the runtime filter then resolves by the actual parameter type, which is closed. Verified on 10.0.400 with the issue's exact shape:Repro (single file,
dotnet run repro.cs)POST {"ProductName":"a","Quantity":5000}returns 400 with both errors as written, and 200 with the attribute removed.Why the unannotated case is skipped: endpoint discovery is
FindEndpoints, a syntax predicate matching the invoked method name againstInvocationOperationExtensions.KnownMethods.app.MapCommand<CreateOrderRequest>(...)doesn't match, so it is never a call site and the closed type argument is never seen. The only site that does match is the innerendpoints.MapPost(pattern, handler), where the handler is stillFunc<TRequest, CancellationToken, Task<IResult>>. Post-#67821 that resolves, but the resolved parameter type is the openTRequest, which falls out ofTryExtractValidatableType, so it fails closed rather than emitting anything broken.One wrinkle from #67993 (verified against current
main): ASP0034 catches this shape as a side effect: the resolved parameter type is the bareTRequest, whoseDeclaredAccessibilityisNotApplicable, soIsInaccessibleFromGeneratedCode()treats it as inaccessible and warns:With
arg1as the parameter name (the delegateInvokemethod's metadata name) and no source location to anchor it. That's better than silence, but it describes the wrong problem (nothing is inaccessible, the type is open at the only call site the generator sees), and the suggested remedy can't be applied to a type parameter.That might reframe the question: rather than a reflection fallback, is the near-term gap that this shape gets either no hint (shipped SDKs) or a misleading one (post-#67993)? Special-casing
ITypeParameterSymbolthere with a message that says what's actually happening, "the request type can't be resolved statically at this call site; annotate it with[ValidatableType]", would cover generic registration helpers generally, with runtime reflection still open as the fuller answer. Happy to help with either direction.