| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Expressions.Contracts; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Activities; |
| | | 6 | | using Elsa.Workflows.Helpers; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using Elsa.Workflows.Serialization.Helpers; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows.Serialization.Converters; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// (De)serializes objects of type <see cref="IActivity"/>. |
| | | 16 | | /// </summary> |
| | 4665 | 17 | | public class ActivityJsonConverter( |
| | 4665 | 18 | | IActivityRegistry activityRegistry, |
| | 4665 | 19 | | IExpressionDescriptorRegistry expressionDescriptorRegistry, |
| | 4665 | 20 | | ActivityWriter activityWriter, |
| | 4665 | 21 | | IServiceProvider serviceProvider) |
| | | 22 | | : JsonConverter<IActivity> |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public override IActivity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 26 | | { |
| | 5574 | 27 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 28 | | throw new JsonException("Failed to parse JsonDocument"); |
| | | 29 | | |
| | 5574 | 30 | | var activityRoot = doc.RootElement; |
| | 5574 | 31 | | var activityTypeName = GetActivityDetails(activityRoot, out var activityTypeVersion, out var activityDescriptor) |
| | 5574 | 32 | | var notFoundActivityTypeName = ActivityTypeNameHelper.GenerateTypeName<NotFoundActivity>(); |
| | | 33 | | |
| | | 34 | | // If the activity type is a NotFoundActivity, try to extract the original activity type name and version. |
| | 5574 | 35 | | if (activityTypeName.Equals(notFoundActivityTypeName) && activityRoot.TryGetProperty("originalActivityJson", out |
| | | 36 | | { |
| | 0 | 37 | | activityRoot = JsonDocument.Parse(originalActivityJson.GetString()!).RootElement; |
| | 0 | 38 | | activityTypeName = GetActivityDetails(activityRoot, out activityTypeVersion, out activityDescriptor); |
| | | 39 | | } |
| | | 40 | | |
| | 5574 | 41 | | var clonedOptions = GetClonedOptions(options); |
| | | 42 | | // If the activity type is not found, create a NotFoundActivity instead. |
| | 5574 | 43 | | if (activityDescriptor == null) |
| | | 44 | | { |
| | 302 | 45 | | var notFoundActivityDescriptor = activityRegistry.Find<NotFoundActivity>()!; |
| | 302 | 46 | | var notFoundActivityResult = JsonActivityConstructorContextHelper.CreateActivity<NotFoundActivity>(notFoundA |
| | 302 | 47 | | LogExceptionsIfAny(notFoundActivityResult); |
| | | 48 | | |
| | 302 | 49 | | var notFoundActivity = notFoundActivityResult.Activity; |
| | 302 | 50 | | notFoundActivity.Type = notFoundActivityTypeName; |
| | 302 | 51 | | notFoundActivity.Version = 1; |
| | 302 | 52 | | notFoundActivity.MissingTypeName = activityTypeName; |
| | 302 | 53 | | notFoundActivity.MissingTypeVersion = activityTypeVersion; |
| | 302 | 54 | | notFoundActivity.OriginalActivityJson = activityRoot.ToString(); |
| | 302 | 55 | | notFoundActivity.SetDisplayText($"Not Found: {activityTypeName}"); |
| | 302 | 56 | | notFoundActivity.SetDescription($"Could not find activity type {activityTypeName} with version {activityType |
| | 302 | 57 | | return notFoundActivity; |
| | | 58 | | } |
| | | 59 | | |
| | 5272 | 60 | | var context = JsonActivityConstructorContextHelper.Create(activityDescriptor, activityRoot, clonedOptions); |
| | 5272 | 61 | | var activityResult = activityDescriptor.Constructor(context); |
| | 5272 | 62 | | LogExceptionsIfAny(activityResult); |
| | | 63 | | |
| | 5272 | 64 | | return activityResult.Activity; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | void LogExceptionsIfAny(ActivityConstructionResult result) |
| | | 68 | | { |
| | 5574 | 69 | | if (!result.HasExceptions) |
| | 5574 | 70 | | return; |
| | | 71 | | |
| | 0 | 72 | | var logger = serviceProvider.GetRequiredService<ILogger<ActivityJsonConverter>>(); |
| | 0 | 73 | | foreach (var exception in result.Exceptions) |
| | 0 | 74 | | logger.LogWarning("An exception was thrown while constructing activity with id '{activityId}': {Message}", r |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public override void Write(Utf8JsonWriter writer, IActivity value, JsonSerializerOptions options) |
| | | 79 | | { |
| | 3454 | 80 | | var clonedOptions = GetClonedWriterOptions(options); |
| | 3454 | 81 | | var activityDescriptor = activityRegistry.Find(value.Type, value.Version); |
| | | 82 | | |
| | | 83 | | // Give the activity descriptor a chance to customize the serializer options. |
| | 3454 | 84 | | clonedOptions = activityDescriptor?.ConfigureSerializerOptions?.Invoke(clonedOptions) ?? clonedOptions; |
| | | 85 | | |
| | 3454 | 86 | | activityWriter.WriteActivity(writer, value, clonedOptions); |
| | 3454 | 87 | | } |
| | | 88 | | |
| | | 89 | | private string GetActivityDetails(JsonElement activityRoot, out int activityTypeVersion, out ActivityDescriptor? act |
| | | 90 | | { |
| | 5574 | 91 | | if (!activityRoot.TryGetProperty("type", out var activityTypeNameElement)) |
| | 0 | 92 | | throw new JsonException("Failed to extract activity type property"); |
| | | 93 | | |
| | 5574 | 94 | | var activityTypeName = activityTypeNameElement.GetString()!; |
| | 5574 | 95 | | activityDescriptor = null; |
| | 5574 | 96 | | activityTypeVersion = 0; |
| | | 97 | | |
| | | 98 | | // First, we check whether the activity type name is a 'well-known' activity; not a workflow-as-activity |
| | | 99 | | |
| | | 100 | | // If the activity type version is specified, use that to find the activity descriptor. |
| | 5574 | 101 | | if (activityRoot.TryGetProperty("version", out var activityVersionElement)) |
| | | 102 | | { |
| | 5569 | 103 | | activityTypeVersion = activityVersionElement.GetInt32(); |
| | 5569 | 104 | | activityDescriptor = activityRegistry.Find(activityTypeName, activityTypeVersion); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // If a version is not specified, or activity with specified version is not found: use the latest version of the |
| | 5574 | 108 | | if (activityDescriptor == null) |
| | | 109 | | { |
| | 341 | 110 | | activityDescriptor = activityRegistry.Find(activityTypeName); |
| | 341 | 111 | | activityTypeVersion = activityDescriptor?.Version ?? 0; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // This is a special case when working with the WorkflowDefinitionActivity: workflowDefinitionVersionId should b |
| | 5574 | 115 | | if (activityRoot.TryGetProperty("workflowDefinitionVersionId", out var workflowDefinitionVersionIdElement)) |
| | | 116 | | { |
| | 734 | 117 | | var activityDescriptorOverride = FindActivityDescriptorByCustomProperty("WorkflowDefinitionVersionId", workf |
| | 734 | 118 | | if (activityDescriptorOverride is null) |
| | 334 | 119 | | return activityTypeName; |
| | | 120 | | |
| | 400 | 121 | | activityDescriptor = activityDescriptorOverride; |
| | 400 | 122 | | activityTypeVersion = activityDescriptor.Version; |
| | | 123 | | } |
| | | 124 | | // This is also a special case when working with the WorkflowDefinitionActivity: if no 'well-known' activity cou |
| | 4840 | 125 | | else if (activityDescriptor is null |
| | 4840 | 126 | | && activityRoot.TryGetProperty("workflowDefinitionId", out var workflowDefinitionIdElement) |
| | 4840 | 127 | | && workflowDefinitionIdElement.ValueKind == JsonValueKind.String) |
| | | 128 | | { |
| | 1 | 129 | | activityDescriptor = FindActivityDescriptorByCustomProperty("WorkflowDefinitionId", workflowDefinitionIdElem |
| | 1 | 130 | | activityTypeVersion = activityDescriptor?.Version ?? 0; |
| | | 131 | | } |
| | | 132 | | |
| | 5240 | 133 | | return activityTypeName; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | private ActivityDescriptor? FindActivityDescriptorByCustomProperty(string customPropertyName, JsonElement valueEleme |
| | | 137 | | { |
| | 735 | 138 | | if (valueElement.ValueKind != JsonValueKind.String) |
| | 0 | 139 | | return null; |
| | | 140 | | |
| | 735 | 141 | | var searchValue = valueElement.GetString(); |
| | 30211 | 142 | | return activityRegistry.Find(x => x.CustomProperties.TryGetValue(customPropertyName, out var value) && (string?) |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | private JsonSerializerOptions GetClonedOptions(JsonSerializerOptions options) |
| | | 146 | | { |
| | 9028 | 147 | | var clonedOptions = new JsonSerializerOptions(options); |
| | 9028 | 148 | | clonedOptions.Converters.Add(new InputJsonConverterFactory(serviceProvider)); |
| | 9028 | 149 | | clonedOptions.Converters.Add(new OutputJsonConverterFactory(serviceProvider)); |
| | 9028 | 150 | | clonedOptions.Converters.Add(new ExpressionJsonConverterFactory(expressionDescriptorRegistry)); |
| | 9028 | 151 | | return clonedOptions; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private JsonSerializerOptions GetClonedWriterOptions(JsonSerializerOptions options) |
| | | 155 | | { |
| | 3454 | 156 | | var clonedOptions = GetClonedOptions(options); |
| | 3454 | 157 | | clonedOptions.Converters.Add(new JsonIgnoreCompositeRootConverterFactory(serviceProvider.GetRequiredService<Acti |
| | 3454 | 158 | | return clonedOptions; |
| | | 159 | | } |
| | | 160 | | } |