@@ -38,9 +38,12 @@ public static partial class RequestDelegateFactory
38
38
private static readonly MethodInfo GetServiceMethod = typeof ( ServiceProviderServiceExtensions ) . GetMethod ( nameof ( ServiceProviderServiceExtensions . GetService ) , BindingFlags . Public | BindingFlags . Static , new Type [ ] { typeof ( IServiceProvider ) } ) ! ;
39
39
private static readonly MethodInfo ResultWriteResponseAsyncMethod = typeof ( RequestDelegateFactory ) . GetMethod ( nameof ( ExecuteResultWriteResponse ) , BindingFlags . NonPublic | BindingFlags . Static ) ! ;
40
40
private static readonly MethodInfo StringResultWriteResponseAsyncMethod = typeof ( RequestDelegateFactory ) . GetMethod ( nameof ( ExecuteWriteStringResponseAsync ) , BindingFlags . NonPublic | BindingFlags . Static ) ! ;
41
- private static readonly MethodInfo JsonResultWriteResponseAsyncMethod = GetMethodInfo < Func < HttpResponse , object , Task > > ( ( response , value ) => HttpResponseJsonExtensions . WriteAsJsonAsync ( response , value , default ) ) ;
42
41
private static readonly MethodInfo StringIsNullOrEmptyMethod = typeof ( string ) . GetMethod ( nameof ( string . IsNullOrEmpty ) , BindingFlags . Static | BindingFlags . Public ) ! ;
43
42
43
+ // Call WriteAsJsonAsync<object?>() to serialize the runtime return type rather than the declared return type.
44
+ // https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism
45
+ private static readonly MethodInfo JsonResultWriteResponseAsyncMethod = GetMethodInfo < Func < HttpResponse , object ? , Task > > ( ( response , value ) => HttpResponseJsonExtensions . WriteAsJsonAsync < object ? > ( response , value , default ) ) ;
46
+
44
47
private static readonly MethodInfo LogParameterBindingFailedMethod = GetMethodInfo < Action < HttpContext , string , string , string , bool > > ( ( httpContext , parameterType , parameterName , sourceValue , shouldThrow ) =>
45
48
Log . ParameterBindingFailed ( httpContext , parameterType , parameterName , sourceValue , shouldThrow ) ) ;
46
49
private static readonly MethodInfo LogRequiredParameterNotProvidedMethod = GetMethodInfo < Action < HttpContext , string , string , string , bool > > ( ( httpContext , parameterType , parameterName , source , shouldThrow ) =>
@@ -1442,7 +1445,8 @@ private static async Task ExecuteObjectReturn(object? obj, HttpContext httpConte
1442
1445
else
1443
1446
{
1444
1447
// Otherwise, we JSON serialize when we reach the terminal state
1445
- await httpContext . Response . WriteAsJsonAsync ( obj ) ;
1448
+ // Call WriteAsJsonAsync<object?>() to serialize the runtime return type rather than the declared return type.
1449
+ await httpContext . Response . WriteAsJsonAsync < object ? > ( obj ) ;
1446
1450
}
1447
1451
}
1448
1452
@@ -1452,12 +1456,14 @@ private static Task ExecuteTask<T>(Task<T> task, HttpContext httpContext)
1452
1456
1453
1457
static async Task ExecuteAwaited ( Task < T > task , HttpContext httpContext )
1454
1458
{
1455
- await httpContext . Response . WriteAsJsonAsync ( await task ) ;
1459
+ // Call WriteAsJsonAsync<object?>() to serialize the runtime return type rather than the declared return type.
1460
+ await httpContext . Response . WriteAsJsonAsync < object ? > ( await task ) ;
1456
1461
}
1457
1462
1458
1463
if ( task . IsCompletedSuccessfully )
1459
1464
{
1460
- return httpContext . Response . WriteAsJsonAsync ( task . GetAwaiter ( ) . GetResult ( ) ) ;
1465
+ // Call WriteAsJsonAsync<object?>() to serialize the runtime return type rather than the declared return type.
1466
+ return httpContext . Response . WriteAsJsonAsync < object ? > ( task . GetAwaiter ( ) . GetResult ( ) ) ;
1461
1467
}
1462
1468
1463
1469
return ExecuteAwaited ( task , httpContext ) ;
@@ -1506,12 +1512,14 @@ private static Task ExecuteValueTaskOfT<T>(ValueTask<T> task, HttpContext httpCo
1506
1512
{
1507
1513
static async Task ExecuteAwaited ( ValueTask < T > task , HttpContext httpContext )
1508
1514
{
1509
- await httpContext . Response . WriteAsJsonAsync ( await task ) ;
1515
+ // Call WriteAsJsonAsync<object?>() to serialize the runtime return type rather than the declared return type.
1516
+ await httpContext . Response . WriteAsJsonAsync < object ? > ( await task ) ;
1510
1517
}
1511
1518
1512
1519
if ( task . IsCompletedSuccessfully )
1513
1520
{
1514
- return httpContext . Response . WriteAsJsonAsync ( task . GetAwaiter ( ) . GetResult ( ) ) ;
1521
+ // Call WriteAsJsonAsync<object?>() to serialize the runtime return type rather than the declared return type.
1522
+ return httpContext . Response . WriteAsJsonAsync < object ? > ( task . GetAwaiter ( ) . GetResult ( ) ) ;
1515
1523
}
1516
1524
1517
1525
return ExecuteAwaited ( task , httpContext ) ;
0 commit comments