elsa-workflows/elsa-core

Models with GUID properties + JSON.stringify() == Cyclic reference detected

Closed

#4,074 opened on May 29, 2023

 (4 comments) (0 reactions) (0 assignees)C# (979 forks)batch import
elsa 2enhancementgood first issue

Repository metrics

Stars
 (5,486 stars)
PR merge metrics
 (Avg merge 1d 17h) (29 merged PRs in 30d)

Description

Hi,

I'm starting a project with Elsa Workflows and I'm facing an issue with Work Flow Context. The models I'm using is using Guid for primary key data type. Using the sample ElsaGuides.WorkflowContexts.Web project, I tried changing,

    public class BlogPost
    {
        public string Id { get; set; } = default!;
        public string Title { get; set; } = default!;
        public string Body { get; set; } = default!;
        public bool IsPublished { get; set; }
    }

to

    public class BlogPost
    {
        public Guid Id { get; set; } = default!;
        public string Title { get; set; } = default!;
        public string Body { get; set; } = default!;
        public bool IsPublished { get; set; }
    }

and updated BlogPostWorkflowContextProvider to reflect the change above, I'm now getting the following error,

    {
        "errorMessage": "Workflow faulted at 2023-05-29T07:45:16Z with error: An exception was thrown whilst setting 'WriteHttpResponse.Content'. See the inner exception for further details.",
        "exception": {
            "Type": "Elsa.Exceptions.CannotSetActivityPropertyValueException",
            "Message": "An exception was thrown whilst setting 'WriteHttpResponse.Content'. See the inner exception for further details.",
            "StackTrace": "   at Elsa.Services.ActivityPropertyProviders.SetNestedActivityPropertiesAsync(Object nestedInstance, ActivityExecutionContext activityExecutionContext, IDictionary`2 providers, IActivityPropertyDefaultValueResolver defaultValueResolver, String nestedInstanceName, CancellationToken cancellationToken)\r\n   at Elsa.Services.ActivityPropertyProviders.SetActivityPropertiesAsync(IActivity activity, ActivityExecutionContext activityExecutionContext, CancellationToken cancellationToken)\r\n   at Elsa.Services.Workflows.ActivityActivator.ActivateActivityAsync(ActivityExecutionContext context, Type type, CancellationToken cancellationToken)\r\n   at Elsa.Providers.ActivityTypes.TypeBasedActivityProvider.<>c__DisplayClass6_0.<<CreateActivityTypeAsync>b__0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n   at Elsa.Services.Workflows.WorkflowRunner.RunCoreAsync(WorkflowExecutionContext workflowExecutionContext, ActivityOperation activityOperation, CancellationToken cancellationToken)\r\n   at Elsa.Services.Workflows.WorkflowRunner.RunCoreAsync(WorkflowExecutionContext workflowExecutionContext, ActivityOperation activityOperation, CancellationToken cancellationToken)\r\n   at Elsa.Services.Workflows.WorkflowRunner.BeginWorkflow(WorkflowExecutionContext workflowExecutionContext, IActivityBlueprint activity, CancellationToken cancellationToken)",
            "InnerException": {
                "Type": "Elsa.Exceptions.ExpressionEvaluationException",
                "Message": "Failed to evaluate expression",
                "StackTrace": "   at Elsa.Expressions.ExpressionEvaluator.EvaluateAsync(String expression, String syntax, Type returnType, ActivityExecutionContext context, CancellationToken cancellationToken)\r\n   at Elsa.Services.Workflows.ExpressionActivityPropertyValueProvider.GetValueAsync(ActivityExecutionContext context, CancellationToken cancellationToken)\r\n   at Elsa.Services.ActivityPropertyProviders.SetNestedActivityPropertiesAsync(Object nestedInstance, ActivityExecutionContext activityExecutionContext, IDictionary`2 providers, IActivityPropertyDefaultValueResolver defaultValueResolver, String nestedInstanceName, CancellationToken cancellationToken)",
                "InnerException": {
                    "Type": "Jint.Runtime.JavaScriptException",
                    "Message": "Cyclic reference detected.",
                    "StackTrace": "   at stringify <anonymous>:6:1\r\n   at <anonymous>:6:8",
                    "InnerException": null,
                    "Data": {}
                },
                "Data": {
                    "Expression": "const model = {\r\n    blogPost: workflowContext,\r\n    workflowInstanceId: workflowInstanceId\r\n}\r\n\r\nreturn JSON.stringify(model, null, 2);",
                    "Syntax": "JavaScript"
                }
            },
            "Data": {}
        },
        "workflow": {
            "name": null,
            "version": 5,
            "instanceId": "9f192ad6d0454a2298ad966115695044"
        }
    }

I found that the cause of the issue is the JSON.stringify() function call in the first WriteHttpResponse due to Id being Guid data type. I tried returning the model constant directly in the Content and it execute fine.

May I know how to resolve this with JSON.stringify()?

Contributor guide