Conversation
A deep scan iterating an array feeds every element to the token that
follows and records the element index in the leaf token. The leaf filter
introduced with 2.7.0 accepted a result only when the path fragment of
the token before the leaf literally equaled "[<index>]". That comparison
can never succeed for wildcard tokens ("[*]"), so paths like
$..*.title returned an empty list since 2.7.0, and multi index tokens
like $..[0,2] were dropped the same way.
Replace the string comparison with acceptsUpstreamArrayIndex, which only
explicit array index tokens restrict by their actual indexes. Wildcard,
slice, scan, property and predicate tokens accept every element, so
$..*.title returns all four book titles again while $..[2].author
still returns only the third author. A token without a previous link is
mounted by a function parameter evaluation (see Length) and has no path
constraint to apply, so it accepts every element instead of failing.
Fixes json-path#1052
Signed-off-by: 付典 <fudianchn@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.
What
$..*.titlereturns the four book titles again.$..[0,2].authorreturns authors 0 and 2.$..[2].authorstill returns only author 2. A wildcard fed by a deep scan used to drop every result, so$..*.*returned 13 instead of 31 elements.Why
Since 2.7.0 a deep scan over an array dropped every leaf result behind a wildcard or multi index token, returning an empty list.
How
New method
acceptsUpstreamArrayIndexreplaces the string comparison in the leaf filter. OnlyArrayIndexTokenrestricts, by its real indexes. Wildcard, slice, scan, property and predicate tokens accept every element. A token without a previous link is mounted by a function parameter evaluation (seeLength), so it accepts every element instead of dereferencing the missing link.Root cause
PR #715 added a filter that compares the path fragment of the token before the leaf with the fed array index.
"[*]"never equals"[0]", and"[0,2]"never equals either, so the results were dropped.Testing
DeepScanTest:$..*.titleand$..[0,2].authorfail on main and pass with the fix; the$..[2].authorguard (issue Unexpected result when executing a jsonpath on json array #273) passes on both.$..*.length()threw NullPointerException in the first version of this fix and now returns 31 like the completed scan results;$..[0].length()and$..[*].length()return 18 and 31.$..*.*returns 31,$..[1:3].authoraccepts all four authors,$..[-1].authorstays empty (pre-existing negative index gap)../gradlew buildgreen.Not covered:
[from,to)range; exact filtering needs the array length at the leaf, left out.Verification of the original issue
JsonPath.read(json, "$..*.title")on the books example:[]on 2.7.0 and main, the four titles with this fix.Fixes #1052