| | | 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> |
| | 2322 | 16 | | public class ActivityJsonConverter( |
| | 2322 | 17 | | IActivityRegistry activityRegistry, |
| | 2322 | 18 | | IExpressionDescriptorRegistry expressionDescriptorRegistry, |
| | 2322 | 19 | | ActivityWriter activityWriter, |
| | 2322 | 20 | | IServiceProvider serviceProvider) |
| | | 21 | | : JsonConverter<IActivity> |
| | | 22 | | { |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override IActivity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 25 | | { |
| | 2602 | 26 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 27 | | throw new JsonException("Failed to parse JsonDocument"); |
| | | 28 | | |
| | 2602 | 29 | | var activityRoot = doc.RootElement; |
| | 2602 | 30 | | var activityTypeName = GetActivityDetails(activityRoot, out var activityTypeVersion, out var activityDescriptor) |
| | 2602 | 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. |
| | 2602 | 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 | | |
| | 2602 | 40 | | var clonedOptions = GetClonedOptions(options); |
| | | 41 | | |
| | | 42 | | // If the activity type is not found, create a NotFoundActivity instead. |
| | 2602 | 43 | | if (activityDescriptor == null) |
| | | 44 | | { |
| | 7 | 45 | | var notFoundActivityDescriptor = activityRegistry.Find<NotFoundActivity>()!; |
| | 7 | 46 | | var notFoundActivity = JsonActivityConstructorContextHelper.CreateActivity<NotFoundActivity>(notFoundActivit |
| | | 47 | | |
| | 7 | 48 | | notFoundActivity.Type = notFoundActivityTypeName; |
| | 7 | 49 | | notFoundActivity.Version = 1; |
| | 7 | 50 | | notFoundActivity.MissingTypeName = activityTypeName; |
| | 7 | 51 | | notFoundActivity.MissingTypeVersion = activityTypeVersion; |
| | 7 | 52 | | notFoundActivity.OriginalActivityJson = activityRoot.ToString(); |
| | 7 | 53 | | notFoundActivity.SetDisplayText($"Not Found: {activityTypeName}"); |
| | 7 | 54 | | notFoundActivity.SetDescription($"Could not find activity type {activityTypeName} with version {activityType |
| | 7 | 55 | | return notFoundActivity; |
| | | 56 | | } |
| | | 57 | | |
| | 2595 | 58 | | var context = JsonActivityConstructorContextHelper.Create(activityDescriptor, activityRoot, clonedOptions); |
| | 2595 | 59 | | var activity = activityDescriptor.Constructor(context); |
| | 2595 | 60 | | return activity; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public override void Write(Utf8JsonWriter writer, IActivity value, JsonSerializerOptions options) |
| | | 65 | | { |
| | 1150 | 66 | | var clonedOptions = GetClonedWriterOptions(options); |
| | 1150 | 67 | | var activityDescriptor = activityRegistry.Find(value.Type, value.Version); |
| | | 68 | | |
| | | 69 | | // Give the activity descriptor a chance to customize the serializer options. |
| | 1150 | 70 | | clonedOptions = activityDescriptor?.ConfigureSerializerOptions?.Invoke(clonedOptions) ?? clonedOptions; |
| | | 71 | | |
| | 1150 | 72 | | activityWriter.WriteActivity(writer, value, clonedOptions); |
| | 1150 | 73 | | } |
| | | 74 | | |
| | | 75 | | private string GetActivityDetails(JsonElement activityRoot, out int activityTypeVersion, out ActivityDescriptor? act |
| | | 76 | | { |
| | 2602 | 77 | | if (!activityRoot.TryGetProperty("type", out var activityTypeNameElement)) |
| | 0 | 78 | | throw new JsonException("Failed to extract activity type property"); |
| | | 79 | | |
| | 2602 | 80 | | var activityTypeName = activityTypeNameElement.GetString()!; |
| | 2602 | 81 | | activityDescriptor = null; |
| | 2602 | 82 | | activityTypeVersion = 0; |
| | | 83 | | |
| | | 84 | | // First try and find the activity by its workflow definition version id. This is a special case when working wi |
| | 2602 | 85 | | if (activityRoot.TryGetProperty("workflowDefinitionVersionId", out var workflowDefinitionVersionIdElement)) |
| | | 86 | | { |
| | 527 | 87 | | var workflowDefinitionVersionId = workflowDefinitionVersionIdElement.GetString(); |
| | 527 | 88 | | activityDescriptor = activityRegistry.Find(x => |
| | 14629 | 89 | | x.CustomProperties.TryGetValue("WorkflowDefinitionVersionId", out var value) && (string?)value == workfl |
| | 527 | 90 | | activityTypeVersion = activityDescriptor?.Version ?? 0; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // If the activity type version is specified, use that to find the activity descriptor. |
| | 2602 | 94 | | if (activityDescriptor == null && activityRoot.TryGetProperty("version", out var activityVersionElement)) |
| | | 95 | | { |
| | 2097 | 96 | | activityTypeVersion = activityVersionElement.GetInt32(); |
| | 2097 | 97 | | activityDescriptor = activityRegistry.Find(activityTypeName, activityTypeVersion); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | // If the activity type version is not specified, use the latest version of the activity descriptor. |
| | 2602 | 101 | | if (activityDescriptor == null) |
| | | 102 | | { |
| | 7 | 103 | | activityDescriptor = activityRegistry.Find(activityTypeName); |
| | 7 | 104 | | activityTypeVersion = activityDescriptor?.Version ?? 0; |
| | | 105 | | } |
| | | 106 | | |
| | 2602 | 107 | | return activityTypeName; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | private JsonSerializerOptions GetClonedOptions(JsonSerializerOptions options) |
| | | 111 | | { |
| | 3752 | 112 | | var clonedOptions = new JsonSerializerOptions(options); |
| | 3752 | 113 | | clonedOptions.Converters.Add(new InputJsonConverterFactory(serviceProvider)); |
| | 3752 | 114 | | clonedOptions.Converters.Add(new OutputJsonConverterFactory(serviceProvider)); |
| | 3752 | 115 | | clonedOptions.Converters.Add(new ExpressionJsonConverterFactory(expressionDescriptorRegistry)); |
| | 3752 | 116 | | return clonedOptions; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | private JsonSerializerOptions GetClonedWriterOptions(JsonSerializerOptions options) |
| | | 120 | | { |
| | 1150 | 121 | | var clonedOptions = GetClonedOptions(options); |
| | 1150 | 122 | | clonedOptions.Converters.Add(new JsonIgnoreCompositeRootConverterFactory(serviceProvider.GetRequiredService<Acti |
| | 1150 | 123 | | return clonedOptions; |
| | | 124 | | } |
| | | 125 | | } |