17
17
using Microsoft . AspNetCore . Http . Features ;
18
18
using Microsoft . AspNetCore . Http . Json ;
19
19
using Microsoft . AspNetCore . Http . Metadata ;
20
+ using Microsoft . AspNetCore . Internal ;
20
21
using Microsoft . AspNetCore . Routing ;
21
22
using Microsoft . Extensions . DependencyInjection ;
22
23
using Microsoft . Extensions . Internal ;
@@ -260,12 +261,12 @@ public static RequestDelegateResult Create(MethodInfo methodInfo, Func<HttpConte
260
261
261
262
private static RequestDelegateFactoryContext CreateFactoryContext ( RequestDelegateFactoryOptions ? options , RequestDelegateMetadataResult ? metadataResult = null , Delegate ? handler = null )
262
263
{
263
- if ( metadataResult ? . CachedFactoryContext is not null )
264
+ if ( metadataResult ? . CachedFactoryContext is RequestDelegateFactoryContext cachedFactoryContext )
264
265
{
265
- metadataResult . CachedFactoryContext . MetadataAlreadyInferred = true ;
266
+ cachedFactoryContext . MetadataAlreadyInferred = true ;
266
267
// The handler was not passed in to the InferMetadata call that originally created this context.
267
- metadataResult . CachedFactoryContext . Handler = handler ;
268
- return metadataResult . CachedFactoryContext ;
268
+ cachedFactoryContext . Handler = handler ;
269
+ return cachedFactoryContext ;
269
270
}
270
271
271
272
var serviceProvider = options ? . ServiceProvider ?? options ? . EndpointBuilder ? . ApplicationServices ?? EmptyServiceProvider . Instance ;
@@ -2135,21 +2136,7 @@ static async Task ExecuteAwaited(Task<object> task, HttpContext httpContext, Jso
2135
2136
2136
2137
private static Task ExecuteAwaitedReturn ( object obj , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < object > jsonTypeInfo )
2137
2138
{
2138
- // Terminal built ins
2139
- if ( obj is IResult result )
2140
- {
2141
- return ExecuteResultWriteResponse ( result , httpContext ) ;
2142
- }
2143
- else if ( obj is string stringValue )
2144
- {
2145
- SetPlaintextContentType ( httpContext ) ;
2146
- return httpContext . Response . WriteAsync ( stringValue ) ;
2147
- }
2148
- else
2149
- {
2150
- // Otherwise, we JSON serialize when we reach the terminal state
2151
- return WriteJsonResponse ( httpContext . Response , obj , options , jsonTypeInfo ) ;
2152
- }
2139
+ return ExecuteHandlerHelper . ExecuteReturnAsync ( obj , httpContext , options , jsonTypeInfo ) ;
2153
2140
}
2154
2141
2155
2142
private static Task ExecuteTaskOfTFast < T > ( Task < T > task , HttpContext httpContext , JsonTypeInfo < T > jsonTypeInfo )
@@ -2188,7 +2175,7 @@ static async Task ExecuteAwaited(Task<T> task, HttpContext httpContext, JsonSeri
2188
2175
2189
2176
private static Task ExecuteTaskOfString ( Task < string ? > task , HttpContext httpContext )
2190
2177
{
2191
- SetPlaintextContentType ( httpContext ) ;
2178
+ ExecuteHandlerHelper . SetPlaintextContentType ( httpContext ) ;
2192
2179
EnsureRequestTaskNotNull ( task ) ;
2193
2180
2194
2181
static async Task ExecuteAwaited ( Task < string > task , HttpContext httpContext )
@@ -2206,7 +2193,7 @@ static async Task ExecuteAwaited(Task<string> task, HttpContext httpContext)
2206
2193
2207
2194
private static Task ExecuteWriteStringResponseAsync ( HttpContext httpContext , string text )
2208
2195
{
2209
- SetPlaintextContentType ( httpContext ) ;
2196
+ ExecuteHandlerHelper . SetPlaintextContentType ( httpContext ) ;
2210
2197
return httpContext . Response . WriteAsync ( text ) ;
2211
2198
}
2212
2199
@@ -2293,7 +2280,7 @@ static async Task ExecuteAwaited(ValueTask<T> task, HttpContext httpContext, Jso
2293
2280
2294
2281
private static Task ExecuteValueTaskOfString ( ValueTask < string ? > task , HttpContext httpContext )
2295
2282
{
2296
- SetPlaintextContentType ( httpContext ) ;
2283
+ ExecuteHandlerHelper . SetPlaintextContentType ( httpContext ) ;
2297
2284
2298
2285
static async Task ExecuteAwaited ( ValueTask < string > task , HttpContext httpContext )
2299
2286
{
@@ -2342,21 +2329,7 @@ private static Task WriteJsonResponseFast<T>(HttpResponse response, T value, Jso
2342
2329
2343
2330
private static Task WriteJsonResponse < T > ( HttpResponse response , T ? value , JsonSerializerOptions options , JsonTypeInfo < T > jsonTypeInfo )
2344
2331
{
2345
- var runtimeType = value ? . GetType ( ) ;
2346
-
2347
- if ( runtimeType is null || jsonTypeInfo . Type == runtimeType || jsonTypeInfo . IsPolymorphicSafe ( ) )
2348
- {
2349
- // In this case the polymorphism is not
2350
- // relevant for us and will be handled by STJ, if needed.
2351
- return HttpResponseJsonExtensions . WriteAsJsonAsync ( response , value ! , jsonTypeInfo , default ) ;
2352
- }
2353
-
2354
- // Call WriteAsJsonAsync() with the runtime type to serialize the runtime type rather than the declared type
2355
- // and avoid source generators issues.
2356
- // https://github.com/dotnet/aspnetcore/issues/43894
2357
- // https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism
2358
- var runtimeTypeInfo = options . GetTypeInfo ( runtimeType ) ;
2359
- return HttpResponseJsonExtensions . WriteAsJsonAsync ( response , value ! , runtimeTypeInfo , default ) ;
2332
+ return ExecuteHandlerHelper . WriteJsonResponseAsync ( response , value , options , jsonTypeInfo ) ;
2360
2333
}
2361
2334
2362
2335
private static NotSupportedException GetUnsupportedReturnTypeException ( Type returnType )
@@ -2545,11 +2518,6 @@ private static IResult EnsureRequestResultNotNull(IResult? result)
2545
2518
return result ;
2546
2519
}
2547
2520
2548
- private static void SetPlaintextContentType ( HttpContext httpContext )
2549
- {
2550
- httpContext . Response . ContentType ??= "text/plain; charset=utf-8" ;
2551
- }
2552
-
2553
2521
private static string BuildErrorMessageForMultipleBodyParameters ( RequestDelegateFactoryContext factoryContext )
2554
2522
{
2555
2523
var errorMessage = new StringBuilder ( ) ;
0 commit comments