elsa-workflows / elsa-workflows/elsa-core
VariableMapper fails to serialize and deserialize complex variable types due to ObjectFormatter.Format limitations
- Dominant language
- C#
- Stars
- 7.9k
- Forks
- 1.5k
- Avg merge
- 15h 22m
- Merged PRs (30d)
- 114
Description
## Description
`ObjectFormatter.Format` does not reliably serialize custom or complex types. For types without a type converter to string or a custom `ToString()` implementation, it returns the type name (e.g., `"Namespace.MyClass"`), not a serialized value. This is problematic because `VariableMapper` uses `Format` to map variable values to strings. As a result, when mapping variables of complex types (e.g., `Variable`), the value is not preserved and cannot be deserialized, leading to data loss or runtime errors in workflows.
## Steps to Reproduce
1. **Detailed Steps**:
1. Define a custom class, e.g.:
```csharp
public class MyClass { public int Value { get; set; } }
```
2. Create a variable of this type:
```csharp
var variable = new Variable("id", "name", new MyClass { Value = 42 });
```
3. Use `VariableMapper.Map(variable)` to map it to a `VariableModel`.
4. Inspect the `Value` property of the resulting `VariableModel`.
5. Attempt to map back from `VariableModel` to `Variable`.
2. **Code Snippets**:
```csharp
public class MyClass { public int Value { get; set; } }
var variable = new Variable("id", "name", new MyClass { Value = 42 });
var mapper = new VariableMapper();
var model = mapper.Map(variable);
// model.Value will be "MyClass" (or the result of obj.ToString()), not a serialized value
var variable2 = mapper.Map(model);
// variable2.Value will be null or invalid
```
3. **Attachments**:
- N/A (can provide a minimal sample project if needed).
4. **Reproduction Rate**: Every time with custom/complex types lacking a string converter or custom `ToString()`.
5. **Video/Screenshots**: N/A
6. **Additional Configuration**:
- No special configuration required.
## Expected Behavior
`Format` should return a string that accurately represents the object's data, allowing for correct deserialization. `VariableMapper` should be able to round-trip variables of any type, including complex types.
## Actual Behavior
`Format` returns the type name or the default `ToString()` output, which cannot be deserialized back to the original object. This results in null or invalid values for complex type variables in workflows.
## Screenshots
N/A
## Environment
- **Elsa Package Version**: 3.4.0
- **Operating System**: Windows 11
- **Browser and Version**: N/A
## Log Output
N/A
## Troubleshooting Attempts
- Verified with primitive types (works as expected).
- Verified with custom types (returns type name).
- Checked for available type converters.
## Additional Context
This issue causes data loss or runtime errors when workflows rely on variable serialization/deserialization for custom types.
## Related Issues
N/A
---
Contributor guide
Research direction
Start at VariableMapper.Map and ObjectFormatter.Format, then reproduce the reported round trip with the MyClass example and inspect how the VariableModel value is produced and read back. Done means the custom object's Value survives mapping in both directions without invalid or null data.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100