Skip to content

return value for single HttpContext parameter #40235

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

Merged
merged 8 commits into from
Mar 3, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public static IEndpointConventionBuilder MapGet(
string pattern,
RequestDelegate requestDelegate)
{
var returnType = requestDelegate.Method.ReturnType;
if (returnType is { IsGenericType: true } && returnType.GetGenericTypeDefinition() == typeof(Task<>))
{
return MapMethods(endpoints, pattern, GetVerb, requestDelegate as Delegate);
}
return MapMethods(endpoints, pattern, GetVerb, requestDelegate);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO.Pipelines;
using System.Linq.Expressions;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;

namespace Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -69,6 +74,30 @@ public void MapEndpoint_StringPattern_BuildsEndpoint()
Assert.Equal("/", endpointBuilder1.RoutePattern.RawText);
}

[Fact]
public async Task MapEndpoint_ReturnGenericTypeTask_GeneratedDelegate()
{
var httpContext = new DefaultHttpContext();
var responseBodyStream = new MemoryStream();
httpContext.Response.Body = responseBodyStream;

// Arrange
var builder = new DefaultEndpointRouteBuilder(Mock.Of<IApplicationBuilder>());
static async Task<string> GenericTypeTaskDelegate(HttpContext context) => await Task.FromResult("String Test");

// Act
var endpointBuilder = builder.MapGet("/", GenericTypeTaskDelegate);

// Assert
var endpointBuilder1 = GetRouteEndpointBuilder(builder);
var requestDelegate = endpointBuilder1.RequestDelegate;
await requestDelegate(httpContext);

var responseBody = Encoding.UTF8.GetString(responseBodyStream.ToArray());

Assert.Equal("String Test", responseBody);
}

[Fact]
public void MapEndpoint_TypedPattern_BuildsEndpoint()
{
Expand Down