< Summary

Information
Class: Elsa.Workflows.Expressions.ObjectExpressionHandler
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Expressions/ObjectExpressionHandler.cs
Line coverage
93%
Covered lines: 15
Uncovered lines: 1
Coverable lines: 16
Total lines: 48
Line coverage: 93.7%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_SerializerOptions()100%22100%
EvaluateAsync(...)50%4483.33%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Expressions/ObjectExpressionHandler.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Text.Encodings.Web;
 3using System.Text.Json;
 4using System.Text.Json.Serialization;
 5using System.Text.Unicode;
 6using Elsa.Common.Converters;
 7using Elsa.Expressions.Contracts;
 8using Elsa.Expressions.Helpers;
 9using Elsa.Expressions.Models;
 10using Elsa.Extensions;
 11using JetBrains.Annotations;
 12
 13namespace Elsa.Workflows.Expressions;
 14
 15/// <summary>
 16/// Evaluates an object expression.
 17/// </summary>
 18[UsedImplicitly]
 19public class ObjectExpressionHandler : IExpressionHandler
 20{
 21    private JsonSerializerOptions? _serializerOptions;
 22
 23    private JsonSerializerOptions SerializerOptions =>
 22024        _serializerOptions ??= new JsonSerializerOptions
 22025        {
 22026            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 22027            ReferenceHandler = ReferenceHandler.Preserve,
 22028            PropertyNameCaseInsensitive = true,
 22029            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
 22030        }.WithConverters(
 22031            new IntegerJsonConverter(),
 22032            new DecimalJsonConverter(),
 22033            new JsonStringEnumConverter());
 34
 35    /// <inheritdoc />
 36    [RequiresUnreferencedCode("The type is not known at compile time.")]
 37    public ValueTask<object?> EvaluateAsync(Expression expression, Type returnType, ExpressionExecutionContext context, 
 38    {
 22039        var value = expression.Value.ConvertTo<string>() ?? "";
 40
 22041        if (string.IsNullOrWhiteSpace(value))
 042            return ValueTask.FromResult(default(object?));
 43
 22044        var converterOptions = new ObjectConverterOptions(SerializerOptions);
 22045        var model = value.ConvertTo(returnType, converterOptions);
 22046        return ValueTask.FromResult(model);
 47    }
 48}