Skip to content

Commit f69643b

Browse files
OskarssonTobias Oskarsson
andauthored
Endpoint filter pipeline now returns EmptyHttpResult in case of 400 BadRequest (dotnet#45073)
* Fixes dotnet#45040 When an endpoint filter is used together with a failed argument validation we now return _new ValueTask<object>(EmptyHttpResult.Instance)_ instead of _new ValueTask<object>(Task.Completed)_ to prevent Task.Completed from being serialized to the response body. * Replaced ConstantExpression with NewExpression to not pass the constant through the generated closure parameter Co-authored-by: Tobias Oskarsson <[email protected]>
1 parent d0f60f2 commit f69643b

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static partial class RequestDelegateFactory
9292
private static readonly MemberExpression FormFilesExpr = Expression.Property(FormExpr, typeof(IFormCollection).GetProperty(nameof(IFormCollection.Files))!);
9393
private static readonly MemberExpression StatusCodeExpr = Expression.Property(HttpResponseExpr, typeof(HttpResponse).GetProperty(nameof(HttpResponse.StatusCode))!);
9494
private static readonly MemberExpression CompletedTaskExpr = Expression.Property(null, (PropertyInfo)GetMemberInfo<Func<Task>>(() => Task.CompletedTask));
95-
private static readonly NewExpression CompletedValueTaskExpr = Expression.New(typeof(ValueTask<object>).GetConstructor(new[] { typeof(Task) })!, CompletedTaskExpr);
95+
private static readonly NewExpression EmptyHttpResultValueTaskExpr = Expression.New(typeof(ValueTask<object>).GetConstructor(new[] { typeof(EmptyHttpResult) })!, Expression.Property(null, typeof(EmptyHttpResult), nameof(EmptyHttpResult.Instance)));
9696

9797
private static readonly ParameterExpression TempSourceStringExpr = ParameterBindingMethodCache.TempSourceStringExpr;
9898
private static readonly BinaryExpression TempSourceStringNotNullExpr = Expression.NotEqual(TempSourceStringExpr, Expression.Constant(null));
@@ -432,7 +432,7 @@ targetExpression is null
432432
var filteredInvocation = Expression.Lambda<EndpointFilterDelegate>(
433433
Expression.Condition(
434434
Expression.GreaterThanOrEqual(FilterContextHttpContextStatusCodeExpr, Expression.Constant(400)),
435-
CompletedValueTaskExpr,
435+
EmptyHttpResultValueTaskExpr,
436436
handlerInvocation),
437437
FilterContextExpr).Compile();
438438
var routeHandlerContext = new EndpointFilterFactoryContext
@@ -463,7 +463,7 @@ private static Expression MapHandlerReturnTypeToValueTask(Expression methodCall,
463463
{
464464
if (returnType == typeof(void))
465465
{
466-
return Expression.Block(methodCall, Expression.Constant(new ValueTask<object?>(EmptyHttpResult.Instance)));
466+
return Expression.Block(methodCall, EmptyHttpResultValueTaskExpr);
467467
}
468468
else if (returnType == typeof(Task))
469469
{

src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5671,6 +5671,8 @@ string HelloName(string name)
56715671
};
56725672

56735673
var httpContext = CreateHttpContext();
5674+
var responseBodyStream = new MemoryStream();
5675+
httpContext.Response.Body = responseBodyStream;
56745676

56755677
// Act
56765678
var factoryResult = RequestDelegateFactory.Create(HelloName, new RequestDelegateFactoryOptions()
@@ -5690,6 +5692,7 @@ string HelloName(string name)
56905692
// Assert
56915693
Assert.False(invoked);
56925694
Assert.Equal(400, httpContext.Response.StatusCode);
5695+
Assert.Equal(0, responseBodyStream.Position);
56935696
}
56945697

56955698
[Fact]

0 commit comments

Comments
 (0)