| | | 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 | | |
| | | 11 | | namespace Elsa.Workflows.Serialization.Converters; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// (De)serializes objects of type <see cref="IActivity"/>. |
| | | 15 | | /// </summary> |
| | 4050 | 16 | | public class ActivityJsonConverter( |
| | 4050 | 17 | | IActivityRegistry activityRegistry, |
| | 4050 | 18 | | IExpressionDescriptorRegistry expressionDescriptorRegistry, |
| | 4050 | 19 | | ActivityWriter activityWriter, |
| | 4050 | 20 | | IServiceProvider serviceProvider) |
| | | 21 | | : JsonConverter<IActivity> |
| | | 22 | | { |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override IActivity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 25 | | { |
| | 3593 | 26 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 27 | | throw new JsonException("Failed to parse JsonDocument"); |
| | | 28 | | |
| | 3593 | 29 | | var activityRoot = doc.RootElement; |
| | 3593 | 30 | | var activityTypeName = GetActivityDetails(activityRoot, out var activityTypeVersion, out var activityDescriptor) |
| | 3593 | 31 | | var notFoundActivityTypeName = ActivityTypeNameHelper.GenerateTypeName<NotFoundActivity>(); |
| | | 32 | | |
| | | 33 | | // If the activity type is a NotFoundActivity, try to extract the original activity type name and version. |
| | 3593 | 34 | | if (activityTypeName.Equals(notFoundActivityTypeName) && activityRoot.TryGetProperty("originalActivityJson", out |
| | | 35 | | { |
| | 0 | 36 | | activityRoot = JsonDocument.Parse(originalActivityJson.GetString()!).RootElement; |
| | 0 | 37 | | activityTypeName = GetActivityDetails(activityRoot, out activityTypeVersion, out activityDescriptor); |
| | | 38 | | } |
| | | 39 | | |
| | 3593 | 40 | | var clonedOptions = GetClonedOptions(options); |
| | | 41 | | |
| | | 42 | | // If the activity type is not found, create a NotFoundActivity instead. |
| | 3593 | 43 | | if (activityDescriptor == null) |
| | | 44 | | { |
| | 8 | 45 | | var notFoundActivityDescriptor = activityRegistry.Find<NotFoundActivity>()!; |
| | 8 | 46 | | var notFoundActivity = JsonActivityConstructorContextHelper.CreateActivity<NotFoundActivity>(notFoundActivit |
| | | 47 | | |
| | 8 | 48 | | notFoundActivity.Type = notFoundActivityTypeName; |
| | 8 | 49 | | notFoundActivity.Version = 1; |
| | 8 | 50 | | notFoundActivity.MissingTypeName = activityTypeName; |
| | 8 | 51 | | notFoundActivity.MissingTypeVersion = activityTypeVersion; |
| | 8 | 52 | | notFoundActivity.OriginalActivityJson = activityRoot.ToString(); |
| | 8 | 53 | | notFoundActivity.SetDisplayText($"Not Found: {activityTypeName}"); |
| | 8 | 54 | | notFoundActivity.SetDescription($"Could not find activity type {activityTypeName} with version {activityType |
| | 8 | 55 | | return notFoundActivity; |
| | | 56 | | } |
| | | 57 | | |
| | 3585 | 58 | | var context = JsonActivityConstructorContextHelper.Create(activityDescriptor, activityRoot, clonedOptions); |
| | 3585 | 59 | | var activity = activityDescriptor.Constructor(context); |
| | 3585 | 60 | | return activity; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public override void Write(Utf8JsonWriter writer, IActivity value, JsonSerializerOptions options) |
| | | 65 | | { |
| | 3454 | 66 | | var clonedOptions = GetClonedWriterOptions(options); |
| | 3454 | 67 | | var activityDescriptor = activityRegistry.Find(value.Type, value.Version); |
| | | 68 | | |
| | | 69 | | // Give the activity descriptor a chance to customize the serializer options. |
| | 3454 | 70 | | clonedOptions = activityDescriptor?.ConfigureSerializerOptions?.Invoke(clonedOptions) ?? clonedOptions; |
| | | 71 | | |
| | 3454 | 72 | | activityWriter.WriteActivity(writer, value, clonedOptions); |
| | 3454 | 73 | | } |
| | | 74 | | |
| | | 75 | | private string GetActivityDetails(JsonElement activityRoot, out int activityTypeVersion, out ActivityDescriptor? act |
| | | 76 | | { |
| | 3593 | 77 | | if (!activityRoot.TryGetProperty("type", out var activityTypeNameElement)) |
| | 0 | 78 | | throw new JsonException("Failed to extract activity type property"); |
| | | 79 | | |
| | 3593 | 80 | | var activityTypeName = activityTypeNameElement.GetString()!; |
| | 3593 | 81 | | activityDescriptor = null; |
| | 3593 | 82 | | activityTypeVersion = 0; |
| | | 83 | | |
| | | 84 | | // First, we check whether the activity type name is a 'well-known' activity; not a workflow-as-activity |
| | | 85 | | |
| | | 86 | | // If the activity type version is specified, use that to find the activity descriptor. |
| | 3593 | 87 | | if (activityRoot.TryGetProperty("version", out var activityVersionElement)) |
| | | 88 | | { |
| | 3588 | 89 | | activityTypeVersion = activityVersionElement.GetInt32(); |
| | 3588 | 90 | | activityDescriptor = activityRegistry.Find(activityTypeName, activityTypeVersion); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // If a version is not specified, or activity with specified version is not found: use the latest version of the |
| | 3593 | 94 | | if (activityDescriptor == null) |
| | | 95 | | { |
| | 47 | 96 | | activityDescriptor = activityRegistry.Find(activityTypeName); |
| | 47 | 97 | | activityTypeVersion = activityDescriptor?.Version ?? 0; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | // This is a special case when working with the WorkflowDefinitionActivity: workflowDefinitionVersionId should b |
| | 3593 | 101 | | if (activityRoot.TryGetProperty("workflowDefinitionVersionId", out var workflowDefinitionVersionIdElement)) |
| | | 102 | | { |
| | 558 | 103 | | var activityDescriptorOverride = FindActivityDescriptorByCustomProperty("WorkflowDefinitionVersionId", workf |
| | 558 | 104 | | if (activityDescriptorOverride is null) |
| | 40 | 105 | | return activityTypeName; |
| | | 106 | | |
| | 518 | 107 | | activityDescriptor = activityDescriptorOverride; |
| | 518 | 108 | | activityTypeVersion = activityDescriptor.Version; |
| | | 109 | | } |
| | | 110 | | // This is also a special case when working with the WorkflowDefinitionActivity: if no 'well-known' activity cou |
| | 3035 | 111 | | else if (activityDescriptor is null |
| | 3035 | 112 | | && activityRoot.TryGetProperty("workflowDefinitionId", out var workflowDefinitionIdElement) |
| | 3035 | 113 | | && workflowDefinitionIdElement.ValueKind == JsonValueKind.String) |
| | | 114 | | { |
| | 1 | 115 | | activityDescriptor = FindActivityDescriptorByCustomProperty("WorkflowDefinitionId", workflowDefinitionIdElem |
| | 1 | 116 | | activityTypeVersion = activityDescriptor?.Version ?? 0; |
| | | 117 | | } |
| | | 118 | | |
| | 3553 | 119 | | return activityTypeName; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private ActivityDescriptor? FindActivityDescriptorByCustomProperty(string customPropertyName, JsonElement valueEleme |
| | | 123 | | { |
| | 559 | 124 | | if (valueElement.ValueKind != JsonValueKind.String) |
| | 0 | 125 | | return null; |
| | | 126 | | |
| | 559 | 127 | | var searchValue = valueElement.GetString(); |
| | 15609 | 128 | | return activityRegistry.Find(x => x.CustomProperties.TryGetValue(customPropertyName, out var value) && (string?) |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private JsonSerializerOptions GetClonedOptions(JsonSerializerOptions options) |
| | | 132 | | { |
| | 7047 | 133 | | var clonedOptions = new JsonSerializerOptions(options); |
| | 7047 | 134 | | clonedOptions.Converters.Add(new InputJsonConverterFactory(serviceProvider)); |
| | 7047 | 135 | | clonedOptions.Converters.Add(new OutputJsonConverterFactory(serviceProvider)); |
| | 7047 | 136 | | clonedOptions.Converters.Add(new ExpressionJsonConverterFactory(expressionDescriptorRegistry)); |
| | 7047 | 137 | | return clonedOptions; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private JsonSerializerOptions GetClonedWriterOptions(JsonSerializerOptions options) |
| | | 141 | | { |
| | 3454 | 142 | | var clonedOptions = GetClonedOptions(options); |
| | 3454 | 143 | | clonedOptions.Converters.Add(new JsonIgnoreCompositeRootConverterFactory(serviceProvider.GetRequiredService<Acti |
| | 3454 | 144 | | return clonedOptions; |
| | | 145 | | } |
| | | 146 | | } |