@@ -261,7 +261,7 @@ private static RequestDelegateFactoryContext CreateFactoryContext(RequestDelegat
261
261
262
262
var serviceProvider = options ? . ServiceProvider ?? options ? . EndpointBuilder ? . ApplicationServices ?? EmptyServiceProvider . Instance ;
263
263
var endpointBuilder = options ? . EndpointBuilder ?? new RdfEndpointBuilder ( serviceProvider ) ;
264
- var jsonSerializerOptions = serviceProvider . GetService < IOptions < JsonOptions > > ( ) ? . Value . SerializerOptions ;
264
+ var jsonSerializerOptions = serviceProvider . GetService < IOptions < JsonOptions > > ( ) ? . Value . SerializerOptions ?? JsonOptions . DefaultSerializerOptions ;
265
265
266
266
var factoryContext = new RequestDelegateFactoryContext
267
267
{
@@ -1000,23 +1000,23 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1000
1000
methodCall ,
1001
1001
HttpContextExpr ,
1002
1002
factoryContext . JsonSerializerOptionsExpression ,
1003
- Expression . Constant ( factoryContext . JsonSerializerOptions ? . GetReadOnlyTypeInfo ( typeof ( object ) ) , typeof ( JsonTypeInfo < object > ) ) ) ;
1003
+ Expression . Constant ( factoryContext . JsonSerializerOptions . GetReadOnlyTypeInfo ( typeof ( object ) ) , typeof ( JsonTypeInfo < object > ) ) ) ;
1004
1004
}
1005
1005
else if ( returnType == typeof ( ValueTask < object > ) )
1006
1006
{
1007
1007
return Expression . Call ( ExecuteValueTaskOfObjectMethod ,
1008
1008
methodCall ,
1009
1009
HttpContextExpr ,
1010
1010
factoryContext . JsonSerializerOptionsExpression ,
1011
- Expression . Constant ( factoryContext . JsonSerializerOptions ? . GetReadOnlyTypeInfo ( typeof ( object ) ) , typeof ( JsonTypeInfo < object > ) ) ) ;
1011
+ Expression . Constant ( factoryContext . JsonSerializerOptions . GetReadOnlyTypeInfo ( typeof ( object ) ) , typeof ( JsonTypeInfo < object > ) ) ) ;
1012
1012
}
1013
1013
else if ( returnType == typeof ( Task < object > ) )
1014
1014
{
1015
1015
return Expression . Call ( ExecuteTaskOfObjectMethod ,
1016
1016
methodCall ,
1017
1017
HttpContextExpr ,
1018
1018
factoryContext . JsonSerializerOptionsExpression ,
1019
- Expression . Constant ( factoryContext . JsonSerializerOptions ? . GetReadOnlyTypeInfo ( typeof ( object ) ) , typeof ( JsonTypeInfo < object > ) ) ) ;
1019
+ Expression . Constant ( factoryContext . JsonSerializerOptions . GetReadOnlyTypeInfo ( typeof ( object ) ) , typeof ( JsonTypeInfo < object > ) ) ) ;
1020
1020
}
1021
1021
else if ( AwaitableInfo . IsTypeAwaitable ( returnType , out _ ) )
1022
1022
{
@@ -1052,9 +1052,9 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1052
1052
}
1053
1053
else
1054
1054
{
1055
- var jsonTypeInfo = factoryContext . JsonSerializerOptions ? . GetReadOnlyTypeInfo ( typeArg ) ;
1055
+ var jsonTypeInfo = factoryContext . JsonSerializerOptions . GetReadOnlyTypeInfo ( typeArg ) ;
1056
1056
1057
- if ( jsonTypeInfo ? . IsPolymorphicSafe ( ) == true )
1057
+ if ( jsonTypeInfo . IsPolymorphicSafe ( ) == true )
1058
1058
{
1059
1059
return Expression . Call (
1060
1060
ExecuteTaskOfTFastMethod . MakeGenericMethod ( typeArg ) ,
@@ -1093,9 +1093,9 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1093
1093
}
1094
1094
else
1095
1095
{
1096
- var jsonTypeInfo = factoryContext . JsonSerializerOptions ? . GetReadOnlyTypeInfo ( typeArg ) ;
1096
+ var jsonTypeInfo = factoryContext . JsonSerializerOptions . GetReadOnlyTypeInfo ( typeArg ) ;
1097
1097
1098
- if ( jsonTypeInfo ? . IsPolymorphicSafe ( ) == true )
1098
+ if ( jsonTypeInfo . IsPolymorphicSafe ( ) == true )
1099
1099
{
1100
1100
return Expression . Call (
1101
1101
ExecuteValueTaskOfTFastMethod . MakeGenericMethod ( typeArg ) ,
@@ -1137,9 +1137,9 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1137
1137
}
1138
1138
else
1139
1139
{
1140
- var jsonTypeInfo = factoryContext . JsonSerializerOptions ? . GetReadOnlyTypeInfo ( returnType ) ;
1140
+ var jsonTypeInfo = factoryContext . JsonSerializerOptions . GetReadOnlyTypeInfo ( returnType ) ;
1141
1141
1142
- if ( jsonTypeInfo ? . IsPolymorphicSafe ( ) == true )
1142
+ if ( jsonTypeInfo . IsPolymorphicSafe ( ) == true )
1143
1143
{
1144
1144
return Expression . Call (
1145
1145
JsonResultWriteResponseOfTFastAsyncMethod . MakeGenericMethod ( returnType ) ,
@@ -1204,6 +1204,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1204
1204
Debug . Assert ( factoryContext . JsonRequestBodyParameter is not null , "factoryContext.JsonRequestBodyParameter is null for a JSON body." ) ;
1205
1205
1206
1206
var bodyType = factoryContext . JsonRequestBodyParameter . ParameterType ;
1207
+ var jsonTypeInfo = factoryContext . JsonSerializerOptions . GetReadOnlyTypeInfo ( bodyType ) ;
1207
1208
var parameterTypeName = TypeNameHelper . GetTypeDisplayName ( factoryContext . JsonRequestBodyParameter . ParameterType , fullName : false ) ;
1208
1209
var parameterName = factoryContext . JsonRequestBodyParameter . Name ;
1209
1210
@@ -1236,7 +1237,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1236
1237
parameterName ,
1237
1238
factoryContext . AllowEmptyRequestBody ,
1238
1239
factoryContext . ThrowOnBadRequest ,
1239
- factoryContext . JsonSerializerOptions ) ;
1240
+ jsonTypeInfo ) ;
1240
1241
1241
1242
if ( ! successful )
1242
1243
{
@@ -1261,7 +1262,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1261
1262
parameterName ,
1262
1263
factoryContext . AllowEmptyRequestBody ,
1263
1264
factoryContext . ThrowOnBadRequest ,
1264
- factoryContext . JsonSerializerOptions ) ;
1265
+ jsonTypeInfo ) ;
1265
1266
1266
1267
if ( ! successful )
1267
1268
{
@@ -1279,7 +1280,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1279
1280
string parameterName ,
1280
1281
bool allowEmptyRequestBody ,
1281
1282
bool throwOnBadRequest ,
1282
- JsonSerializerOptions ? jsonSerializerOptions )
1283
+ JsonTypeInfo jsonTypeInfo )
1283
1284
{
1284
1285
object ? defaultBodyValue = null ;
1285
1286
@@ -1301,7 +1302,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1301
1302
}
1302
1303
try
1303
1304
{
1304
- bodyValue = await httpContext . Request . ReadFromJsonAsync ( bodyType , jsonSerializerOptions ) ;
1305
+ bodyValue = await httpContext . Request . ReadFromJsonAsync ( jsonTypeInfo ) ;
1305
1306
}
1306
1307
catch ( IOException ex )
1307
1308
{
@@ -2091,9 +2092,9 @@ private static MemberInfo GetMemberInfo<T>(Expression<T> expr)
2091
2092
// if necessary and restart the cycle until we've reached a terminal state (unknown type).
2092
2093
// We currently don't handle Task<unknown> or ValueTask<unknown>. We can support this later if this
2093
2094
// ends up being a common scenario.
2094
- private static Task ExecuteValueTaskOfObject ( ValueTask < object > valueTask , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < object > ? jsonTypeInfo )
2095
+ private static Task ExecuteValueTaskOfObject ( ValueTask < object > valueTask , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < object > jsonTypeInfo )
2095
2096
{
2096
- static async Task ExecuteAwaited ( ValueTask < object > valueTask , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < object > ? jsonTypeInfo )
2097
+ static async Task ExecuteAwaited ( ValueTask < object > valueTask , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < object > jsonTypeInfo )
2097
2098
{
2098
2099
await ExecuteAwaitedReturn ( await valueTask , httpContext , options , jsonTypeInfo ) ;
2099
2100
}
@@ -2106,9 +2107,9 @@ static async Task ExecuteAwaited(ValueTask<object> valueTask, HttpContext httpCo
2106
2107
return ExecuteAwaited ( valueTask , httpContext , options , jsonTypeInfo ) ;
2107
2108
}
2108
2109
2109
- private static Task ExecuteTaskOfObject ( Task < object > task , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < object > ? jsonTypeInfo )
2110
+ private static Task ExecuteTaskOfObject ( Task < object > task , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < object > jsonTypeInfo )
2110
2111
{
2111
- static async Task ExecuteAwaited ( Task < object > task , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < object > ? jsonTypeInfo )
2112
+ static async Task ExecuteAwaited ( Task < object > task , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < object > jsonTypeInfo )
2112
2113
{
2113
2114
await ExecuteAwaitedReturn ( await task , httpContext , options , jsonTypeInfo ) ;
2114
2115
}
@@ -2121,7 +2122,7 @@ static async Task ExecuteAwaited(Task<object> task, HttpContext httpContext, Jso
2121
2122
return ExecuteAwaited ( task , httpContext , options , jsonTypeInfo ) ;
2122
2123
}
2123
2124
2124
- private static Task ExecuteAwaitedReturn ( object obj , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < object > ? jsonTypeInfo )
2125
+ private static Task ExecuteAwaitedReturn ( object obj , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < object > jsonTypeInfo )
2125
2126
{
2126
2127
// Terminal built ins
2127
2128
if ( obj is IResult result )
@@ -2157,11 +2158,11 @@ static async Task ExecuteAwaited(Task<T> task, HttpContext httpContext, JsonType
2157
2158
return ExecuteAwaited ( task , httpContext , jsonTypeInfo ) ;
2158
2159
}
2159
2160
2160
- private static Task ExecuteTaskOfT < T > ( Task < T > task , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < T > jsonTypeInfo )
2161
+ private static Task ExecuteTaskOfT < T > ( Task < T > task , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < T > jsonTypeInfo )
2161
2162
{
2162
2163
EnsureRequestTaskNotNull ( task ) ;
2163
2164
2164
- static async Task ExecuteAwaited ( Task < T > task , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < T > jsonTypeInfo )
2165
+ static async Task ExecuteAwaited ( Task < T > task , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < T > jsonTypeInfo )
2165
2166
{
2166
2167
await WriteJsonResponse ( httpContext . Response , await task , options , jsonTypeInfo ) ;
2167
2168
}
@@ -2262,9 +2263,9 @@ static async Task ExecuteAwaited(ValueTask<T> task, HttpContext httpContext, Jso
2262
2263
return ExecuteAwaited ( task , httpContext , jsonTypeInfo ) ;
2263
2264
}
2264
2265
2265
- private static Task ExecuteValueTaskOfT < T > ( ValueTask < T > task , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < T > jsonTypeInfo )
2266
+ private static Task ExecuteValueTaskOfT < T > ( ValueTask < T > task , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < T > jsonTypeInfo )
2266
2267
{
2267
- static async Task ExecuteAwaited ( ValueTask < T > task , HttpContext httpContext , JsonSerializerOptions ? options , JsonTypeInfo < T > jsonTypeInfo )
2268
+ static async Task ExecuteAwaited ( ValueTask < T > task , HttpContext httpContext , JsonSerializerOptions options , JsonTypeInfo < T > jsonTypeInfo )
2268
2269
{
2269
2270
await WriteJsonResponse ( httpContext . Response , await task , options , jsonTypeInfo ) ;
2270
2271
}
@@ -2326,16 +2327,10 @@ private static async Task ExecuteResultWriteResponse(IResult? result, HttpContex
2326
2327
private static Task WriteJsonResponseFast < T > ( HttpResponse response , T value , JsonTypeInfo < T > jsonTypeInfo )
2327
2328
=> HttpResponseJsonExtensions . WriteAsJsonAsync ( response , value , jsonTypeInfo , default ) ;
2328
2329
2329
- private static Task WriteJsonResponse < T > ( HttpResponse response , T ? value , JsonSerializerOptions ? options , JsonTypeInfo < T > ? jsonTypeInfo )
2330
+ private static Task WriteJsonResponse < T > ( HttpResponse response , T ? value , JsonSerializerOptions options , JsonTypeInfo < T > jsonTypeInfo )
2330
2331
{
2331
2332
var runtimeType = value ? . GetType ( ) ;
2332
2333
2333
- // Edge case but possible if the RequestDelegateFactoryOptions.ServiceProvider and
2334
- // RequestDelegateFactoryOptions.EndpointBuilder.ServiceProvider are null
2335
- // In this situation both options and jsonTypeInfo are null.
2336
- options ??= response . HttpContext . RequestServices . GetService < IOptions < JsonOptions > > ( ) ? . Value . SerializerOptions ?? JsonOptions . DefaultSerializerOptions ;
2337
- jsonTypeInfo ??= ( JsonTypeInfo < T > ) options . GetTypeInfo ( typeof ( T ) ) ;
2338
-
2339
2334
if ( runtimeType is null || jsonTypeInfo . Type == runtimeType || jsonTypeInfo . IsPolymorphicSafe ( ) )
2340
2335
{
2341
2336
// In this case the polymorphism is not
0 commit comments