< Summary

Information
Class: Elsa.Workflows.Serialization.Helpers.SyntheticPropertiesWriter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Helpers/SyntheticPropertiesWriter.cs
Line coverage
90%
Covered lines: 49
Uncovered lines: 5
Coverable lines: 54
Total lines: 104
Line coverage: 90.7%
Branch coverage
72%
Covered branches: 13
Total branches: 18
Branch coverage: 72.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
WriteSyntheticProperties(...)100%11100%
WriteSyntheticInputProperties(...)66.66%121289.28%
WriteSyntheticOutputProperties(...)83.33%6690.9%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using Elsa.Expressions.Contracts;
 3using Elsa.Workflows.Models;
 4using Humanizer;
 5
 6namespace Elsa.Workflows.Serialization.Helpers;
 7
 8/// <summary>
 9/// Helper class for writing synthetic properties of an activity to a JSON writer.
 10/// </summary>
 1411public class SyntheticPropertiesWriter(IExpressionDescriptorRegistry expressionDescriptorRegistry)
 12{
 13    /// <summary>
 14    /// Writes synthetic properties of an activity to a JSON writer.
 15    /// </summary>
 16    public void WriteSyntheticProperties(Utf8JsonWriter writer, IActivity value, ActivityDescriptor activityDescriptor, 
 17    {
 118518        WriteSyntheticInputProperties(writer, value, activityDescriptor, options);
 118519        WriteSyntheticOutputProperties(writer, value, activityDescriptor, options);
 118520    }
 21
 22    private void WriteSyntheticInputProperties(Utf8JsonWriter writer, IActivity value, ActivityDescriptor activityDescri
 23    {
 360424        var syntheticInputs = activityDescriptor.Inputs.Where(x => x.IsSynthetic).ToList();
 25
 238426        foreach (var inputDescriptor in syntheticInputs)
 27        {
 728            var inputName = inputDescriptor.Name;
 729            var propertyName = inputName.Camelize();
 30
 731            if (!value.SyntheticProperties.TryGetValue(inputName, out var inputValue))
 32                continue;
 33
 734            writer.WritePropertyName(propertyName);
 35
 736            var input = (Input?)inputValue;
 37
 738            if (input == null)
 39            {
 040                writer.WriteNullValue();
 041                continue;
 42            }
 43
 744            var expression = input.Expression;
 745            var expressionType = expression?.Type;
 746            var inputType = input.Type;
 747            var memoryReferenceId = input.MemoryBlockReference().Id;
 748            var expressionDescriptor = expressionType != null ? expressionDescriptorRegistry.Find(expressionType) : null
 49
 750            if (expressionDescriptor == null)
 051                throw new Exception($"Syntax descriptor with expression type {expressionType} not found in registry");
 52
 753            var inputModel = new
 754            {
 755                TypeName = inputType,
 756                Expression = expression,
 757                MemoryReference = new
 758                {
 759                    Id = memoryReferenceId
 760                }
 761            };
 62
 763            JsonSerializer.Serialize(writer, inputModel, inputModel.GetType(), options);
 64        }
 118565    }
 66
 67    private void WriteSyntheticOutputProperties(Utf8JsonWriter writer, IActivity value, ActivityDescriptor activityDescr
 68    {
 176569        var syntheticOutputs = activityDescriptor.Outputs.Where(x => x.IsSynthetic).ToList();
 70
 71        // Write synthetic outputs.
 241272        foreach (var outputDescriptor in syntheticOutputs)
 73        {
 2174            var outputName = outputDescriptor.Name;
 2175            var propertyName = outputName.Camelize();
 76
 2177            if (!value.SyntheticProperties.TryGetValue(outputName, out var outputValue))
 78                continue;
 79
 780            writer.WritePropertyName(propertyName);
 781            var output = (Output?)outputValue;
 82
 783            if (output == null)
 84            {
 085                writer.WriteNullValue();
 086                continue;
 87            }
 88
 789            var outputType = outputDescriptor.Type;
 790            var memoryReferenceId = output.MemoryBlockReference().Id;
 91
 792            var outputModel = new
 793            {
 794                TypeName = outputType,
 795                MemoryReference = new
 796                {
 797                    Id = memoryReferenceId
 798                }
 799            };
 100
 7101            JsonSerializer.Serialize(writer, outputModel, outputModel.GetType(), options);
 102        }
 1185103    }
 104}