elsa-workflows/elsa-core · error · InvalidOperationException

Invalid variable test values.

Error message

Invalid variable test values.

What it means

WorkflowDefinitionExtensions.GetVariableTestValues deserializes the definition's variable test values into IDictionary<string, object?>; if the payload is neither a dictionary nor a JSON object, it throws InvalidOperationException('Invalid variable test values.').

Solutions

  1. Fix the workflow definition so variable test values are a JSON object keyed by variable id
  2. Deserialize defensively and validate the shape before calling
  3. Catch InvalidOperationException and treat as an empty test-values set

Example fix

// before
var v = def.GetVariableTestValue(varId);
// after
IDictionary<string, object?> vals;
try { vals = def.GetVariableTestValues(); } catch (InvalidOperationException) { vals = new Dictionary<string, object?>(); }
var v = vals.TryGetValue(varId, out var x) ? x : null;
Defensive patterns

Strategy: validation

Validate before calling

var raw = def.VariableTestValues;
var ok = raw is IDictionary<string, object?> or (JsonElement e && e.ValueKind == JsonValueKind.Object);

Type guard

static bool IsValidTestValues(object? v) => v is IDictionary<string, object?> or JsonElement { ValueKind: JsonValueKind.Object };

Try / catch

try { values = def.GetVariableTestValues(); } catch (InvalidOperationException) { values = new Dictionary<string, object?>(); }

Prevention

When it happens

Trigger: Calling GetVariableTestValues/GetVariableTestValue on a WorkflowDefinition whose VariableTestValues property is a JSON array, string, or other non-object shape.

Common situations: Hand-edited or migrated workflow definitions with malformed variable test values; API responses where the property was not a JSON object.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/ff726f87d024f383. Report an issue: GitHub.

Appendix: source

Thrown at src/clients/Elsa.Api.Client/Resources/WorkflowDefinitions/Extensions/WorkflowDefinitionExtensions.cs:22

namespace Elsa.Api.Client.Resources.WorkflowDefinitions.Extensions;

public static class WorkflowDefinitionExtensions
{
    private const string VariableTestValuesKey = "VariableTestValues";

    public static IDictionary<string, object?> GetVariableTestValues(this WorkflowDefinition workflowDefinition)
    {
        if (!workflowDefinition.CustomProperties.TryGetValue(VariableTestValuesKey, out var dictionary))
        {
            var testValues = new Dictionary<string, object?>();
            workflowDefinition.CustomProperties[VariableTestValuesKey] = testValues;
            return testValues;
        }

        if (dictionary is JsonElement jsonElement)
            dictionary = JsonSerializer.Deserialize<Dictionary<string, object?>>(jsonElement.GetRawText());

        return dictionary as IDictionary<string, object?> ?? throw new InvalidOperationException("Invalid variable test values.");
    }

    public static object? GetVariableTestValue(this WorkflowDefinition workflowDefinition, string variableId)
    {
        var testValues = workflowDefinition.GetVariableTestValues();
        return testValues.TryGetValue(variableId, out var value) ? value : null;
    }

    public static void SetVariableTestValue(this WorkflowDefinition workflowDefinition, string variableId, object? value)
    {
        var testValues = workflowDefinition.GetVariableTestValues();
        testValues[variableId] = value;
        workflowDefinition.CustomProperties[VariableTestValuesKey] = testValues;
    }

    public static void ClearVariableTestValue(this WorkflowDefinition workflowDefinition, string variableId)
    {
        var testValues = workflowDefinition.GetVariableTestValues();

View on GitHub (pinned to fe9217bdfa)