Skip to content
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

Support optionality via nullability and default values #34505

Merged
merged 8 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
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
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 @@ -10,6 +10,7 @@
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
Expand Down Expand Up @@ -487,7 +488,8 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
{
object? bodyValue = defaultBodyValue;

if (httpContext.Request.ContentLength != 0 && httpContext.Request.HasJsonContentType())
var feature = httpContext.Features.Get<IHttpRequestBodyDetectionFeature>();
halter73 marked this conversation as resolved.
Show resolved Hide resolved
if (feature?.CanHaveBody == true)
{
try
{
Expand Down
21 changes: 16 additions & 5 deletions src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,6 @@ void TestImpliedFromBodyInterface(HttpContext httpContext, ITodo myService)
[MemberData(nameof(FromBodyActions))]
public async Task RequestDelegatePopulatesFromBodyParameter(Delegate action)
{
// while (!System.Diagnostics.Debugger.IsAttached)
// {
// System.Console.WriteLine($"Waiting to attach on ${Environment.ProcessId}");
// System.Threading.Thread.Sleep(1000);
// }
Todo originalTodo = new()
{
Name = "Write more tests!"
Expand All @@ -686,6 +681,7 @@ public async Task RequestDelegatePopulatesFromBodyParameter(Delegate action)

httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Headers["Content-Length"] = stream.Length.ToString();
httpContext.Features.Set<IHttpRequestBodyDetectionFeature>(new RequestBodyDetectionFeature(true));

var jsonOptions = new JsonOptions();
jsonOptions.SerializerOptions.Converters.Add(new TodoJsonConverter());
Expand Down Expand Up @@ -717,6 +713,7 @@ public async Task RequestDelegateRejectsEmptyBodyGivenFromBodyParameter(Delegate
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Headers["Content-Length"] = "0";
httpContext.Features.Set<IHttpRequestBodyDetectionFeature>(new RequestBodyDetectionFeature(false));

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(LoggerFactory);
Expand Down Expand Up @@ -793,6 +790,7 @@ void TestAction([FromBody] Todo todo)
httpContext.Request.Headers["Content-Length"] = "1";
httpContext.Request.Body = new IOExceptionThrowingRequestBodyStream(ioException);
httpContext.Features.Set<IHttpRequestLifetimeFeature>(new TestHttpRequestLifetimeFeature());
httpContext.Features.Set<IHttpRequestBodyDetectionFeature>(new RequestBodyDetectionFeature(true));
httpContext.RequestServices = serviceCollection.BuildServiceProvider();

var requestDelegate = RequestDelegateFactory.Create(TestAction);
Expand Down Expand Up @@ -826,7 +824,9 @@ void TestAction([FromBody] Todo todo)
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Headers["Content-Length"] = "1";
httpContext.Request.Body = new IOExceptionThrowingRequestBodyStream(invalidDataException);
httpContext.Features.Set<IHttpRequestBodyDetectionFeature>(new RequestBodyDetectionFeature(true));
httpContext.Features.Set<IHttpRequestLifetimeFeature>(new TestHttpRequestLifetimeFeature());

httpContext.RequestServices = serviceCollection.BuildServiceProvider();

var requestDelegate = RequestDelegateFactory.Create(TestAction);
Expand Down Expand Up @@ -1550,6 +1550,7 @@ public async Task RequestDelegateHandlesBodyParamOptionality(Delegate @delegate,
httpContext.Request.Body = stream;
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.ContentLength = stream.Length;
httpContext.Features.Set<IHttpRequestBodyDetectionFeature>(new RequestBodyDetectionFeature(true));
}

var jsonOptions = new JsonOptions();
Expand Down Expand Up @@ -1947,5 +1948,15 @@ public void Abort()
_requestAbortedCts.Cancel();
}
}

private class RequestBodyDetectionFeature : IHttpRequestBodyDetectionFeature
{
public RequestBodyDetectionFeature(bool canHaveBody)
{
CanHaveBody = canHaveBody;
}

public bool CanHaveBody { get; }
}
}
}