| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Workflows.Management.Models; |
| | | 3 | | using Elsa.Workflows.Management.Notifications; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Management.Handlers.Notifications; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Validates output-converter bindings before a workflow definition is accepted. |
| | | 11 | | /// </summary> |
| | 401 | 12 | | public class ValidateOutputConverters( |
| | 401 | 13 | | IWorkflowGraphBuilder workflowGraphBuilder, |
| | 401 | 14 | | IActivityRegistryLookupService activityRegistryLookupService, |
| | 401 | 15 | | IOutputConverterRegistry outputConverterRegistry, |
| | 401 | 16 | | IOutputBindingDestinationResolver destinationResolver, |
| | 401 | 17 | | IOutputConverterSettingsValidator settingsValidator, |
| | 401 | 18 | | IServiceProvider serviceProvider) : INotificationHandler<WorkflowDefinitionValidating> |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public async Task HandleAsync(WorkflowDefinitionValidating notification, CancellationToken cancellationToken) |
| | | 22 | | { |
| | 8 | 23 | | var graph = await workflowGraphBuilder.BuildAsync(notification.Workflow, cancellationToken); |
| | | 24 | | |
| | 58 | 25 | | foreach (var node in graph.Nodes) |
| | | 26 | | { |
| | 21 | 27 | | var activity = node.Activity; |
| | 21 | 28 | | var activityDescriptor = await activityRegistryLookupService.FindAsync(activity.Type, activity.Version); |
| | | 29 | | |
| | 21 | 30 | | if (activityDescriptor == null) |
| | | 31 | | continue; |
| | | 32 | | |
| | 88 | 33 | | foreach (var outputDescriptor in activityDescriptor.Outputs) |
| | | 34 | | { |
| | 23 | 35 | | if (outputDescriptor.ValueGetter(activity) is not Output { Converter: { } configuration } output) |
| | | 36 | | continue; |
| | | 37 | | |
| | 3 | 38 | | ValidateBinding( |
| | 3 | 39 | | graph, |
| | 3 | 40 | | node, |
| | 3 | 41 | | output, |
| | 3 | 42 | | outputDescriptor, |
| | 3 | 43 | | configuration, |
| | 3 | 44 | | notification.ValidationErrors); |
| | | 45 | | } |
| | 21 | 46 | | } |
| | 8 | 47 | | } |
| | | 48 | | |
| | | 49 | | private void ValidateBinding( |
| | | 50 | | WorkflowGraph graph, |
| | | 51 | | ActivityNode node, |
| | | 52 | | Output output, |
| | | 53 | | OutputDescriptor outputDescriptor, |
| | | 54 | | OutputConverterConfiguration configuration, |
| | | 55 | | ICollection<WorkflowValidationError> validationErrors) |
| | | 56 | | { |
| | 3 | 57 | | var activity = node.Activity; |
| | | 58 | | |
| | | 59 | | void AddError(string message) => |
| | 2 | 60 | | validationErrors.Add(new($"Output '{outputDescriptor.Name}' converter: {message}", activity.Id)); |
| | | 61 | | |
| | 3 | 62 | | if (string.IsNullOrWhiteSpace(configuration.Id)) |
| | | 63 | | { |
| | 0 | 64 | | AddError("a converter ID is required."); |
| | 0 | 65 | | return; |
| | | 66 | | } |
| | | 67 | | |
| | 3 | 68 | | var destination = destinationResolver.Resolve(graph, node, output); |
| | | 69 | | |
| | 3 | 70 | | if (destination == null) |
| | | 71 | | { |
| | 1 | 72 | | AddError("a converter can only be configured when the output has a variable or workflow-output destination." |
| | 1 | 73 | | return; |
| | | 74 | | } |
| | | 75 | | |
| | 2 | 76 | | var registration = outputConverterRegistry.FindRegistration(configuration.Id); |
| | | 77 | | |
| | 2 | 78 | | if (registration == null) |
| | | 79 | | { |
| | 1 | 80 | | AddError($"converter '{configuration.Id}' is not registered."); |
| | 1 | 81 | | return; |
| | | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | var converterDescriptor = registration.Descriptor; |
| | | 85 | | |
| | 1 | 86 | | if (!converterDescriptor.SourceType.IsAssignableFrom(outputDescriptor.Type)) |
| | 0 | 87 | | AddError($"converter '{configuration.Id}' cannot accept declared source type '{outputDescriptor.Type.FullNam |
| | | 88 | | |
| | 1 | 89 | | if (!CanAssign(converterDescriptor.ResultType, destination.Type)) |
| | 0 | 90 | | AddError($"converter '{configuration.Id}' result type '{converterDescriptor.ResultType.FullName}' cannot be |
| | | 91 | | |
| | | 92 | | IOutputConverter converter; |
| | | 93 | | |
| | | 94 | | try |
| | | 95 | | { |
| | 1 | 96 | | converter = serviceProvider.GetRequiredKeyedService<IOutputConverter>(registration.ServiceKey); |
| | 1 | 97 | | } |
| | 0 | 98 | | catch |
| | | 99 | | { |
| | 0 | 100 | | AddError($"converter '{configuration.Id}' could not be resolved."); |
| | 0 | 101 | | return; |
| | | 102 | | } |
| | | 103 | | |
| | 2 | 104 | | foreach (var settingsError in settingsValidator.Validate(converterDescriptor, converter, configuration.Settings) |
| | 0 | 105 | | AddError($"converter '{configuration.Id}' settings are invalid: {settingsError}"); |
| | 1 | 106 | | } |
| | | 107 | | |
| | | 108 | | private static bool CanAssign(Type sourceType, Type destinationType) |
| | | 109 | | { |
| | 1 | 110 | | if (destinationType.IsAssignableFrom(sourceType)) |
| | 1 | 111 | | return true; |
| | | 112 | | |
| | 0 | 113 | | return Nullable.GetUnderlyingType(destinationType)?.IsAssignableFrom(sourceType) == true; |
| | | 114 | | } |
| | | 115 | | } |