Skip to content

Commit e3ae0c6

Browse files
github-actions[bot]Tobias Oskarsson
andauthored
[release/7.0] Endpoint filter pipeline now returns EmptyHttpResult in case of 400 BadRequest (#45109)
* Fixes #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 ab6b267 commit e3ae0c6

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
@@ -91,7 +91,7 @@ public static partial class RequestDelegateFactory
9191
private static readonly MemberExpression FormFilesExpr = Expression.Property(FormExpr, typeof(IFormCollection).GetProperty(nameof(IFormCollection.Files))!);
9292
private static readonly MemberExpression StatusCodeExpr = Expression.Property(HttpResponseExpr, typeof(HttpResponse).GetProperty(nameof(HttpResponse.StatusCode))!);
9393
private static readonly MemberExpression CompletedTaskExpr = Expression.Property(null, (PropertyInfo)GetMemberInfo<Func<Task>>(() => Task.CompletedTask));
94-
private static readonly NewExpression CompletedValueTaskExpr = Expression.New(typeof(ValueTask<object>).GetConstructor(new[] { typeof(Task) })!, CompletedTaskExpr);
94+
private static readonly NewExpression EmptyHttpResultValueTaskExpr = Expression.New(typeof(ValueTask<object>).GetConstructor(new[] { typeof(EmptyHttpResult) })!, Expression.Property(null, typeof(EmptyHttpResult), nameof(EmptyHttpResult.Instance)));
9595

9696
private static readonly ParameterExpression TempSourceStringExpr = ParameterBindingMethodCache.TempSourceStringExpr;
9797
private static readonly BinaryExpression TempSourceStringNotNullExpr = Expression.NotEqual(TempSourceStringExpr, Expression.Constant(null));
@@ -424,7 +424,7 @@ targetExpression is null
424424
var filteredInvocation = Expression.Lambda<EndpointFilterDelegate>(
425425
Expression.Condition(
426426
Expression.GreaterThanOrEqual(FilterContextHttpContextStatusCodeExpr, Expression.Constant(400)),
427-
CompletedValueTaskExpr,
427+
EmptyHttpResultValueTaskExpr,
428428
handlerInvocation),
429429
FilterContextExpr).Compile();
430430
var routeHandlerContext = new EndpointFilterFactoryContext
@@ -455,7 +455,7 @@ private static Expression MapHandlerReturnTypeToValueTask(Expression methodCall,
455455
{
456456
if (returnType == typeof(void))
457457
{
458-
return Expression.Block(methodCall, Expression.Constant(new ValueTask<object?>(EmptyHttpResult.Instance)));
458+
return Expression.Block(methodCall, EmptyHttpResultValueTaskExpr);
459459
}
460460
else if (returnType == typeof(Task))
461461
{

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

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

52485248
var httpContext = CreateHttpContext();
5249+
var responseBodyStream = new MemoryStream();
5250+
httpContext.Response.Body = responseBodyStream;
52495251

52505252
// Act
52515253
var factoryResult = RequestDelegateFactory.Create(HelloName, new RequestDelegateFactoryOptions()
@@ -5265,6 +5267,7 @@ string HelloName(string name)
52655267
// Assert
52665268
Assert.False(invoked);
52675269
Assert.Equal(400, httpContext.Response.StatusCode);
5270+
Assert.Equal(0, responseBodyStream.Position);
52685271
}
52695272

52705273
[Fact]

0 commit comments

Comments
 (0)