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

Minimal APIs - return string aren't setting content type header #33860 #34285

Merged
merged 5 commits into from
Jul 19, 2021
Merged
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
22 changes: 19 additions & 3 deletions src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static partial class RequestDelegateFactory
private static readonly MethodInfo ExecuteObjectReturnMethod = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteObjectReturn), BindingFlags.NonPublic | BindingFlags.Static)!;
private static readonly MethodInfo GetRequiredServiceMethod = typeof(ServiceProviderServiceExtensions).GetMethod(nameof(ServiceProviderServiceExtensions.GetRequiredService), BindingFlags.Public | BindingFlags.Static, new Type[] { typeof(IServiceProvider) })!;
private static readonly MethodInfo ResultWriteResponseAsyncMethod = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteResultWriteResponse), BindingFlags.NonPublic | BindingFlags.Static)!;
private static readonly MethodInfo StringResultWriteResponseAsyncMethod = GetMethodInfo<Func<HttpResponse, string, Task>>((response, text) => HttpResponseWritingExtensions.WriteAsync(response, text, default));
private static readonly MethodInfo StringResultWriteResponseAsyncMethod = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteWriteStringResponseAsync), BindingFlags.NonPublic | BindingFlags.Static)!;
private static readonly MethodInfo JsonResultWriteResponseAsyncMethod = GetMethodInfo<Func<HttpResponse, object, Task>>((response, value) => HttpResponseJsonExtensions.WriteAsJsonAsync(response, value, default));
private static readonly MethodInfo LogParameterBindingFailureMethod = GetMethodInfo<Action<HttpContext, string, string, string>>((httpContext, parameterType, parameterName, sourceValue) =>
Log.ParameterBindingFailed(httpContext, parameterType, parameterName, sourceValue));
Expand Down Expand Up @@ -444,7 +444,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
}
else if (returnType == typeof(string))
{
return Expression.Call(StringResultWriteResponseAsyncMethod, HttpResponseExpr, methodCall, Expression.Constant(CancellationToken.None));
return Expression.Call(StringResultWriteResponseAsyncMethod, HttpContextExpr, methodCall);
}
else if (returnType.IsValueType)
{
Expand Down Expand Up @@ -704,6 +704,7 @@ private static async Task ExecuteObjectReturn(object? obj, HttpContext httpConte
}
else if (obj is string stringValue)
{
SetPlaintextContentType(httpContext);
await httpContext.Response.WriteAsync(stringValue);
}
else
Expand Down Expand Up @@ -732,8 +733,10 @@ static async Task ExecuteAwaited(Task<T> task, HttpContext httpContext)

private static Task ExecuteTaskOfString(Task<string?> task, HttpContext httpContext)
{
EnsureRequestTaskNotNull(task);

SetPlaintextContentType(httpContext);
EnsureRequestTaskNotNull(task);

static async Task ExecuteAwaited(Task<string> task, HttpContext httpContext)
{
await httpContext.Response.WriteAsync(await task);
Expand All @@ -747,6 +750,12 @@ static async Task ExecuteAwaited(Task<string> task, HttpContext httpContext)
return ExecuteAwaited(task!, httpContext);
}

private static Task ExecuteWriteStringResponseAsync(HttpContext httpContext, string text)
{
SetPlaintextContentType(httpContext);
return httpContext.Response.WriteAsync(text);
}

private static Task ExecuteValueTask(ValueTask task)
{
static async Task ExecuteAwaited(ValueTask task)
Expand Down Expand Up @@ -779,6 +788,8 @@ static async Task ExecuteAwaited(ValueTask<T> task, HttpContext httpContext)

private static Task ExecuteValueTaskOfString(ValueTask<string?> task, HttpContext httpContext)
{
SetPlaintextContentType(httpContext);

static async Task ExecuteAwaited(ValueTask<string> task, HttpContext httpContext)
{
await httpContext.Response.WriteAsync(await task);
Expand Down Expand Up @@ -884,5 +895,10 @@ private static IResult EnsureRequestResultNotNull(IResult? result)

return result;
}

private static void SetPlaintextContentType(HttpContext httpContext)
{
httpContext.Response.ContentType ??= "text/plain; charset=utf-8";
}
}
}
17 changes: 16 additions & 1 deletion src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ public static IEnumerable<object[]> StringResult

[Theory]
[MemberData(nameof(StringResult))]
public async Task RequestDelegateWritesStringReturnValueAsJsonResponseBody(Delegate @delegate)
public async Task RequestDelegateWritesStringReturnValueAndSetContentTypeWhenNull(Delegate @delegate)
{
var httpContext = new DefaultHttpContext();
var responseBodyStream = new MemoryStream();
Expand All @@ -1187,6 +1187,21 @@ public async Task RequestDelegateWritesStringReturnValueAsJsonResponseBody(Deleg
var responseBody = Encoding.UTF8.GetString(responseBodyStream.ToArray());

Assert.Equal("String Test", responseBody);
Assert.Equal("text/plain; charset=utf-8", httpContext.Response.ContentType);
}

[Theory]
[MemberData(nameof(StringResult))]
public async Task RequestDelegateWritesStringReturnDoNotChangeContentType(Delegate @delegate)
{
var httpContext = new DefaultHttpContext();
httpContext.Response.ContentType = "application/json; charset=utf-8";

var requestDelegate = RequestDelegateFactory.Create(@delegate);

await requestDelegate(httpContext);

Assert.Equal("application/json; charset=utf-8", httpContext.Response.ContentType);
}

public static IEnumerable<object[]> IntResult
Expand Down