Skip to content

Expression $[-1] throws ArrayIndexOutOfBoundsException on empty array input #106

Description

@Arowa-Z

Title

$[-1] throws ArrayIndexOutOfBoundsException when evaluating against an empty array []


Description

The expression $[-1] (to get the last element of an array) throws an ArrayIndexOutOfBoundsException when evaluated against an empty array [], instead of returning no match/null as expected and as seen in the official JSONata online evaluator.


Steps to Reproduce

  1. Define the expression $[-1].
  2. Evaluate the expression against an empty array [].

Sample Code:

import com.dashjoin.jsonata.Jsonata;
import java.util.List;

public class UnitTest {
    public static void main(String[] args) {
        var jsonata = Jsonata.jsonata("$[-1]");
        System.out.println(jsonata.evaluate(List.of()));
    }
}

Expected Behavior

The expression should return null or no match (consistent with the behavior in the official JSONata Exerciser) without throwing an exception.

Image

Actual Behavior

The evaluation throws an ArrayIndexOutOfBoundsException:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0
	at java.base/java.util.ImmutableCollections$ListN.get(ImmutableCollections.java:687)
	at com.dashjoin.jsonata.Jsonata.evaluateFilter(Jsonata.java:503)
	at com.dashjoin.jsonata.Jsonata._evaluate(Jsonata.java:212)
	at com.dashjoin.jsonata.Jsonata.evaluate(Jsonata.java:131)
	at com.dashjoin.jsonata.Jsonata.evaluate(Jsonata.java:2583)
	at com.dashjoin.jsonata.Jsonata.evaluate(Jsonata.java:2544)
	at com.example.demo.UnitTest.main(UnitTest.java:17)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions