< Summary

Information
Class: Elsa.Workflows.Api.Serialization.ArgumentJsonConverter
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Serialization/ArgumentJsonConverter.cs
Line coverage
48%
Covered lines: 15
Uncovered lines: 16
Coverable lines: 31
Total lines: 68
Line coverage: 48.3%
Branch coverage
15%
Covered branches: 4
Total branches: 26
Branch coverage: 15.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%
Write(...)0%342180%
Read(...)50%88100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Serialization/ArgumentJsonConverter.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Text.Json;
 3using System.Text.Json.Nodes;
 4using System.Text.Json.Serialization;
 5using Elsa.Expressions.Contracts;
 6using Elsa.Expressions.Extensions;
 7using Elsa.Extensions;
 8using Elsa.Workflows.Models;
 9
 10namespace Elsa.Workflows.Api.Serialization;
 11
 12/// <summary>
 13/// Converts <see cref="ArgumentDefinition"/> derivatives from and to JSON
 14/// </summary>
 15public class ArgumentJsonConverter : JsonConverter<ArgumentDefinition>
 16{
 17    private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry;
 18
 19    /// <inheritdoc />
 29020    public ArgumentJsonConverter(IWellKnownTypeRegistry wellKnownTypeRegistry)
 21    {
 29022        _wellKnownTypeRegistry = wellKnownTypeRegistry;
 29023    }
 24
 25    /// <inheritdoc />
 26    public override void Write(Utf8JsonWriter writer, ArgumentDefinition value, JsonSerializerOptions options)
 27    {
 028        var newOptions = new JsonSerializerOptions(options);
 029        newOptions.Converters.RemoveWhere(x => x is ArgumentJsonConverterFactory);
 30
 031        var jsonObject = (JsonObject)JsonSerializer.SerializeToNode(value, value.GetType(), newOptions)!;
 032        var typeName = value.Type;
 033        var typeAlias = _wellKnownTypeRegistry.TryGetAlias(typeName, out var alias) ? alias : null;
 034        var isArray = typeName.IsArray;
 035        var isCollection = typeName.IsCollectionType();
 036        var elementTypeName = isArray ? typeName.GetElementType()! : isCollection ? typeName.GenericTypeArguments[0] : t
 037        var elementTypeAlias = _wellKnownTypeRegistry.GetAliasOrDefault(elementTypeName);
 038        var isAliasedArray = (isArray || isCollection) && typeAlias != null;
 039        var finalTypeAlias = isArray || isCollection ? typeAlias ?? elementTypeAlias : elementTypeAlias;
 40
 041        if (isArray && !isAliasedArray) jsonObject["isArray"] = isArray;
 042        if (isCollection) jsonObject["isCollection"] = isCollection;
 43
 044        jsonObject["type"] = finalTypeAlias;
 045        JsonSerializer.Serialize(writer, jsonObject, newOptions);
 046    }
 47
 48    /// <inheritdoc />
 49    [UnconditionalSuppressMessage("Trimming", "IL2055:Call to MakeGenericType can not be statically analyzed", Justifica
 50    public override ArgumentDefinition Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options
 51    {
 11452        var jsonObject = (JsonObject)JsonNode.Parse(ref reader)!;
 11453        var isArray = jsonObject["isArray"]?.GetValue<bool>() ?? false;
 11454        var isCollection = jsonObject["isCollection"]?.GetValue<bool>() ?? false;
 11455        var typeAlias = jsonObject["type"]!.GetValue<string>();
 11456        var type = _wellKnownTypeRegistry.GetTypeOrDefault(typeAlias);
 57
 11458        if (isArray) type = type.MakeArrayType();
 11459        if (isCollection) type = type.MakeGenericType(type.GenericTypeArguments[0]);
 60
 11461        var newOptions = new JsonSerializerOptions(options);
 171062        newOptions.Converters.RemoveWhere(x => x is ArgumentJsonConverterFactory);
 11463        var inputDefinition = (ArgumentDefinition)jsonObject.Deserialize(typeToConvert, newOptions)!;
 11464        inputDefinition.Type = type;
 65
 11466        return inputDefinition;
 67    }
 68}