Skip to content

SortedResponse getSorted causes IndexOutOfBoundsException #3

@bristermitten

Description

@bristermitten

I'm not sure if this project is abandoned or not, so not necessarily expecting a response.

So, I've got this code:
client.request(QueryConstants.RESOURCE_SEARCH.get(searchQuery)) .fields("author,name,testedVersions") .sorted().size(18) .getSorted(Resource.class, (resources, throwable) -> { if (resources == null) { throwable.printStackTrace(); return; } for (Resource r : resources) { //arbitary code } });

But the for loop throws an IndexOutOfBounds exception
[17:32:20 WARN]: java.lang.IndexOutOfBoundsException: Index: 6, Size: 6 [17:32:20 WARN]: at java.util.ArrayList.rangeCheck(ArrayList.java:657) [17:32:20 WARN]: at java.util.ArrayList.get(ArrayList.java:433) [17:32:20 WARN]: at com.google.gson.JsonArray.get(JsonArray.java:101) [17:32:20 WARN]: at org.inventivetalent.spiget.client.DefaultSortedResponse$SortedResponseIterator.next(DefaultSortedResponse.java:55) [17:32:20 WARN]: at me.bristermitten.megapluginmanager.menu.SearchMenu.lambda$construct$1(SearchMenu.java:35) [17:32:20 WARN]: at org.inventivetalent.spiget.client.DefaultSortedRequest.lambda$getSorted$0(DefaultSortedRequest.java:47) [17:32:20 WARN]: at org.inventivetalent.spiget.client.SpigetClient.lambda$getDirect$0(SpigetClient.java:51) [17:32:20 WARN]: at org.inventivetalent.spiget.client.SpigetClient.execute(SpigetClient.java:45) [17:32:20 WARN]: at org.inventivetalent.spiget.client.SpigetClient.getDirect(SpigetClient.java:49) [17:32:20 WARN]: at org.inventivetalent.spiget.client.DefaultSortedRequest.getSorted(DefaultSortedRequest.java:26) //extra lines omitted

I've taken a look at the source code, and have found out that this is possibly an issue in the underlying API, as when testing it in the API tester, something like this https://api.spiget.org/v2/search/resources/uskyblock?field=name&fields=author%2Cname%2CtestedVersions returns X-Page-Size as 10, when the JsonArray size is only 6, presumably causing problems as the client tries to iterate up to 10.
I've tested changing callback.call(new DefaultSortedResponse<>(type, jsonElement.getAsJsonArray(), pageSize, pageIndex, pageCount), null); to callback.call(new DefaultSortedResponse<>(type, jsonElement.getAsJsonArray(), jsonElement.getAsJsonArray().size(), pageIndex, pageCount), null); in DefaultSortedRequest, and it all works fine, so perhaps I could submit a PR?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions