elsa-workflows / elsa-workflows/elsa-core

[BUG] ElasticSearch - Executing a workflow with an error results in a Serialization and deserialization of 'System.Type' instances is not supported ($.WorkflowState.Incidents.Exception.Type)

Open
#5,851 0 comments 0 reactions 0 assignees View on GitHub
bug triaged
Dominant language
C#
Stars
7.9k
Forks
1.5k
Avg merge
15h 22m
Merged PRs (30d)
114

Description

## Description
I've created a workflow which looks as follows:

![image](https://github.com/user-attachments/assets/54d64f48-fbf3-4257-b3be-f34ea2780176)

When I execute it I end up with the following error:

Elastic.Transport.UnexpectedTransportException: Serialization and deserialization of 'System.Type' instances is not supported. Path: $.WorkflowState.Incidents.Exception.Type.
---> System.NotSupportedException: Serialization and deserialization of 'System.Type' instances is not supported. Path: $.WorkflowState.Incidents.Exception.Type.

This only occurs with Workflow instances which have errors.

## Steps to Reproduce
To help us identify the issue more quickly, please follow these guidelines:

1. **Detailed Steps**: Provide a step-by-step description of what leads to the bug. Be as specific as possible.
- Create a workflow with a fault Activity. Execute it and the error occurs.

2. **Code Snippets**: If the issue involves code (e.g., JavaScript error, server request failure), include the relevant snippets where the issue occurs.

- This error occurs in WorkflowInstanceStore.cs

```c
public async ValueTask SaveAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) =>
await _store.SaveAsync(instance, cancellationToken);
```

3. **Attachments**:
- **Workflow JSON**: If your issue is reproducible with a specific workflow, please attach the workflow's JSON file. This will help us understand the exact process that leads to the problem.
- **Sample Project**: If possible, attach a minimal sample project or code that reproduces the issue. This could be a simplified version of your project that isolates and demonstrates the bug.

4. **Reproduction Rate**: Indicate how often the bug occurs when following these steps (e.g., "every time", "intermittently: about 50% of the time").
- Every Time

5. **Video/Screenshots**: Sometimes, complex behaviors are better shown than described. If you think a video or screenshots would help clarify the issue, please include them.
- None
6. **Additional Configuration**:
```c
elsa.UseWorkflowManagement(management =>
{
// Use EF core for workflow definitions and instances.
management.UseWorkflowDefinitions(ef => ef.UseEntityFrameworkCore(ef => ef.UseSqlServer(connectionString)));
management.UseWorkflowInstances(i => i.UseElasticsearch());
});

elsa.UseElasticsearch(options =>
{
builder.Configuration.GetSection("Elsa:Elasticsearch").Bind(options);

var indexNameMappingSettings = builder.Configuration.GetSection("Elsa:Elasticsearch:IndexNameMappings").GetChildren();

foreach (var indexNameMappingSetting in indexNameMappingSettings)
{
var entityType = Type.GetType(indexNameMappingSetting.Key);
if (entityType == null)
continue;

options.IndexNameMappings[entityType] = indexNameMappingSetting.Value;
}
});

```

## Expected Behavior
Describe what you expected to happen.
- I should get an entry in the ElasticSearch WorkflowInstance index.

## Actual Behavior
Describe what actually happened. Include screenshots, if applicable.
- I get the error described above with no entry in the ElasticSearch Index.

## Screenshots
If possible, add screenshots or screen recordings to help explain the problem.
![image](https://github.com/user-attachments/assets/dd81581d-69b4-4aee-8d7d-075a999acca0)

## Environment
- I've cloned the 3.1.3 branch.
- Mac ( 14.0 (23A344 )

## Troubleshooting Attempts
I've created the Workflow with a fault to recreate this issue.

## Possible fix

I was considering creating a CustomConverter like the following:

```c
public class CustomJsonConverterForType : JsonConverter
{
public override Type Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
// Caution: Deserialization of type instances like this
// is not recommended and should be avoided
// since it can lead to potential security issues.

// If you really want this supported (for instance if the JSON input is trusted):
// string assemblyQualifiedName = reader.GetString();
// return Type.GetType(assemblyQualifiedName);
throw new NotSupportedException();
}

public override void Write(
Utf8JsonWriter writer,
Type value,
JsonSerializerOptions options
)
{
string assemblyQualifiedName = value.AssemblyQualifiedName;
// Use this with caution, since you are disclosing type information.
writer.WriteStringValue(assemblyQualifiedName);
}
}
```
I'm not sure if you have any other suggestions?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.