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