Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Object invoke(String currentPath, PathRef parent, Object model, Evaluatio
return objectList.get(targetIndex);
} else {
int realIndex = objectList.size() + targetIndex;
if (realIndex > 0) {
if (realIndex >= 0) {
return objectList.get(realIndex);
} else {
throw new JsonPathException("Target index:" + targetIndex + " larger than object count:" + objectList.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.jayway.jsonpath.internal.function;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.JsonPathException;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class SequenceBoundaryTest {
@Test
void lastOfSingleton() {
Object result = JsonPath.read("[42]", "$.last()");
assertThat(result).isEqualTo(42);
}

@Test
void negativeIndexCanSelectFirstElement() {
Object result = JsonPath.read("[1,2,3]", "$.index(-3)");
assertThat(result).isEqualTo(1);
}

@Test
void negativeIndexBeforeStartStillFails() {
assertThatThrownBy(() -> JsonPath.read("[1,2,3]", "$.index(-4)"))
.isInstanceOf(JsonPathException.class);
}
}