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
InvalidOperationException is thrown while patching elements in array #393
Comments
Thanks for reporting this. Yeah, it seems I have several places across the libs where I just assign a pre-existing node instead of copying it. This should be a simple fix. |
So the first case, where it does nothing, is correct. It should do nothing because the locations indicated by the patch don't exist. The patch is [
{"op":"replace","path":"/first_name","value":"*****"},
{"op":"replace","path":"/last_name","value":"*****"}
] The data is [
{"_id":"640729d45434f90313d25c78","guid":"f2e2767c-03e0-4862-addc-7d46c55efb33","first_name":"Kathrine","last_name":"Pate"},
{"_id":"640729d45b5824ffcabc30a5","guid":"73193eda-074b-4f31-9f09-507a008ccb75","first_name":"Rivers","last_name":"Smith"}
]
As far as I'm aware, there's not a way to apply a single patch to all elements in an array at once like this. Mainly this is due to JSON Patch using JsonPointer which can only indicate a single location within a JSON structure. You'd need to indicate multiple locations. |
Lastly, just a suggestion. Instead of var singleObject = JsonDocument.Parse(singleObjectJson).RootElement;
var patchedSingleObject = patchConfig.Apply(singleObject.AsNode()).Result; Just use var singleObject = JsonNode.Parse(singleObjectJson);
var patchedSingleObject = patchConfig.Apply(singleObject).Result; |
Environment
Describe the bug
InvalidOperationException
is thrown when I try to patch JSON object from a JSON array.Exception message:
The node already has a parent.
See call stack below.
To Reproduce
Code to reproduce the problem
Expected behavior
JSON object is patched successfully.
Additional context
Call stack
The text was updated successfully, but these errors were encountered: