Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

totalResourcesCount is not properly computed for nonexistent page #878

Open
tory-kk opened this issue Apr 4, 2023 · 1 comment
Open

Comments

@tory-kk
Copy link

tory-kk commented Apr 4, 2023

Hello,
We use NumberSizePagingBehavior with JpaEntityRepositoryBase repository (version 3.4.20210509072026).
And recently, we have discovered the issue that totalResourcesCount is not properly computed when the requested page is greater than actually available.

I will try to describe the issue via an example:
Assume that we have 3 records in the database.

1st query /resource?page[number]=1&page[size]=2:
resourceList.getMeta(PagedMetaInformation.class).getTotalResourceCount() gives 3 (correct, value was fetched from the database).

2nd query /resource?page[number]=2&page[size]=2:
resourceList.getMeta(PagedMetaInformation.class).getTotalResourceCount() gives 3 (correct, value was not fetched from the database: link in source code )

3rd query for nonexistent page /resource?page[number]=5&page[size]=2:
resourceList.getMeta(PagedMetaInformation.class).getTotalResourceCount() gives 8 (wrong).
totalResourcesCount also was not fetched from the database and no errors occurred.

For the same situation, we are getting an error response when using QuerySpec#apply method instead of JpaEntityRepositoryBase queries:

{status=400, title=BAD_REQUEST, detail=page offset out of range, cannot move beyond data set}

In this situation, I would expect consistency with InMemoryEvaluator validation behaviour or at least properly fetched total count.

@dimas
Copy link

dimas commented Apr 4, 2023

Our theory is that you tried to optimise things a bit and not run count query where it can be avoided - that is when data is being fetched for the last page - you may get fewer items than the page size and that instantly gives you total count of items.
For example, if pageSize=100 and we are fetching page=5 and JPA returns us 33 items, we can easily derive total item count - we got 4 "full" pages before and 33 items on the last page meaning total = pageSize * (page-1) + itemsFetched = 100 * 4 + 33 = 433

This logic, however, breaks if someone requests page beyond the last one. IMHO, the optimisation should not apply if the JPA result is empty.

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

No branches or pull requests

2 participants