< Summary

Information
Class: Elsa.Workflows.Management.Mappers.WorkflowDefinitionMapper
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Mappers/WorkflowDefinitionMapper.cs
Line coverage
61%
Covered lines: 80
Uncovered lines: 51
Coverable lines: 131
Total lines: 226
Line coverage: 61%
Branch coverage
70%
Covered branches: 24
Total branches: 34
Branch coverage: 70.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Map(...)100%22100%
MapFromModelWithIdentity(...)100%1010100%
Map(...)100%1212100%
MapToWorkflowDefinition(...)0%110100%
<MapAsync()100%210%
MapAsync()100%210%
MapAsync()100%11100%
Map(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Mappers/WorkflowDefinitionMapper.cs

#LineLine coverage
 1using Elsa.Workflows.Activities;
 2using Elsa.Workflows.Management.Entities;
 3using Elsa.Workflows.Management.Materializers;
 4using Elsa.Workflows.Management.Models;
 5using Elsa.Workflows.Models;
 6
 7namespace Elsa.Workflows.Management.Mappers;
 8
 9/// <summary>
 10/// Maps <see cref="WorkflowDefinition"/> to and from <see cref="Workflow"/> and <see cref="WorkflowDefinitionModel"/>.
 11/// </summary>
 12public class WorkflowDefinitionMapper
 13{
 14    private readonly IActivitySerializer _activitySerializer;
 15    private readonly IWorkflowDefinitionService _workflowDefinitionService;
 16    private readonly VariableDefinitionMapper _variableDefinitionMapper;
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="WorkflowDefinitionMapper"/> class.
 20    /// </summary>
 30321    public WorkflowDefinitionMapper(IActivitySerializer activitySerializer, IWorkflowDefinitionService workflowDefinitio
 22    {
 30323        _activitySerializer = activitySerializer;
 30324        _workflowDefinitionService = workflowDefinitionService;
 30325        _variableDefinitionMapper = variableDefinitionMapper;
 30326    }
 27
 28    /// <summary>
 29    /// Maps a <see cref="WorkflowDefinition"/> to a <see cref="Workflow"/>.
 30    /// </summary>
 31    /// <param name="source">The source <see cref="WorkflowDefinition"/>.</param>
 32    /// <returns>The mapped <see cref="Workflow"/>.</returns>
 33    public Workflow Map(WorkflowDefinition source)
 34    {
 35        // NEW WAY: If OriginalSource is present, deserialize the full WorkflowDefinitionModel
 36        // This enables symmetric round-trip without re-serialization
 120437        if (!string.IsNullOrEmpty(source.OriginalSource))
 38        {
 118239            var model = _activitySerializer.Deserialize<WorkflowDefinitionModel>(source.OriginalSource);
 118240            return MapFromModelWithIdentity(model, source);
 41        }
 42
 43        // OLD WAY: Deserialize from StringData (backwards compatibility)
 2244        var root = _activitySerializer.Deserialize(source.StringData!);
 45
 2246        return new(
 2247            new(source.DefinitionId, source.Version, source.Id, source.TenantId),
 2248            new(source.IsLatest, source.IsPublished),
 2249            new(source.Name, source.Description, source.CreatedAt, source.ToolVersion),
 2250            source.Options,
 2251            root,
 2252            source.Variables,
 2253            source.Inputs,
 2254            source.Outputs,
 2255            source.Outcomes,
 2256            source.CustomProperties,
 2257            source.IsReadonly,
 2258            source.IsSystem);
 59    }
 60
 61    /// <summary>
 62    /// Maps a <see cref="WorkflowDefinitionModel"/> to a <see cref="Workflow"/>, using identity from stored definition.
 63    /// </summary>
 64    private Workflow MapFromModelWithIdentity(WorkflowDefinitionModel model, WorkflowDefinition storedDefinition)
 65    {
 118266        var root = model.Root!;
 118267        var variables = _variableDefinitionMapper.Map(model.Variables).ToList();
 118268        var options = model.Options ?? new WorkflowOptions();
 69
 118270        return new(
 118271            new(storedDefinition.DefinitionId, storedDefinition.Version, storedDefinition.Id, storedDefinition.TenantId)
 118272            new(storedDefinition.IsLatest, storedDefinition.IsPublished),
 118273            new(model.Name, model.Description, model.CreatedAt, model.ToolVersion),
 118274            options,
 118275            root,
 118276            variables,
 118277            model.Inputs ?? new List<InputDefinition>(),
 118278            model.Outputs ?? new List<OutputDefinition>(),
 118279            model.Outcomes ?? new List<string>(),
 118280            model.CustomProperties ?? new Dictionary<string, object>(),
 118281            storedDefinition.IsReadonly,
 118282            storedDefinition.IsSystem);
 83    }
 84
 85    /// <summary>
 86    /// Maps a <see cref="WorkflowDefinitionModel"/> to a <see cref="Workflow"/>.
 87    /// </summary>
 88    /// <param name="source">The source <see cref="WorkflowDefinitionModel"/>.</param>
 89    /// <returns>The mapped <see cref="Workflow"/>.</returns>
 90    public Workflow Map(WorkflowDefinitionModel source)
 91    {
 86492        var root = source.Root!;
 86493        var variables = _variableDefinitionMapper.Map(source.Variables).ToList();
 86494        var options = source.Options ?? new WorkflowOptions();
 95
 96        // TODO: Remove this in the future when users have migrated workflows to use the new UsableAsActivity options pr
 97
 98#pragma warning disable CS0618
 86499        options.UsableAsActivity ??= source.UsableAsActivity ?? false;
 100#pragma warning restore CS0618
 101
 864102        return new(
 864103            new(source.DefinitionId, source.Version, source.Id, source.TenantId),
 864104            new(source.IsLatest, source.IsPublished),
 864105            new(source.Name, source.Description, source.CreatedAt, source.ToolVersion),
 864106            options,
 864107            root,
 864108            variables,
 864109            source.Inputs ?? new List<InputDefinition>(),
 864110            source.Outputs ?? new List<OutputDefinition>(),
 864111            source.Outcomes ?? new List<string>(),
 864112            source.CustomProperties ?? new Dictionary<string, object>(),
 864113            source.IsReadonly,
 864114            source.IsSystem);
 115    }
 116
 117    public WorkflowDefinition MapToWorkflowDefinition(WorkflowDefinitionModel source)
 118    {
 0119        var root = source.Root!;
 0120        var variables = _variableDefinitionMapper.Map(source.Variables).ToList();
 0121        var options = source.Options ?? new WorkflowOptions();
 0122        var stringData = _activitySerializer.Serialize(root);
 123
 0124        return new()
 0125        {
 0126            IsPublished = source.IsPublished,
 0127            Description = source.Description,
 0128            Id = source.Id,
 0129            Inputs = source.Inputs ?? [],
 0130            Name = source.Name,
 0131            Options = options,
 0132            Outcomes = source.Outcomes ?? [],
 0133            Outputs = source.Outputs ?? [],
 0134            Variables = variables,
 0135            Version = source.Version,
 0136            CreatedAt = source.CreatedAt,
 0137            CustomProperties = source.CustomProperties ?? new Dictionary<string, object>(),
 0138            DefinitionId = source.DefinitionId,
 0139            IsLatest = source.IsLatest,
 0140            IsReadonly = source.IsReadonly,
 0141            IsSystem = source.IsSystem,
 0142            TenantId = source.TenantId,
 0143            ToolVersion = source.ToolVersion,
 0144            StringData = stringData,
 0145            MaterializerName = JsonWorkflowMaterializer.MaterializerName
 0146        };
 147    }
 148
 149    /// <summary>
 150    /// Maps many <see cref="WorkflowDefinition"/>s to many <see cref="WorkflowDefinitionModel"/>s.
 151    /// </summary>
 152    /// <param name="source">The source <see cref="WorkflowDefinition"/>s.</param>
 153    /// <param name="cancellationToken">An optional cancellation token.</param>
 154    /// <returns>The mapped <see cref="WorkflowDefinitionModel"/>s.</returns>
 155    public async Task<IEnumerable<WorkflowDefinitionModel>> MapAsync(IEnumerable<WorkflowDefinition> source, Cancellatio
 156    {
 0157        return await Task.WhenAll(source.Select(async x => await MapAsync(x, cancellationToken)));
 0158    }
 159
 160    /// <summary>
 161    /// Maps a <see cref="WorkflowDefinition"/> to a <see cref="Workflow"/>.
 162    /// </summary>
 163    /// <param name="workflowDefinition">The source <see cref="WorkflowDefinition"/>.</param>
 164    /// <param name="cancellationToken">The cancellation token.</param>
 165    /// <returns>The mapped <see cref="Workflow"/>.</returns>
 166    public async Task<WorkflowDefinitionModel> MapAsync(WorkflowDefinition workflowDefinition, CancellationToken cancell
 167    {
 10168        var workflowGraph = await _workflowDefinitionService.MaterializeWorkflowAsync(workflowDefinition, cancellationTo
 10169        var workflow = workflowGraph.Workflow;
 10170        var variables = _variableDefinitionMapper.Map(workflow.Variables).ToList();
 171
 10172        return new(
 10173            workflowDefinition.Id,
 10174            workflowDefinition.DefinitionId,
 10175            workflowDefinition.TenantId,
 10176            workflowDefinition.Name,
 10177            workflowDefinition.Description,
 10178            workflowDefinition.CreatedAt,
 10179            workflowDefinition.Version,
 10180            workflowDefinition.ToolVersion,
 10181            variables,
 10182            workflowDefinition.Inputs,
 10183            workflowDefinition.Outputs,
 10184            workflowDefinition.Outcomes,
 10185            workflowDefinition.CustomProperties,
 10186            workflowDefinition.IsReadonly,
 10187            workflowDefinition.IsSystem,
 10188            workflowDefinition.IsLatest,
 10189            workflowDefinition.IsPublished,
 10190            workflow.Options,
 10191            null,
 10192            workflow.Root);
 10193    }
 194
 195    /// <summary>
 196    /// Maps a <see cref="Workflow"/> to a <see cref="WorkflowDefinitionModel"/>.
 197    /// </summary>
 198    /// <param name="workflow">The source <see cref="WorkflowDefinition"/>.</param>
 199    /// <returns>The mapped <see cref="WorkflowDefinitionModel"/>.</returns>
 200    public WorkflowDefinitionModel Map(Workflow workflow)
 201    {
 0202        var variables = _variableDefinitionMapper.Map(workflow.Variables).ToList();
 203
 0204        return new(
 0205            workflow.Identity.Id,
 0206            workflow.Identity.DefinitionId,
 0207            workflow.Identity.TenantId,
 0208            workflow.WorkflowMetadata.Name,
 0209            workflow.WorkflowMetadata.Description,
 0210            workflow.WorkflowMetadata.CreatedAt,
 0211            workflow.Identity.Version,
 0212            workflow.WorkflowMetadata.ToolVersion,
 0213            variables,
 0214            workflow.Inputs,
 0215            workflow.Outputs,
 0216            workflow.Outcomes,
 0217            workflow.CustomProperties,
 0218            workflow.IsReadonly,
 0219            workflow.IsSystem,
 0220            workflow.Publication.IsLatest,
 0221            workflow.Publication.IsPublished,
 0222            workflow.Options,
 0223            null,
 0224            workflow.Root);
 225    }
 226}