Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PolymorphicObjectConverter throws InvalidOperationException for decimals #4714

Closed
chris-sargood opened this issue Dec 19, 2023 · 1 comment
Closed
Assignees
Milestone

Comments

@chris-sargood
Copy link

chris-sargood commented Dec 19, 2023

Hi,

We're getting the following exception thrown:

Exception Details

    ---> System.InvalidOperationException: The requested operation requires an element of type 'Object', but the target element has type 'Number'.
         at System.Text.Json.ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType expectedType, JsonTokenType actualType)
         at System.Text.Json.JsonElement.EnumerateObject()
         at Elsa.Workflows.Core.Serialization.Converters.PolymorphicObjectConverter.Write(Utf8JsonWriter writer, Object value, JsonSerializerOptions options)

Version

Elsa V3.0.0-preview.906

Steps to reproduce

Run a workflow that has an Expando object variable. Then assign a Decimal to the expando. Run the workflow.

This workflow demonstrates the issue:

public class IffyWorkflow : WorkflowBase
{
    protected override void Build(IWorkflowBuilder builder)
    {
        var document = builder.WithVariable<ExpandoObject>();
        builder.Root = new Sequence
        {
            Activities =
            {
                new Inline(context =>
                {
                    dynamic boom = new ExpandoObject();
                    boom.Ok = "456";
                    boom.AlsoOk = 456;
                    boom.NotOk = 456m;
                    context.Set(document,boom);
                }),
                new Finish()
            }
        };
    }
}

Result

Execution of the JsonWorkflowStateSerializer will eventually cause the above exception to be thrown and the WorkflowInstance table wont be updated.

Cause

Decimals aren't Primitives. When Elsa.Workflows.Core.Serialization.Converters.PolymorphicObjectConverter tests using if (type.IsPrimitive || value is string or DateTimeOffset or DateTime or DateOnly or TimeOnly or JsonElement or Guid or TimeSpan or Uri or Version or Enum) false is returned.
Execution will continue to foreach (var property in jsonElement.EnumerateObject().Where(property => !property.NameEquals(TypePropertyName))) which causes the exception as the value isn't an Object either.

@chris-sargood chris-sargood changed the title PolymorphicObjectConverter InvalidOperationException thrown PolymorphicObjectConverter throws InvalidOperationException for decimals Dec 19, 2023
@sfmskywalker sfmskywalker self-assigned this Jan 6, 2024
@sfmskywalker sfmskywalker added this to the Elsa 3.1 milestone Jan 6, 2024
@sfmskywalker
Copy link
Member

Thanks for the great bug report - I was able to reproduce this with the latest version 👍🏻

sfmskywalker added a commit that referenced this issue Jan 6, 2024
In the PolymorphicObjectConverter class, the check for primitive types and specific object types was updated to include decimal.

Fixes #4714
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants