From 0b8d97c422d6c3c3a72e3267b1a6725df4e7cb5f Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sat, 12 Aug 2023 11:47:07 +0200 Subject: [PATCH] Fix WriteHttpResponse when NoContent (#4322) --- .../Elsa.Http/Activities/WriteHttpResponse.cs | 6 ++- .../Extensions/InputExtensions.cs | 42 ++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/modules/Elsa.Http/Activities/WriteHttpResponse.cs b/src/modules/Elsa.Http/Activities/WriteHttpResponse.cs index 58ee79d93..ebb9026fa 100644 --- a/src/modules/Elsa.Http/Activities/WriteHttpResponse.cs +++ b/src/modules/Elsa.Http/Activities/WriteHttpResponse.cs @@ -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(); diff --git a/src/modules/Elsa.Workflows.Core/Extensions/InputExtensions.cs b/src/modules/Elsa.Workflows.Core/Extensions/InputExtensions.cs index fcdece0fd..a42731aa9 100644 --- a/src/modules/Elsa.Workflows.Core/Extensions/InputExtensions.cs +++ b/src/modules/Elsa.Workflows.Core/Extensions/InputExtensions.cs @@ -6,10 +6,48 @@ using Elsa.Workflows.Core.Models; // ReSharper disable once CheckNamespace namespace Elsa.Extensions; +/// +/// Provides extensions on and +/// public static class InputExtensions { - public static T? GetOrDefault(this Input? input, ActivityExecutionContext context) => context.Get(input); - public static T? GetOrDefault(this Input? input, ExpressionExecutionContext context) => context.Get(input); + /// + /// Returns the value of the specified input, or a default value if the input is not found. + /// + public static T? GetOrDefault(this Input? input, ActivityExecutionContext context, Func? defaultValue = default) + { + var value = context.Get(input); + return value != null ? value : defaultValue != null ? defaultValue.Invoke() : default; + } + + /// + /// Returns the value of the specified input, or a default value if the input is not found. + /// + public static T? GetOrDefault(this Input? input, ExpressionExecutionContext context, Func? defaultValue = default) + { + var value = context.Get(input); + return value != null ? value : defaultValue != null ? defaultValue.Invoke() : default; + } + + /// + /// Returns the value of the specified input. + /// + /// The input. + /// The context. + /// The name of the input. + /// The type of the input. + /// The value of the specified input. + /// Throws an exception if the input is not found. public static T Get(this Input? input, ActivityExecutionContext context, [CallerArgumentExpression("input")]string? inputName = default) => context.Get(input) ?? throw new Exception($"{inputName} is required."); + + /// + /// Returns the value of the specified input. + /// + /// The input. + /// The context. + /// The name of the input. + /// The type of the input. + /// The value of the specified input. + /// Throws an exception if the input is not found. public static T Get(this Input? input, ExpressionExecutionContext context, [CallerArgumentExpression("input")]string? inputName = default) => context.Get(input) ?? throw new Exception($"{inputName} is required."); } \ No newline at end of file