Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Security.Claims;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Json;
Expand Down Expand Up @@ -1159,6 +1160,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
Debug.Assert(factoryContext.JsonRequestBodyParameter is not null, "factoryContext.JsonRequestBodyParameter is null for a JSON body.");

var bodyType = factoryContext.JsonRequestBodyParameter.ParameterType;
var jsonTypeInfo = factoryContext.JsonSerializerOptions?.GetReadOnlyTypeInfo(bodyType);
var parameterTypeName = TypeNameHelper.GetTypeDisplayName(factoryContext.JsonRequestBodyParameter.ParameterType, fullName: false);
var parameterName = factoryContext.JsonRequestBodyParameter.Name;

Expand Down Expand Up @@ -1191,7 +1193,8 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
parameterName,
factoryContext.AllowEmptyRequestBody,
factoryContext.ThrowOnBadRequest,
factoryContext.JsonSerializerOptions);
factoryContext.JsonSerializerOptions,
jsonTypeInfo);

if (!successful)
{
Expand All @@ -1216,7 +1219,8 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
parameterName,
factoryContext.AllowEmptyRequestBody,
factoryContext.ThrowOnBadRequest,
factoryContext.JsonSerializerOptions);
factoryContext.JsonSerializerOptions,
jsonTypeInfo);

if (!successful)
{
Expand All @@ -1234,7 +1238,8 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
string parameterName,
bool allowEmptyRequestBody,
bool throwOnBadRequest,
JsonSerializerOptions? jsonSerializerOptions)
JsonSerializerOptions? jsonSerializerOptions,
JsonTypeInfo? jsonTypeInfo)
{
object? defaultBodyValue = null;

Expand All @@ -1256,7 +1261,13 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
}
try
{
bodyValue = await httpContext.Request.ReadFromJsonAsync(bodyType, jsonSerializerOptions);
// Edge case but possible if the RequestDelegateFactoryOptions.ServiceProvider and
// RequestDelegateFactoryOptions.EndpointBuilder.ServiceProvider are null
// In this situation both options and jsonTypeInfo are null.
jsonSerializerOptions ??= httpContext.RequestServices.GetService<IOptions<JsonOptions>>()?.Value.SerializerOptions ?? JsonOptions.DefaultSerializerOptions;
jsonTypeInfo ??= jsonSerializerOptions.GetTypeInfo(bodyType);

bodyValue = await httpContext.Request.ReadFromJsonAsync(jsonTypeInfo);
}
catch (IOException ex)
{
Expand Down