| | | 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.Flowchart.Models; |
| | | 6 | | using Elsa.Workflows.Memory; |
| | | 7 | | using Elsa.Workflows.Serialization.Converters; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Activities.Flowchart.Serialization; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A JSON converter for <see cref="Activities.Flowchart"/>. |
| | | 13 | | /// </summary> |
| | 771 | 14 | | public class FlowchartJsonConverter(IIdentityGenerator identityGenerator, IWellKnownTypeRegistry wellKnownTypeRegistry) |
| | | 15 | | { |
| | | 16 | | private const string AllActivitiesKey = "allActivities"; |
| | | 17 | | private const string AllConnectionsKey = "allConnections"; |
| | | 18 | | private const string NotFoundConnectionsKey = "notFoundConnections"; |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public override Activities.Flowchart Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions optio |
| | | 22 | | { |
| | 1061 | 23 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 24 | | throw new JsonException("Failed to parse JsonDocument"); |
| | | 25 | | |
| | 1061 | 26 | | var id = doc.RootElement.TryGetProperty("id", out var idAttribute) ? idAttribute.GetString()! : identityGenerato |
| | 1061 | 27 | | var nodeId = doc.RootElement.TryGetProperty("nodeId", out var nodeIdAttribute) ? nodeIdAttribute.GetString() : n |
| | 1061 | 28 | | var name = doc.RootElement.TryGetProperty("name", out var nameElement) ? nameElement.GetString() : null; |
| | 1061 | 29 | | var type = doc.RootElement.TryGetProperty("type", out var typeElement) ? typeElement.GetString() : null; |
| | 1061 | 30 | | var version = doc.RootElement.TryGetProperty("version", out var versionElement) ? versionElement.GetInt32() : 1; |
| | 1061 | 31 | | var runAsynchronously = doc.RootElement.TryGetProperty("runAsynchronously", out var runAsyncElement) && runAsync |
| | | 32 | | |
| | 1061 | 33 | | var connectionsElement = doc.RootElement.TryGetProperty("connections", out var connectionsEl) ? connectionsEl : |
| | 1061 | 34 | | var activitiesElement = doc.RootElement.TryGetProperty("activities", out var activitiesEl) ? activitiesEl : defa |
| | 1061 | 35 | | var activities = activitiesElement.ValueKind != JsonValueKind.Undefined ? activitiesElement.Deserialize<ICollect |
| | 2594 | 36 | | var activityDictionary = activities.ToDictionary(x => x.Id); |
| | 1061 | 37 | | var connections = DeserializeConnections(connectionsElement, activityDictionary, options); |
| | 1061 | 38 | | var notFoundConnections = GetNotFoundConnections(doc.RootElement, activityDictionary, connections, options); |
| | 1061 | 39 | | var connectionsToRestore = FindConnectionsThatCanBeRestored(notFoundConnections, activities); |
| | 1061 | 40 | | var connectionComparer = new ConnectionComparer(); |
| | 1061 | 41 | | var connectionsWithRestoredOnes = connections.Except(notFoundConnections, connectionComparer).Union(connectionsT |
| | | 42 | | |
| | 1061 | 43 | | var variablesElement = doc.RootElement.TryGetProperty("variables", out var variablesEl) ? variablesEl : default; |
| | 1061 | 44 | | var variables = variablesElement.ValueKind != JsonValueKind.Undefined ? variablesElement.Deserialize<ICollection |
| | | 45 | | |
| | 1061 | 46 | | JsonSerializerOptions polymorphicOptions = options.Clone(); |
| | 1061 | 47 | | polymorphicOptions.Converters.Add(new PolymorphicDictionaryConverter(options, wellKnownTypeRegistry)); |
| | | 48 | | |
| | 1061 | 49 | | var metadataElement = doc.RootElement.TryGetProperty("metadata", out var metadataEl) ? metadataEl : default; |
| | 1061 | 50 | | var metadata = metadataElement.ValueKind != JsonValueKind.Undefined ? metadataElement.Deserialize<IDictionary<st |
| | | 51 | | |
| | 1061 | 52 | | var customPropertiesElement = doc.RootElement.TryGetProperty("customProperties", out var customPropertiesEl) ? c |
| | 1061 | 53 | | var customProperties = customPropertiesEl.ValueKind != JsonValueKind.Undefined ? customPropertiesElement.Deseria |
| | 1061 | 54 | | customProperties[AllActivitiesKey] = activities.ToList(); |
| | 1061 | 55 | | customProperties[AllConnectionsKey] = connectionsWithRestoredOnes; |
| | 1061 | 56 | | customProperties[NotFoundConnectionsKey] = notFoundConnections.Except(connectionsToRestore, connectionComparer). |
| | | 57 | | |
| | 1061 | 58 | | var flowChart = new Activities.Flowchart |
| | 1061 | 59 | | { |
| | 1061 | 60 | | Id = id, |
| | 1061 | 61 | | NodeId = nodeId!, |
| | 1061 | 62 | | Name = name, |
| | 1061 | 63 | | Type = type!, |
| | 1061 | 64 | | RunAsynchronously = runAsynchronously, |
| | 1061 | 65 | | Version = version, |
| | 1061 | 66 | | CustomProperties = customProperties, |
| | 1061 | 67 | | Metadata = metadata, |
| | 1061 | 68 | | Activities = activities, |
| | 1061 | 69 | | Variables = variables, |
| | 1061 | 70 | | Connections = connectionsWithRestoredOnes, |
| | 1061 | 71 | | }; |
| | | 72 | | |
| | 1061 | 73 | | return flowChart; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public override void Write(Utf8JsonWriter writer, Activities.Flowchart value, JsonSerializerOptions options) |
| | | 78 | | { |
| | 195 | 79 | | var activities = value.Activities; |
| | 561 | 80 | | var activityDictionary = activities.ToDictionary(x => x.Id); |
| | | 81 | | |
| | 195 | 82 | | var customProperties = new Dictionary<string, object>(value.CustomProperties); |
| | 195 | 83 | | var allActivities = customProperties.GetValueOrDefault(AllActivitiesKey, activities); |
| | 195 | 84 | | var allConnections = (ICollection<Connection>)(customProperties.TryGetValue(AllConnectionsKey, out var c) ? c : |
| | | 85 | | |
| | 195 | 86 | | customProperties.Remove(AllActivitiesKey); |
| | 195 | 87 | | customProperties.Remove(AllConnectionsKey); |
| | | 88 | | |
| | 195 | 89 | | var model = new |
| | 195 | 90 | | { |
| | 195 | 91 | | value.Id, |
| | 195 | 92 | | value.NodeId, |
| | 195 | 93 | | value.Name, |
| | 195 | 94 | | value.Type, |
| | 195 | 95 | | value.Version, |
| | 195 | 96 | | CustomProperties = customProperties, |
| | 195 | 97 | | value.Metadata, |
| | 195 | 98 | | Activities = allActivities, |
| | 195 | 99 | | value.Variables, |
| | 195 | 100 | | Connections = allConnections, |
| | 195 | 101 | | }; |
| | | 102 | | |
| | 195 | 103 | | var flowchartSerializerOptions = new JsonSerializerOptions(options); |
| | 195 | 104 | | flowchartSerializerOptions.Converters.Add(new ConnectionJsonConverter(activityDictionary)); |
| | 195 | 105 | | flowchartSerializerOptions.Converters.Add(new PolymorphicDictionaryConverter(options, wellKnownTypeRegistry)); |
| | | 106 | | |
| | 195 | 107 | | JsonSerializer.Serialize(writer, model, flowchartSerializerOptions); |
| | 195 | 108 | | } |
| | | 109 | | |
| | | 110 | | private static ICollection<Connection> GetNotFoundConnections(JsonElement rootElement, IDictionary<string, IActivity |
| | | 111 | | { |
| | 1061 | 112 | | var customPropertiesElement = rootElement.TryGetProperty("customProperties", out var customPropertiesEl) ? custo |
| | | 113 | | |
| | 1061 | 114 | | var notFoundConnectionsElement = |
| | 1061 | 115 | | customPropertiesElement.ValueKind != JsonValueKind.Undefined |
| | 1061 | 116 | | ? customPropertiesElement.TryGetProperty(NotFoundConnectionsKey, out var notFoundConnectionsEl) |
| | 1061 | 117 | | ? notFoundConnectionsEl |
| | 1061 | 118 | | : default |
| | 1061 | 119 | | : default; |
| | 1061 | 120 | | var notFoundConnections = notFoundConnectionsElement.ValueKind != JsonValueKind.Undefined ? DeserializeConnectio |
| | | 121 | | |
| | | 122 | | // Add connections of NotFoundActivity to the list if they aren't already in it. |
| | 2594 | 123 | | var notFoundActivities = activities.Values.Where(x => x is NotFoundActivity).Cast<NotFoundActivity>().ToList(); |
| | 1533 | 124 | | var notFoundActivityConnections = connections.Where(x => notFoundActivities.Contains(x.Source.Activity)).ToList( |
| | | 125 | | |
| | 2124 | 126 | | foreach (var notFoundConnection in notFoundActivityConnections) |
| | | 127 | | { |
| | 1 | 128 | | if (notFoundConnections.All(x => x.Source != notFoundConnection.Source || x.Target != notFoundConnection.Tar |
| | 1 | 129 | | notFoundConnections.Add(notFoundConnection); |
| | | 130 | | } |
| | | 131 | | |
| | 1061 | 132 | | return notFoundConnections; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | private static List<Connection> FindConnectionsThatCanBeRestored(IEnumerable<Connection> notFoundConnections, IEnume |
| | | 136 | | { |
| | 1061 | 137 | | var connectionsThatCanBeRestored = new List<Connection>(); |
| | 2594 | 138 | | var foundActivities = activities.Where(x => x is not NotFoundActivity).ToList(); |
| | | 139 | | |
| | 2124 | 140 | | foreach (var notFoundConnection in notFoundConnections.ToList()) |
| | | 141 | | { |
| | 1 | 142 | | var missingSource = notFoundConnection.Source; |
| | 1 | 143 | | var missingTarget = notFoundConnection.Target; |
| | | 144 | | |
| | | 145 | | // ReSharper disable ConditionalAccessQualifierIsNonNullableAccordingToAPIContract |
| | | 146 | | // Activity might be null in case of JSON missing information. |
| | 1 | 147 | | var source = foundActivities.FirstOrDefault(x => x.Id == missingSource.Activity?.Id); |
| | 1 | 148 | | var target = foundActivities.FirstOrDefault(x => x.Id == missingTarget.Activity?.Id); |
| | | 149 | | // ReSharper restore ConditionalAccessQualifierIsNonNullableAccordingToAPIContract |
| | | 150 | | |
| | 1 | 151 | | if (source == null || target == null) |
| | | 152 | | continue; |
| | | 153 | | |
| | 0 | 154 | | var connection = new Connection(new Endpoint(source, missingSource.Port), new Endpoint(target, missingTarget |
| | 0 | 155 | | connectionsThatCanBeRestored.Add(connection); |
| | | 156 | | } |
| | | 157 | | |
| | 1061 | 158 | | return connectionsThatCanBeRestored; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | private static ICollection<Connection> DeserializeConnections(JsonElement connectionsElement, IDictionary<string, IA |
| | | 162 | | { |
| | | 163 | | // 1) Nothing → empty |
| | 2068 | 164 | | if (connectionsElement.ValueKind == JsonValueKind.Undefined || connectionsElement.ValueKind == JsonValueKind.Nul |
| | 0 | 165 | | return new List<Connection>(); |
| | | 166 | | |
| | | 167 | | // 2) OData‐style wrapper: { "$values": [ … ] } |
| | 2068 | 168 | | if (connectionsElement.ValueKind == JsonValueKind.Object && connectionsElement.TryGetProperty("$values", out var |
| | | 169 | | { |
| | 0 | 170 | | connectionsElement = valuesEl; |
| | | 171 | | } |
| | | 172 | | // 3) Single‐object (old style): wrap into a 1‑element array if it has a "source" property |
| | 2068 | 173 | | else if (connectionsElement.ValueKind == JsonValueKind.Object && connectionsElement.TryGetProperty("source", out |
| | | 174 | | { |
| | 0 | 175 | | using var tmp = JsonDocument.Parse($"[{connectionsElement.GetRawText()}]"); |
| | 0 | 176 | | connectionsElement = tmp.RootElement; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | // 4) If it’s still not an array, bail |
| | 2068 | 180 | | if (connectionsElement.ValueKind != JsonValueKind.Array) |
| | 0 | 181 | | return new List<Connection>(); |
| | | 182 | | |
| | | 183 | | // Shortcut: detect the classic flat‐connection JSON and parse manually |
| | 2068 | 184 | | var arr = connectionsElement.EnumerateArray().ToArray(); |
| | 2068 | 185 | | if (arr.Length > 0 && arr[0].TryGetProperty("source", out var srcProp) && srcProp.ValueKind == JsonValueKind.Str |
| | | 186 | | { |
| | 0 | 187 | | var list = new List<Connection>(); |
| | | 188 | | |
| | 0 | 189 | | foreach (var el in arr) |
| | | 190 | | { |
| | 0 | 191 | | var srcId = el.GetProperty("source").GetString()!; |
| | 0 | 192 | | var tgtId = el.GetProperty("target").GetString()!; |
| | 0 | 193 | | var srcPort = el.TryGetProperty("sourcePort", out var sp) && sp.ValueKind == JsonValueKind.String ? sp.G |
| | 0 | 194 | | var tgtPort = el.TryGetProperty("targetPort", out var tp) && tp.ValueKind == JsonValueKind.String ? tp.G |
| | | 195 | | |
| | 0 | 196 | | var srcAct = activityDictionary[srcId]; |
| | 0 | 197 | | var tgtAct = activityDictionary[tgtId]; |
| | 0 | 198 | | list.Add(new Connection(new Endpoint(srcAct, srcPort), new Endpoint(tgtAct, tgtPort))); |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | return list; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | // Otherwise, it's an array of nested‐object connections → delegate to your converters |
| | 2068 | 205 | | var serializer = new JsonSerializerOptions(options); |
| | | 206 | | |
| | | 207 | | // Legacy check: look for "sourcePort" on the first item to choose the old converter |
| | 2068 | 208 | | if (arr.Length > 0 && arr[0].TryGetProperty("sourcePort", out _)) |
| | 0 | 209 | | serializer.Converters.Add(new ObsoleteConnectionJsonConverter(activityDictionary)); |
| | | 210 | | else |
| | 2068 | 211 | | serializer.Converters.Add(new ConnectionJsonConverter(activityDictionary)); |
| | | 212 | | |
| | 2068 | 213 | | var raw = connectionsElement.Deserialize<ICollection<Connection>>(serializer) ?? new List<Connection>(); |
| | | 214 | | |
| | | 215 | | // drop any half‑baked entries |
| | 2540 | 216 | | return raw.Where(c => c.Source?.Activity != null && c.Target?.Activity != null).ToList(); |
| | | 217 | | } |
| | | 218 | | } |