Skip to content

Commit aab1413

Browse files
authored
[release/8.0] Add explicit conversion for value-type returning handlers with filters (#57966)
* Add explicit conversion for value-type returning handlers with filters * Fix using for testing packages
1 parent daef895 commit aab1413

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ private static Expression MapHandlerReturnTypeToValueTask(Expression methodCall,
533533
}
534534
else
535535
{
536+
if (returnType.IsValueType)
537+
{
538+
return Expression.Call(WrapObjectAsValueTaskMethod, Expression.Convert(methodCall, typeof(object)));
539+
}
540+
536541
return Expression.Call(WrapObjectAsValueTaskMethod, methodCall);
537542
}
538543
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Testing;
6+
7+
namespace Microsoft.AspNetCore.Routing.Internal;
8+
9+
public partial class RequestDelegateFactoryTests : LoggedTest
10+
{
11+
public static object[][] ValueTypeReturningDelegates =>
12+
[
13+
[(Func<HttpContext, int>)((HttpContext httpContext) => 42)],
14+
[(Func<HttpContext, char>)((HttpContext httpContext) => 'b')],
15+
[(Func<HttpContext, bool>)((HttpContext httpContext) => true)],
16+
[(Func<HttpContext, float>)((HttpContext httpContext) => 4.2f)],
17+
[(Func<HttpContext, double>)((HttpContext httpContext) => 4.2)],
18+
[(Func<HttpContext, decimal>)((HttpContext httpContext) => 4.2m)],
19+
[(Func<HttpContext, long>)((HttpContext httpContext) => 42)],
20+
[(Func<HttpContext, short>)((HttpContext httpContext) => 42)],
21+
[(Func<HttpContext, byte>)((HttpContext httpContext) => 42)],
22+
[(Func<HttpContext, uint>)((HttpContext httpContext) => 42)],
23+
[(Func<HttpContext, ulong>)((HttpContext httpContext) => 42)],
24+
[(Func<HttpContext, ushort>)((HttpContext httpContext) => 42)],
25+
[(Func<HttpContext, sbyte>)((HttpContext httpContext) => 42)]
26+
];
27+
28+
[Theory]
29+
[MemberData(nameof(ValueTypeReturningDelegates))]
30+
public void Create_WithEndpointFilterOnBuiltInValueTypeReturningDelegate_Works(Delegate @delegate)
31+
{
32+
var invokeCount = 0;
33+
34+
RequestDelegateFactoryOptions options = new()
35+
{
36+
EndpointBuilder = CreateEndpointBuilderFromFilterFactories(
37+
[
38+
(routeHandlerContext, next) =>
39+
{
40+
invokeCount++;
41+
return next;
42+
},
43+
(routeHandlerContext, next) =>
44+
{
45+
invokeCount++;
46+
return next;
47+
},
48+
]),
49+
};
50+
51+
var result = RequestDelegateFactory.Create(@delegate, options);
52+
Assert.Equal(2, invokeCount);
53+
}
54+
55+
public static object[][] NullableValueTypeReturningDelegates =>
56+
[
57+
[(Func<HttpContext, int?>)((HttpContext httpContext) => 42)],
58+
[(Func<HttpContext, char?>)((HttpContext httpContext) => 'b')],
59+
[(Func<HttpContext, bool?>)((HttpContext httpContext) => true)],
60+
[(Func<HttpContext, float?>)((HttpContext httpContext) => 4.2f)],
61+
[(Func<HttpContext, double?>)((HttpContext httpContext) => 4.2)],
62+
[(Func<HttpContext, decimal?>)((HttpContext httpContext) => 4.2m)],
63+
[(Func<HttpContext, long?>)((HttpContext httpContext) => 42)],
64+
[(Func<HttpContext, short?>)((HttpContext httpContext) => 42)],
65+
[(Func<HttpContext, byte?>)((HttpContext httpContext) => 42)],
66+
[(Func<HttpContext, uint?>)((HttpContext httpContext) => 42)],
67+
[(Func<HttpContext, ulong?>)((HttpContext httpContext) => 42)],
68+
[(Func<HttpContext, ushort?>)((HttpContext httpContext) => 42)],
69+
[(Func<HttpContext, sbyte?>)((HttpContext httpContext) => 42)]
70+
];
71+
72+
[Theory]
73+
[MemberData(nameof(NullableValueTypeReturningDelegates))]
74+
public void Create_WithEndpointFilterOnNullableBuiltInValueTypeReturningDelegate_Works(Delegate @delegate)
75+
{
76+
var invokeCount = 0;
77+
78+
RequestDelegateFactoryOptions options = new()
79+
{
80+
EndpointBuilder = CreateEndpointBuilderFromFilterFactories(
81+
[
82+
(routeHandlerContext, next) =>
83+
{
84+
invokeCount++;
85+
return next;
86+
},
87+
(routeHandlerContext, next) =>
88+
{
89+
invokeCount++;
90+
return next;
91+
},
92+
]),
93+
};
94+
95+
var result = RequestDelegateFactory.Create(@delegate, options);
96+
Assert.Equal(2, invokeCount);
97+
}
98+
}

0 commit comments

Comments
 (0)