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

"The node already has a parent." exception when setting JsonNode in Workflows ForEach Task #16846

Open
zl2fxy opened this issue Oct 8, 2024 · 5 comments · May be fixed by #16882
Open

"The node already has a parent." exception when setting JsonNode in Workflows ForEach Task #16846

zl2fxy opened this issue Oct 8, 2024 · 5 comments · May be fixed by #16882

Comments

@zl2fxy
Copy link

zl2fxy commented Oct 8, 2024

Title: System.InvalidOperationException: The node already has a parent. when setting JsonNode in OrchardCore Workflows

Description: I encountered a System.InvalidOperationException: The node already has a parent. when working with JsonNode in OrchardCore workflows. The error occurs when setting a JsonNode as the current value in a ForEachTask activity. It seems that the same JsonNode is being assigned to multiple parents, which causes the exception.

Error Message:

mathematica
复制代码
System.InvalidOperationException
  HResult=0x80131509
  Message=The node already has a parent.
  Source=System.Text.Json
  StackTrace:
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_NodeAlreadyHasParent()
   at System.Text.Json.Nodes.JsonObject.SetItem(String propertyName, JsonNode value)
   at OrchardCore.Workflows.Activities.Activity.SetProperty(Object value, String name) in E:\Test\test-orchard-core1\src\OrchardCore\OrchardCore.Workflows.Abstractions\Activities\Activity.cs:line 149
   at OrchardCore.Workflows.Activities.ForEachTask.set_Current(Object value) in E:\Test\test-orchard-core1\src\OrchardCore.Modules\OrchardCore.Workflows\Activities\ForEachTask.cs:line 47
   at OrchardCore.Workflows.Activities.ForEachTask.<ExecuteAsync>d__20.MoveNext() in E:\Test\test-orchard-core1\src\OrchardCore.Modules\OrchardCore.Workflows\Activities\ForEachTask.cs:line 74

Steps to Reproduce:

Create a workflow in OrchardCore that includes a ForEachTask activity.
Iterate over a collection of JsonNode objects.
Set a JsonNode object as the current value of the task in each iteration.
The exception is thrown because the JsonNode has already been assigned to a parent.
Expected Behavior: There should be no exception when setting a JsonNode in each iteration of the ForEachTask. Either JsonNode should be cloned or detached from its current parent before being reassigned.

Actual Behavior: An exception is thrown because JsonNode is being reused and assigned to multiple parents.

Possible Solutions:

Clone the JsonNode using DeepClone() before assigning it to avoid parent conflicts.
Remove the JsonNode from its current parent before reassigning it.

Environment:

OrchardCore version: 2.0
.NET version: .net8.0
Operating System: windows

@zl2fxy zl2fxy added the bug 🐛 label Oct 8, 2024
@Piedone Piedone changed the title JNode.fromObject work error "The node already has a parent." exception when setting JsonNode in Workflows ForEach Task Oct 8, 2024
@sebastienros
Copy link
Member

I assume that's a technical limitation to STJ and you just can't copy these nodes.
You either have to extract the information (strings, ...) yourself or clone the json node before you reuse it.

Copy link
Contributor

We triaged this issue and set the milestone according to the priority we think is appropriate (see the docs on how we triage and prioritize issues).

This indicates when the core team may start working on it. However, if you'd like to contribute, we'd warmly welcome you to do that anytime. See our guide on contributions here.

@zl2fxy
Copy link
Author

zl2fxy commented Oct 11, 2024

I assume that's a technical limitation to STJ and you just can't copy these nodes. You either have to extract the information (strings, ...) yourself or clone the json node before you reuse it.

Yes, I have already dealt with this issue, so do I need to submit a pull reset now?

@sebastienros
Copy link
Member

@zl2fxy if you think there is a product defect (a bug) somewhere in OC feel free to open one. It didn't seem like there is one to me, but maybe it will clear it out by seeing a PR.

Copy link
Contributor

It seems that this issue didn't really move for quite a while despite us asking the author for further feedback. Is this something you'd like to revisit any time soon or should we close? Please reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants