elsa-workflows/elsa-core

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

Fechada

#4.074 aberto em 29 de mai. de 2023

 (4 comentários) (0 reação) (0 responsável)C# (979 forks)batch import
elsa 2enhancementgood first issue

Métricas do repositório

Stars
 (5.486 estrelas)
Métricas de merge de PR
 (Mesclagem média 1d 17h) (29 fundiu PRs em 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()?

Guia do colaborador