Skip to content

Refactor param binding failure handling in RequestDelegate - #64539

Merged
BrennanConroy merged 4 commits into
dotnet:mainfrom
DotNetToscana:endpointfilterfactory-fix
Apr 8, 2026
Merged

BrennanConroy merged 4 commits into
dotnet:mainfrom
DotNetToscana:endpointfilterfactory-fix

Conversation

@marcominerva

Copy link
Copy Markdown
Contributor

Description

Refactor RequestDelegateFactory to handle parameter binding failures more flexibly:

  • Execute filter pipelines on failure if filters are present and return type is ValueTask<object?>, allowing filters to observe/modify the 400 response.
  • Short-circuit with a 400 response if no filters are present.
  • Simplify logic using Expression.Condition and Expression.Block.

Add new test cases in RouteHandlerEndpointRouteBuilderExtensionsTest.cs:

  • Validate behavior with/without filters and custom responses.
  • Ensure handlers are not executed on binding failure.

Fixes #64341

Refactor `RequestDelegateFactory` to handle parameter binding
failures more flexibly:
- Execute filter pipelines on failure if filters are present
  and return type is `ValueTask<object?>`, allowing filters
  to observe/modify the 400 response.
- Short-circuit with a 400 response if no filters are present.
- Simplify logic using `Expression.Condition` and
  `Expression.Block`.

Add new test cases in `RouteHandlerEndpointRouteBuilderExtensionsTest.cs`:
- Validate behavior with/without filters and custom responses.
- Ensure handlers are not executed on binding failure.
Copilot AI review requested due to automatic review settings November 26, 2025 11:16
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Nov 26, 2025
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 26, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request refactors parameter binding failure handling in RequestDelegateFactory to provide more flexible filter integration. The key change allows endpoint filters to observe and potentially customize 400 Bad Request responses when parameter binding fails, rather than always short-circuiting before filters run.

Key Changes:

  • Unified the parameter binding failure logic using Expression.Condition instead of separate code paths for filtered vs non-filtered scenarios
  • When filters are present and return type is ValueTask<object?>, the filter pipeline executes on binding failure, allowing filters to customize the 400 response
  • Added comprehensive test coverage for binding failure scenarios with and without filters

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Http/Http.Extensions/src/RequestDelegateFactory.cs Refactored CreateParamCheckingResponseWritingMethodCall to conditionally run filter pipelines on parameter binding failures, simplifying the code with a unified Expression.Condition approach
src/Http/Routing/test/UnitTests/Builder/RouteHandlerEndpointRouteBuilderExtensionsTest.cs Added 4 test cases validating parameter binding failure behavior: with/without filters, with custom filter responses, and with validation filters

@captainsafia

Copy link
Copy Markdown
Contributor

@marcominerva To sanity check this, can you add tests for the short-circuiting logic in the infrastructure that is shared between RDG/RDF? That'll allow us to confirm that the behavior is the same across runtime and compile-time generated handler implementations.

The shared tests are implemented here.

… between RDG/RDF

This confirm that the behavior is the same across runtime and compile-time generated handler implementations.
@marcominerva

Copy link
Copy Markdown
Contributor Author

@captainsafia, I have added the new tests.

@marcominerva

Copy link
Copy Markdown
Contributor Author

I hope this PR will be considered for .NET 11, as I believe it is an important fix.

Expression failureBranch;
if (factoryContext.EndpointBuilder.FilterFactories.Count > 0 && returnType == typeof(ValueTask<object?>))
{
failureBranch = Expression.Block(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could we keep (and update) the comments that show what the expression tree is generating? Very helpful for visualizing what's happening 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BrennanConroy failureBranch definition is already commented at lines 996-1000:

// On parameter binding failure:
//  - If filters are present (pipeline built -> returnType ValueTask<object?>), run the filter pipeline so that
//    filters can observe/modify the 400 response. The handler itself will be skipped by the StatusCode >= 400
//    check embedded in the pipeline (see CreateFilterPipeline), allowing filters to customize the error.
//  - If no filters are present, just set 400 and short-circuit.

I have added a comment to describe how checkWasParamCheckFailure works. Please, let me know if it is enough.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the

// if (wasParamCheckFailure)
// {
//     httpContext.Response.StatusCode = 400;
// }

type comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don’t quite follow you. These three lines of code are part of the current implementation, which I have replaced in this PR with new logic. The behavior of the new logic is documented in lines 996–1000, as I showed you earlier.

Added explanatory comments to RequestDelegateFactory describing the checkWasParamCheckFailure conditional expression. These comments clarify how parameter binding or validation failures result in a 400 response, while successful checks invoke the handler method. No functional changes were made.
@BrennanConroy

Copy link
Copy Markdown
Member

/ba-g unrelated

@BrennanConroy
BrennanConroy merged commit 003b66c into dotnet:main Apr 8, 2026
22 of 25 checks passed
@BrennanConroy

Copy link
Copy Markdown
Member

/backport to release/10.0

@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@BrennanConroy

Copy link
Copy Markdown
Member

Thanks for the fix @marcominerva! Will try to get this approved for 10.0 patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions community-contribution Indicates that the PR has been added by a community member feature-rdg pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using AddValidation causes non-validated Minimal API endpoints to continue after parsing errors

5 participants