Skip to content

Fix negative index selecting the first element in sequence functions - #1092

Open
lee3Q wants to merge 2 commits into
json-path:masterfrom
lee3Q:lee3Q-patch-1
Open

lee3Q wants to merge 2 commits into
json-path:masterfrom
lee3Q:lee3Q-patch-1

Conversation

@lee3Q

@lee3Q lee3Q commented Nov 4, 2026

Copy link
Copy Markdown

AbstractSequenceAggregation converts a negative target index with realIndex = size + targetIndex and then checks realIndex > 0. realIndex == 0 is the first element, so it is rejected by mistake:

  • JsonPath.read("[42]", "$.last()") throws instead of returning 42;
  • JsonPath.read("[1,2,3]", "$.index(-3)") throws instead of returning 1.

This changes the check to realIndex >= 0. An index before the start (for example $.index(-4) on a 3-element array) still throws the same exception with the same message.

Tests (SequenceBoundaryTest, 3 tests): last() on a one-element array, index(-3) selecting the first element, and index(-4) still failing. The first two fail without the fix.

Verification: with JDK 17 (Corretto 17.0.21) and Gradle 8.5, the json-path module tests passed (933 tests, 0 failures, 0 errors, 2 skipped) with the fix, and the same run reverted to the old check shows the failing cases. ./gradlew build also finished successfully. A full clean build of every module on other JDKs/OSes and CI were not run.

Related open PRs #1079, #1081 and #1084 change negative index handling for set/delete/map, bounded slices and deep scan; this PR touches only the sequence function code path.

This branch has not been deployed

No deployments
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.

1 participant