< 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>
 35921    public WorkflowDefinitionMapper(IActivitySerializer activitySerializer, IWorkflowDefinitionService workflowDefinitio
 22    {
 35923        _activitySerializer = activitySerializer;
 35924        _workflowDefinitionService = workflowDefinitionService;
 35925        _variableDefinitionMapper = variableDefinitionMapper;
 35926    }
 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
 99337        if (!string.IsNullOrEmpty(source.OriginalSource))
 38        {
 96939            var model = _activitySerializer.Deserialize<WorkflowDefinitionModel>(source.OriginalSource);
 96940            return MapFromModelWithIdentity(model, source);
 41        }
 42
 43        // OLD WAY: Deserialize from StringData (backwards compatibility)
 2444        var root = _activitySerializer.Deserialize(source.StringData!);
 45
 2446        return new(
 2447            new(source.DefinitionId, source.Version, source.Id, source.TenantId),
 2448            new(source.IsLatest, source.IsPublished),
 2449            new(source.Name, source.Description, source.CreatedAt, source.ToolVersion),
 2450            source.Options,
 2451            root,
 2452            source.Variables,
 2453            source.Inputs,
 2454            source.Outputs,
 2455            source.Outcomes,
 2456            source.CustomProperties,
 2457            source.IsReadonly,
 2458            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    {
 96966        var root = model.Root!;
 96967        var variables = _variableDefinitionMapper.Map(model.Variables).ToList();
 96968        var options = model.Options ?? new WorkflowOptions();
 69
 96970        return new(
 96971            new(storedDefinition.DefinitionId, storedDefinition.Version, storedDefinition.Id, storedDefinition.TenantId)
 96972            new(storedDefinition.IsLatest, storedDefinition.IsPublished),
 96973            new(model.Name, model.Description, model.CreatedAt, model.ToolVersion),
 96974            options,
 96975            root,
 96976            variables,
 96977            model.Inputs ?? new List<InputDefinition>(),
 96978            model.Outputs ?? new List<OutputDefinition>(),
 96979            model.Outcomes ?? new List<string>(),
 96980            model.CustomProperties ?? new Dictionary<string, object>(),
 96981            storedDefinition.IsReadonly,
 96982            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    {
 12168        var workflowGraph = await _workflowDefinitionService.MaterializeWorkflowAsync(workflowDefinition, cancellationTo
 12169        var workflow = workflowGraph.Workflow;
 12170        var variables = _variableDefinitionMapper.Map(workflow.Variables).ToList();
 171
 12172        return new(
 12173            workflowDefinition.Id,
 12174            workflowDefinition.DefinitionId,
 12175            workflowDefinition.TenantId,
 12176            workflowDefinition.Name,
 12177            workflowDefinition.Description,
 12178            workflowDefinition.CreatedAt,
 12179            workflowDefinition.Version,
 12180            workflowDefinition.ToolVersion,
 12181            variables,
 12182            workflowDefinition.Inputs,
 12183            workflowDefinition.Outputs,
 12184            workflowDefinition.Outcomes,
 12185            workflowDefinition.CustomProperties,
 12186            workflowDefinition.IsReadonly,
 12187            workflowDefinition.IsSystem,
 12188            workflowDefinition.IsLatest,
 12189            workflowDefinition.IsPublished,
 12190            workflow.Options,
 12191            null,
 12192            workflow.Root);
 12193    }
 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}