Skip to content

Fix RequestDelegateFactory to handle nullable types correctly in empty request body scenarios #62861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,9 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
{
object? defaultBodyValue = null;

if (allowEmptyRequestBody && bodyType.IsValueType)
var isNullableType = bodyType.IsGenericType && bodyType.GetGenericTypeDefinition() == typeof(Nullable<>);

if (allowEmptyRequestBody && bodyType.IsValueType && !isNullableType) // Nullable types should be left null
Comment on lines +1379 to +1380
Copy link
Preview

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the complex condition into a well-named local variable or method to improve readability. For example: var shouldCreateDefaultValueType = allowEmptyRequestBody && bodyType.IsValueType && !isNullableType;

Suggested change
if (allowEmptyRequestBody && bodyType.IsValueType && !isNullableType) // Nullable types should be left null
var shouldCreateDefaultValueType = allowEmptyRequestBody && bodyType.IsValueType && !isNullableType;
if (shouldCreateDefaultValueType) // Nullable types should be left null

Copilot uses AI. Check for mistakes.

{
defaultBodyValue = CreateValueType(bodyType);
}
Expand Down
21 changes: 21 additions & 0 deletions src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,27 @@ public async Task RequestDelegateRejectsEmptyBodyGivenExplicitFromBodyParameter(
Assert.Equal(400, httpContext.Response.StatusCode);
}

[Fact]
public async Task RequestDelegatePopulatesNullFromBodyNullableParameterWithEmptyBody()
{
var httpContext = CreateHttpContext();
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Headers["Content-Length"] = "0";
httpContext.Features.Set<IHttpRequestBodyDetectionFeature>(new RequestBodyDetectionFeature(false));

var factoryResult = RequestDelegateFactory.Create(
([FromBody] int? body) =>
{
httpContext.Items["body"] = body;
}
);

var requestDelegate = factoryResult.RequestDelegate;
await requestDelegate(httpContext);

Assert.Null(httpContext.Items["body"]);
}

[Fact]
public void RequestDelegateFactoryThrowsForByRefReturnTypes()
{
Expand Down
Loading