< Summary

Information
Class: Elsa.Workflows.Serialization.Helpers.ActivityWriter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Helpers/ActivityWriter.cs
Line coverage
83%
Covered lines: 31
Uncovered lines: 6
Coverable lines: 37
Total lines: 97
Line coverage: 83.7%
Branch coverage
76%
Covered branches: 29
Total branches: 38
Branch coverage: 76.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
WriteActivity(...)76.31%453883.33%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Helpers/ActivityWriter.cs

#LineLine coverage
 1using System.Reflection;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4using Microsoft.Extensions.Logging;
 5
 6namespace Elsa.Workflows.Serialization.Helpers;
 7
 8/// <summary>
 9/// The ActivityWriter class is responsible for writing an activity to a JSON writer using the provided options.
 10/// </summary>
 1411public class ActivityWriter(IActivityRegistry activityRegistry, SyntheticPropertiesWriter syntheticPropertiesWriter, ILo
 12{
 13    /// <summary>
 14    /// Writes an activity to a JSON writer using the provided options.
 15    /// </summary>
 16    /// <param name="writer">The JSON writer.</param>
 17    /// <param name="value">The activity to write.</param>
 18    /// <param name="options">The JSON serialization options.</param>
 19    /// <param name="ignoreSpecializedConverters">Controls whether to ignore the availability of converters that can han
 20    /// <param name="excludeChildren">A flag indicating whether to exclude child activities.</param>
 21    /// <param name="propertyFilter">An additional property filter. Returning true will skip the property.</param>
 22    public void WriteActivity(Utf8JsonWriter writer, IActivity? value, JsonSerializerOptions options, bool ignoreSpecial
 23    {
 233524        if (value == null)
 25        {
 026            writer.WriteNullValue();
 027            return;
 28        }
 29
 233530        if (!ignoreSpecializedConverters)
 31        {
 32            // Check if there's a specialized converter for the activity.
 115133            var valueType = value.GetType();
 2234434            var specializedConverter = options.Converters.FirstOrDefault(x => x.CanConvert(valueType));
 115135            if (specializedConverter != null)
 36            {
 115037                JsonSerializer.Serialize(writer, value, valueType, options);
 115038                return;
 39            }
 40        }
 41
 118542        writer.WriteStartObject();
 43
 118544        var properties = value.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
 45
 3798446        foreach (var property in properties)
 47        {
 1780748            if (property.GetCustomAttribute<JsonIgnoreAttribute>() != null)
 49                continue;
 50
 1176051            if (excludeChildren)
 52            {
 053                if (typeof(IActivity).IsAssignableFrom(property.PropertyType))
 54                    continue;
 55
 056                if (typeof(IEnumerable<IActivity>).IsAssignableFrom(property.PropertyType))
 57                    continue;
 58            }
 59
 1176060            if (propertyFilter?.Invoke(property) == true)
 61                continue;
 62
 1170063            var propName = options.PropertyNamingPolicy?.ConvertName(property.Name) ?? property.Name;
 1170064            writer.WritePropertyName(propName);
 1170065            var input = property.GetValue(value);
 66
 1170067            if (input == null)
 68            {
 212769                writer.WriteNullValue();
 212770                continue;
 71            }
 72
 957373            if (property.Name == nameof(IActivity.CustomProperties))
 74            {
 118575                var customProperties = new Dictionary<string, object>(value.CustomProperties);
 541076                foreach (var kvp in customProperties)
 77                {
 152078                    if (kvp.Value is IActivity or IEnumerable<IActivity>)
 079                        customProperties.Remove(kvp.Key);
 80                }
 81
 118582                input = customProperties;
 83            }
 84
 957385            JsonSerializer.Serialize(writer, input, options);
 86        }
 87
 118588        var activityDescriptor = activityRegistry.Find(value.Type, value.Version);
 89
 118590        if (activityDescriptor == null)
 091            logger.LogDebug("No descriptor found for activity {ActivityType}", value.GetType().Name);
 92        else
 118593            syntheticPropertiesWriter.WriteSyntheticProperties(writer, value, activityDescriptor, options);
 94
 118595        writer.WriteEndObject();
 118596    }
 97}