From 2b77ac8fe0dcd614506a230f327907eb124c35ae Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 28 May 2021 16:38:59 +0200 Subject: [PATCH] Handle potential null dictionary --- .../Services/ActivityPropertyProviders.cs | 19 +++++++++++-------- .../Workflows/ForEachWorkflow.cs | 4 ++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/core/Elsa.Abstractions/Services/ActivityPropertyProviders.cs b/src/core/Elsa.Abstractions/Services/ActivityPropertyProviders.cs index efe92e299..1a56897c8 100644 --- a/src/core/Elsa.Abstractions/Services/ActivityPropertyProviders.cs +++ b/src/core/Elsa.Abstractions/Services/ActivityPropertyProviders.cs @@ -36,13 +36,16 @@ namespace Elsa.Services properties[propertyName] = provider; } - public IDictionary GetProviders(string activityId) => _providers.TryGetValue(activityId, out var properties) ? properties : new Dictionary(); + public IDictionary GetProviders(string activityId) => + _providers.TryGetValue(activityId, out var properties) + ? properties ?? new Dictionary() + : new Dictionary(); public IActivityPropertyValueProvider? GetProvider(string activityId, string propertyName) => - _providers.TryGetValue(activityId, out var properties) - && properties != null - && properties.TryGetValue(propertyName, out var provider) - ? provider + _providers.TryGetValue(activityId, out var properties) + && properties != null + && properties.TryGetValue(propertyName, out var provider) + ? provider : null; public async ValueTask SetActivityPropertiesAsync(IActivity activity, ActivityExecutionContext activityExecutionContext, CancellationToken cancellationToken = default) @@ -64,14 +67,14 @@ namespace Elsa.Services var activityPropertyAttribute = property.GetCustomAttribute(); value = activityPropertyAttribute?.DefaultValue; } - - if(value != null) + + if (value != null) { property.SetValue(activity, value); activityExecutionContext.SetState(property.Name, value); } } - catch(Exception e) + catch (Exception e) { throw new CannotSetActivityPropertyValueException($@"An exception was thrown whilst setting '{activity?.GetType().Name}.{property.Name}'. See the inner exception for further details.", e); } diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/ForEachWorkflow.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/ForEachWorkflow.cs index 414b39c43..21f738806 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/ForEachWorkflow.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/ForEachWorkflow.cs @@ -22,8 +22,8 @@ namespace Elsa.Core.IntegrationTests.Workflows .ForEach( _items, iterate => iterate - .Then(activity => activity.Set(x => x.Text, context => $"{context.Input}")).WithId("WriteLine") - .Then() /* Block workflow.*/ + .Then(activity => activity.WithText(context => $"{context.Input}")).WithId("WriteLine") + .SignalReceived("The Signal") /* Block workflow.*/ .WriteLine("Resumed")) .WriteLine("One iterations executing, rest is blocked"); }