Conversation
When a JsonArray was passed as the target parameter, it was being cast to IList, resulting in null. This happened because JsonArray only properly implements IList<JsonNode>, not the non-generic IList interface. The fix adds a conditional check: if the object is IList<JsonNode> (like JsonArray), handle it properly instead of returning null. So now method returns actual array value rather than null. All types that implements IList are handled as before. Types that implement IList<JsonNode> now correctly return the array value
When a JsonArray was passed as the target parameter, it was being cast to IList, resulting in null. This happened because JsonArray only properly implements IList<JsonNode>, not the non-generic IList interface. The fix adds a conditional check: if the object is IList<JsonNode> (like JsonArray), handle it properly instead of returning null. So now method returns actual array value rather than null. All types that implements IList are handled as before. Types that implement IList<JsonNode> now correctly return the array value
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
Thanks for the fix @HPOD00019. I'd suggest a simpler approach though. Every other method in ListAdapter handles this dual IList/JsonArray support through GenericListOrJsonArrayUtilities. TryTraverse was the only method that bypassed it, which I think was an oversight. Rather than adding a second code path for IList<JsonNode>, we can just use the same utility here, which should fix this issue.
|
Please let us know if you'd like to update the PR yourself or if you'd prefer we can take care of it. |
|
Thanks for your answer! |
|
After refactoring, I noticed that test is now failing. But now it throws an exception because |
|
The test shouldn't need to change. The issue is that If you keep the original Would be good to have more tests here too btw... unit tests on ListAdapter.TryTraverse for both IList and JsonArray (including invalid index and out-of-bounds cases), and integration tests for replace, remove, add, and deeply-nested array traversal against JsonObject. But feel free to leave the added testing for now and we can have Copilot add those easily. |
|
Fixed! Returned to I'll go ahead and add the additional tests you suggested |
|
I added some unit tests to
I thought that duplicating all tests with |
src/Features/JsonPatch.SystemTextJson/src/Internal/ListAdapter.cs
Outdated
Show resolved
Hide resolved
| return false; | ||
| } | ||
|
|
||
| var count = GenericListOrJsonArrayUtilities.GetCount(target); |
There was a problem hiding this comment.
This could be moved down after this if check and maybe even inlined into the if where it is actually checked, so the code is only executed when necessary.
Co-authored-by: campersau <buchholz.bastian@googlemail.com>
Fixing JsonPatchDocument bug with Replace json array item
There was a bug when try using
during apply attempt the exception was thrown:
Description
Problem was in ListAdapter file, method TryTraverse consumed target parameter as "object" type and then tried to cast it to IList, but JsonArray implements only generic IList (not non-generic IList), so I added condition to proccess it any IList implementations correctly (instead of just returning null).
Also added one test to demonstrate problem (it runs with my insertations).
Fixes #65409