From 32a6d4aef26b4e0fe3841c5b0dae99fa2e01aa40 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 25 Apr 2023 11:18:38 +0200 Subject: [PATCH] Refactor SendEmail inputs to support single string and string lists --- .../Elsa.Email/Activities/SendEmail.cs | 22 +++++-------------- .../Helpers/ObjectConverter.cs | 4 ++++ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/modules/Elsa.Email/Activities/SendEmail.cs b/src/modules/Elsa.Email/Activities/SendEmail.cs index da4bca0df..6ccc13e69 100644 --- a/src/modules/Elsa.Email/Activities/SendEmail.cs +++ b/src/modules/Elsa.Email/Activities/SendEmail.cs @@ -43,7 +43,7 @@ public class SendEmail : Activity /// The recipients email addresses. /// [Input(Description = "The recipients email addresses.", UIHint = InputUIHints.MultiText)] - public Input To { get; set; } = default!; + public Input> To { get; set; } = default!; /// /// The CC recipient email addresses. @@ -52,7 +52,7 @@ public class SendEmail : Activity Description = "The CC recipient email addresses.", UIHint = InputUIHints.MultiText, Category = "More")] - public Input Cc { get; set; } = default!; + public Input> Cc { get; set; } = default!; /// /// The BCC recipients email addresses. @@ -61,7 +61,7 @@ public class SendEmail : Activity Description = "The BCC recipients email addresses.", UIHint = InputUIHints.MultiText, Category = "More")] - public Input Bcc { get; set; } = default!; + public Input> Bcc { get; set; } = default!; /// /// The subject of the email message. @@ -103,7 +103,7 @@ public class SendEmail : Activity message.Sender = MailboxAddress.Parse(from); message.From.Add(MailboxAddress.Parse(from)); - message.Subject = Subject.GetOrDefault(context); + message.Subject = Subject.GetOrDefault(context) ?? ""; var bodyBuilder = new BodyBuilder { HtmlBody = Body.GetOrDefault(context) }; await AddAttachmentsAsync(context, bodyBuilder, cancellationToken); @@ -129,19 +129,9 @@ public class SendEmail : Activity private async ValueTask OnErrorCompletedAsync(ActivityExecutionContext context, ActivityExecutionContext childContext) => await context.CompleteActivityAsync(); - private static ICollection GetAddresses(ActivityExecutionContext context, Input input) + private static ICollection GetAddresses(ActivityExecutionContext context, Input> input) { - var addresses = input.GetOrDefault(context); - - if (addresses == null) - return new List(0); - - return addresses switch - { - string s => new[] { s }, - IEnumerable e => e.ToList(), - _ => new[] { addresses.ToString()! } - }; + return input.GetOrDefault(context) ?? new List(0); } private async Task AddAttachmentsAsync(ActivityExecutionContext context, BodyBuilder bodyBuilder, CancellationToken cancellationToken) diff --git a/src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs b/src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs index 8282ac04e..598facddd 100644 --- a/src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs +++ b/src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs @@ -145,6 +145,10 @@ public static class ObjectConverter if (underlyingTargetType == typeof(Type)) return converterOptions?.WellKnownTypeRegistry != null ? converterOptions.WellKnownTypeRegistry.GetTypeOrDefault(s) : Type.GetType(s); + + // Perhaps it's a bit of a leap, but if the input is a string and the target type is IEnumerable, then let's assume the string is a comma-separated list of strings. + if (typeof(IEnumerable).IsAssignableFrom(underlyingTargetType)) + return new[] { s }; } if (value is IEnumerable enumerable)