Fix WriteHttpResponse when NoContent (#4322)

This commit is contained in:
Sipke Schoorstra 2023-08-12 11:47:07 +02:00 committed by GitHub
parent 74eec314fe
commit 0b8d97c422
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 4 deletions

View file

@ -85,7 +85,8 @@ public class WriteHttpResponse : Activity
private async Task WriteResponseAsync(ActivityExecutionContext context, HttpResponse response)
{
// Set status code.
response.StatusCode = (int)context.Get(StatusCode);
var statusCode = StatusCode.GetOrDefault(context, () => HttpStatusCode.OK);
response.StatusCode = (int)statusCode;
// Add headers.
var headers = context.GetHeaders(ResponseHeaders);
@ -110,7 +111,8 @@ public class WriteHttpResponse : Activity
response.ContentType = httpContent.Headers.ContentType?.ToString() ?? contentType;
// Write content.
await httpContent.CopyToAsync(response.Body);
if(statusCode != HttpStatusCode.NoContent)
await httpContent.CopyToAsync(response.Body);
// Complete activity.
await context.CompleteActivityAsync();

View file

@ -6,10 +6,48 @@ using Elsa.Workflows.Core.Models;
// ReSharper disable once CheckNamespace
namespace Elsa.Extensions;
/// <summary>
/// Provides extensions on <see cref="ExpressionExecutionContext"/> and <see cref="ActivityExecutionContext"/>
/// </summary>
public static class InputExtensions
{
public static T? GetOrDefault<T>(this Input<T>? input, ActivityExecutionContext context) => context.Get(input);
public static T? GetOrDefault<T>(this Input<T>? input, ExpressionExecutionContext context) => context.Get(input);
/// <summary>
/// Returns the value of the specified input, or a default value if the input is not found.
/// </summary>
public static T? GetOrDefault<T>(this Input<T>? input, ActivityExecutionContext context, Func<T>? defaultValue = default)
{
var value = context.Get(input);
return value != null ? value : defaultValue != null ? defaultValue.Invoke() : default;
}
/// <summary>
/// Returns the value of the specified input, or a default value if the input is not found.
/// </summary>
public static T? GetOrDefault<T>(this Input<T>? input, ExpressionExecutionContext context, Func<T>? defaultValue = default)
{
var value = context.Get(input);
return value != null ? value : defaultValue != null ? defaultValue.Invoke() : default;
}
/// <summary>
/// Returns the value of the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="context">The context.</param>
/// <param name="inputName">The name of the input.</param>
/// <typeparam name="T">The type of the input.</typeparam>
/// <returns>The value of the specified input.</returns>
/// <exception cref="Exception">Throws an exception if the input is not found.</exception>
public static T Get<T>(this Input<T>? input, ActivityExecutionContext context, [CallerArgumentExpression("input")]string? inputName = default) => context.Get(input) ?? throw new Exception($"{inputName} is required.");
/// <summary>
/// Returns the value of the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="context">The context.</param>
/// <param name="inputName">The name of the input.</param>
/// <typeparam name="T">The type of the input.</typeparam>
/// <returns>The value of the specified input.</returns>
/// <exception cref="Exception">Throws an exception if the input is not found.</exception>
public static T Get<T>(this Input<T>? input, ExpressionExecutionContext context, [CallerArgumentExpression("input")]string? inputName = default) => context.Get(input) ?? throw new Exception($"{inputName} is required.");
}