Skip to content

Return an error for paths through scalars and missing list elements - #37

Merged
corka149 merged 2 commits into
corka149:masterfrom
bardrop-oss:fix-path-through-scalar
Nov 30, 2026
Merged

corka149 merged 2 commits into
corka149:masterfrom
bardrop-oss:fix-path-through-scalar

Conversation

@bardrop

@bardrop bardrop commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

apply_patch/3 raises when a path goes through a scalar or through a list element that is not there:

Jsonpatch.apply_patch(%{"op" => "add", "path" => "/a/b", "value" => 1}, %{"a" => 1})
# ** (FunctionClauseError) no function clause matching in Jsonpatch.Utils.find_destination/4
Jsonpatch.apply_patch(%{"op" => "add", "path" => "/2/a", "value" => 1}, [1, 2])
# ** (Enum.OutOfBoundsError)
Jsonpatch.apply_patch(%{"op" => "add", "path" => "/-/a", "value" => 1}, [1, 2])
# ** (FunctionClauseError)

The same happens for "test", "copy" and "move". "remove" and "replace" already return {:error, {:invalid_path, ...}} for a scalar, and ignore_invalid_paths: true does not help because of the raise.

Now find_destination/4 returns {:error, {:invalid_path, ...}} in these cases. The new test covers five paths with all four operations and fails without the change.

@corka149

Copy link
Copy Markdown
Owner

Hi @bardrop , thank you for your pr. I think, I will have spare time on the weekend to check.

@corka149 corka149 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lgtm - I will try to update the GH action asap

@corka149
corka149 merged commit 79d19de into corka149:master Nov 30, 2026
4 checks passed
@corka149

Copy link
Copy Markdown
Owner

Hi @bardrop , thanks again for your effort. I released the bug fix as version 2.3.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants