| | | 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> |
| | 4946 | 17 | | public class ActivityJsonConverter( |
| | 4946 | 18 | | IActivityRegistry activityRegistry, |
| | 4946 | 19 | | IExpressionDescriptorRegistry expressionDescriptorRegistry, |
| | 4946 | 20 | | ActivityWriter activityWriter, |
| | 4946 | 21 | | IServiceProvider serviceProvider) |
| | | 22 | | : JsonConverter<IActivity> |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public override IActivity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 26 | | { |
| | 3768 | 27 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 28 | | throw new JsonException("Failed to parse JsonDocument"); |
| | | 29 | | |
| | 3768 | 30 | | using (doc) |
| | | 31 | | { |
| | 3768 | 32 | | JsonDocument? originalActivityDoc = null; |
| | | 33 | | |
| | | 34 | | try |
| | | 35 | | { |
| | 3768 | 36 | | var activityRoot = doc.RootElement; |
| | 3768 | 37 | | var activityTypeName = GetActivityDetails(activityRoot, out var activityTypeVersion, out var activityDes |
| | 3768 | 38 | | var notFoundActivityTypeName = ActivityTypeNameHelper.GenerateTypeName<NotFoundActivity>(); |
| | | 39 | | |
| | | 40 | | // If the activity type is a NotFoundActivity, try to extract the original activity type name and versio |
| | 3768 | 41 | | if (activityTypeName.Equals(notFoundActivityTypeName) && activityRoot.TryGetProperty("originalActivityJs |
| | | 42 | | { |
| | 1 | 43 | | originalActivityDoc = JsonDocument.Parse(originalActivityJson.GetString()!); |
| | 1 | 44 | | activityRoot = originalActivityDoc.RootElement; |
| | 1 | 45 | | activityTypeName = GetActivityDetails(activityRoot, out activityTypeVersion, out activityDescriptor) |
| | | 46 | | } |
| | | 47 | | |
| | 3768 | 48 | | var clonedOptions = GetClonedOptions(options); |
| | | 49 | | // If the activity type is not found, create a NotFoundActivity instead. |
| | 3768 | 50 | | if (activityDescriptor == null) |
| | | 51 | | { |
| | 174 | 52 | | var notFoundActivityDescriptor = activityRegistry.Find<NotFoundActivity>(); |
| | | 53 | | |
| | 174 | 54 | | if (notFoundActivityDescriptor == null) |
| | 1 | 55 | | throw new InvalidOperationException($"Unable to deserialize activity type '{activityTypeName}' b |
| | | 56 | | |
| | 173 | 57 | | var notFoundActivityResult = JsonActivityConstructorContextHelper.CreateActivity<NotFoundActivity>(n |
| | 173 | 58 | | LogExceptionsIfAny(notFoundActivityResult); |
| | | 59 | | |
| | 173 | 60 | | var notFoundActivity = notFoundActivityResult.Activity; |
| | 173 | 61 | | notFoundActivity.Type = notFoundActivityTypeName; |
| | 173 | 62 | | notFoundActivity.Version = 1; |
| | 173 | 63 | | notFoundActivity.MissingTypeName = activityTypeName; |
| | 173 | 64 | | notFoundActivity.MissingTypeVersion = activityTypeVersion; |
| | 173 | 65 | | notFoundActivity.OriginalActivityJson = activityRoot.ToString(); |
| | | 66 | | |
| | | 67 | | // Extract metadata from doc.RootElement rather than activityRoot. |
| | | 68 | | // In round-trip scenarios, activityRoot may have been reassigned to the inner originalActivityJson |
| | | 69 | | // but we want the metadata from the current activity being deserialized, which represents the NotFo |
| | | 70 | | // placeholder's position and annotations in the designer. |
| | 173 | 71 | | if (doc.RootElement.TryGetProperty("metadata", out var outerMetadataElement)) |
| | | 72 | | { |
| | 173 | 73 | | var outerMetadata = JsonSerializer.Deserialize<IDictionary<string, object>>(outerMetadataElement |
| | 173 | 74 | | if (outerMetadata != null) |
| | | 75 | | { |
| | 173 | 76 | | notFoundActivity.Metadata = outerMetadata; |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // Set display text and description after metadata assignment to ensure they always reflect the curr |
| | 173 | 81 | | notFoundActivity.SetDisplayText($"Not Found: {activityTypeName}"); |
| | 173 | 82 | | notFoundActivity.SetDescription($"Could not find activity type {activityTypeName} with version {acti |
| | | 83 | | |
| | 173 | 84 | | return notFoundActivity; |
| | | 85 | | } |
| | | 86 | | |
| | 3594 | 87 | | var context = JsonActivityConstructorContextHelper.Create(activityDescriptor, activityRoot, clonedOption |
| | 3594 | 88 | | var activityResult = activityDescriptor.Constructor(context); |
| | 3594 | 89 | | LogExceptionsIfAny(activityResult); |
| | | 90 | | |
| | 3594 | 91 | | return activityResult.Activity; |
| | | 92 | | } |
| | | 93 | | finally |
| | | 94 | | { |
| | 3768 | 95 | | originalActivityDoc?.Dispose(); |
| | 3768 | 96 | | } |
| | | 97 | | } |
| | 3767 | 98 | | } |
| | | 99 | | |
| | | 100 | | void LogExceptionsIfAny(ActivityConstructionResult result) |
| | | 101 | | { |
| | 3767 | 102 | | if (!result.HasExceptions) |
| | 3767 | 103 | | return; |
| | | 104 | | |
| | 0 | 105 | | var logger = serviceProvider.GetRequiredService<ILogger<ActivityJsonConverter>>(); |
| | 0 | 106 | | foreach (var exception in result.Exceptions) |
| | 0 | 107 | | logger.LogWarning("An exception was thrown while constructing activity with id '{activityId}': {Message}", r |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | | 111 | | public override void Write(Utf8JsonWriter writer, IActivity value, JsonSerializerOptions options) |
| | | 112 | | { |
| | 5559 | 113 | | var clonedOptions = GetClonedWriterOptions(options); |
| | 5559 | 114 | | var activityDescriptor = activityRegistry.Find(value.Type, value.Version); |
| | | 115 | | |
| | | 116 | | // Give the activity descriptor a chance to customize the serializer options. |
| | 5559 | 117 | | clonedOptions = activityDescriptor?.ConfigureSerializerOptions?.Invoke(clonedOptions) ?? clonedOptions; |
| | | 118 | | |
| | 5559 | 119 | | activityWriter.WriteActivity(writer, value, clonedOptions); |
| | 5559 | 120 | | } |
| | | 121 | | |
| | | 122 | | private string GetActivityDetails(JsonElement activityRoot, out int activityTypeVersion, out ActivityDescriptor? act |
| | | 123 | | { |
| | 3769 | 124 | | if (!activityRoot.TryGetProperty("type", out var activityTypeNameElement)) |
| | 0 | 125 | | throw new JsonException("Failed to extract activity type property"); |
| | | 126 | | |
| | 3769 | 127 | | var activityTypeName = activityTypeNameElement.GetString()!; |
| | 3769 | 128 | | activityDescriptor = null; |
| | 3769 | 129 | | activityTypeVersion = 0; |
| | | 130 | | |
| | | 131 | | // First, we check whether the activity type name is a 'well-known' activity; not a workflow-as-activity |
| | | 132 | | |
| | | 133 | | // If the activity type version is specified, use that to find the activity descriptor. |
| | 3769 | 134 | | if (activityRoot.TryGetProperty("version", out var activityVersionElement)) |
| | | 135 | | { |
| | 3762 | 136 | | activityTypeVersion = activityVersionElement.GetInt32(); |
| | 3762 | 137 | | activityDescriptor = activityRegistry.Find(activityTypeName, activityTypeVersion); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | // If a version is not specified, or activity with specified version is not found: use the latest version of the |
| | 3769 | 141 | | if (activityDescriptor == null) |
| | | 142 | | { |
| | 208 | 143 | | activityDescriptor = activityRegistry.Find(activityTypeName); |
| | 208 | 144 | | activityTypeVersion = activityDescriptor?.Version ?? 0; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | // This is a special case when working with the WorkflowDefinitionActivity: workflowDefinitionVersionId should b |
| | 3769 | 148 | | if (activityRoot.TryGetProperty("workflowDefinitionVersionId", out var workflowDefinitionVersionIdElement)) |
| | | 149 | | { |
| | 514 | 150 | | var activityDescriptorOverride = FindActivityDescriptorByCustomProperty("WorkflowDefinitionVersionId", workf |
| | 514 | 151 | | if (activityDescriptorOverride is null) |
| | 199 | 152 | | return activityTypeName; |
| | | 153 | | |
| | 315 | 154 | | activityDescriptor = activityDescriptorOverride; |
| | 315 | 155 | | activityTypeVersion = activityDescriptor.Version; |
| | | 156 | | } |
| | | 157 | | // This is also a special case when working with the WorkflowDefinitionActivity: if no 'well-known' activity cou |
| | 3255 | 158 | | else if (activityDescriptor is null |
| | 3255 | 159 | | && activityRoot.TryGetProperty("workflowDefinitionId", out var workflowDefinitionIdElement) |
| | 3255 | 160 | | && workflowDefinitionIdElement.ValueKind == JsonValueKind.String) |
| | | 161 | | { |
| | 1 | 162 | | activityDescriptor = FindActivityDescriptorByCustomProperty("WorkflowDefinitionId", workflowDefinitionIdElem |
| | 1 | 163 | | activityTypeVersion = activityDescriptor?.Version ?? 0; |
| | | 164 | | } |
| | | 165 | | |
| | 3570 | 166 | | return activityTypeName; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | private ActivityDescriptor? FindActivityDescriptorByCustomProperty(string customPropertyName, JsonElement valueEleme |
| | | 170 | | { |
| | 515 | 171 | | if (valueElement.ValueKind != JsonValueKind.String) |
| | 0 | 172 | | return null; |
| | | 173 | | |
| | 515 | 174 | | var searchValue = valueElement.GetString(); |
| | 19365 | 175 | | return activityRegistry.Find(x => x.CustomProperties.TryGetValue(customPropertyName, out var value) && (string?) |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | private JsonSerializerOptions GetClonedOptions(JsonSerializerOptions options) |
| | | 179 | | { |
| | 9327 | 180 | | var clonedOptions = new JsonSerializerOptions(options); |
| | 9327 | 181 | | clonedOptions.Converters.Add(new InputJsonConverterFactory(serviceProvider)); |
| | 9327 | 182 | | clonedOptions.Converters.Add(new OutputJsonConverterFactory(serviceProvider)); |
| | 9327 | 183 | | clonedOptions.Converters.Add(new ExpressionJsonConverterFactory(expressionDescriptorRegistry)); |
| | 9327 | 184 | | return clonedOptions; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | private JsonSerializerOptions GetClonedWriterOptions(JsonSerializerOptions options) |
| | | 188 | | { |
| | 5559 | 189 | | var clonedOptions = GetClonedOptions(options); |
| | 5559 | 190 | | clonedOptions.Converters.Add(new JsonIgnoreCompositeRootConverterFactory(serviceProvider.GetRequiredService<Acti |
| | 5559 | 191 | | return clonedOptions; |
| | | 192 | | } |
| | | 193 | | } |