Skip to content

Conversation

@m-nash
Copy link
Member

@m-nash m-nash commented Dec 3, 2025

If a non Append patch is set for a given path, any child path will mistakenly assume it needs to normalize the indexers across the root and patch array items.

This can occur for property sets as well as item removals and item property removals.

This PR fixes this and adds corresponding test cases for regression.

Copilot AI review requested due to automatic review settings December 3, 2025 21:47
@m-nash m-nash requested a review from a team as a code owner December 3, 2025 21:47
Copilot finished reviewing on behalf of m-nash December 3, 2025 21:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in the JsonPatch.TryGetEncodedValueInternal method where non-Append patch operations (property sets, item removals, and item property removals) incorrectly assumed they needed to normalize array indexers. The fix ensures that array index normalization only occurs when an Append operation exists on the ancestor array path.

Key Changes:

  • Added a check to return early when accessing removed array items (line 75-78)
  • Removed premature optimization for empty array paths that was preventing correct behavior
  • Added a condition to break from array index adjustment loop when the ancestor array is not an Append operation (lines 110-114)

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
sdk/core/System.ClientModel/src/ModelReaderWriter/JsonPatch.Implementation.cs Fixed the TryGetEncodedValueInternal method to properly handle non-Append patch operations by breaking out of index normalization when ancestor arrays aren't Append operations
sdk/core/System.ClientModel/tests/ModelReaderWriterTests/JsonPatchTests.cs Added comprehensive regression tests covering array iteration with property modifications, item removals, item property removals, and append operations

Copy link
Member

@jsquire jsquire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This covers the scenario that we talked about and a couple of additional corners.

@m-nash
Copy link
Member Author

m-nash commented Dec 3, 2025

/check-enforcer evaluate

@m-nash m-nash merged commit c05382e into main Dec 5, 2025
36 checks passed
@m-nash m-nash deleted the jsonPatchArrayIteration branch December 5, 2025 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants