quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.35k stars 2.56k forks source link

The total count for page links do not considered the complete query #41500

Open j1myx opened 1 week ago

j1myx commented 1 week ago

Describe the bug

I'm using the quarkus-hibernate-orm-rest-data-panache extension to generate CRUD's, and in the endpoint that lists the records, the pagination links are not correct. This only becomes evident when I use a filter and/or custom query. Reviewing the source code of the rest-data-panache extension I discovered the reason:

ResultHandle pageCount = tryBlock.invokeVirtualMethod(
                    ofMethod(resourceMetadata.getResourceClass(), Constants.PAGE_COUNT_METHOD_PREFIX + RESOURCE_METHOD_NAME,
                            int.class, Page.class),
                    resource, page);

Class: io.quarkus.rest.data.panache.deployment.methods.ListMethodImplementor.java Line: 232 Version: 3.11.2

The reason is that this SQL query to obtain the total number of records must also consider the filters and/or custom queries that I apply as part of my query-params, and in this way both queries (for the total and list) will be affected by the filters and/or queries:

return creator.invokeVirtualMethod(
        ofMethod(resourceMetadata.getResourceClass(), "list", isNotReactivePanache() ? List.class : Uni.class,
                Page.class, Sort.class, String.class, Map.class),
        resource, page == null ? creator.loadNull() : page, sort, query, dataParams);

Class: io.quarkus.rest.data.panache.deployment.methods.ListMethodImplementor.java Line: 361 Version: 3.11.2

In this second source code fragment you can see that the query and dataParams parameters are being injected, both parameters should also be used to obtain the total.

Expected behavior

Records in my database:

id name createdBy
1 Jimy Nikola
2 Jaime Steve
3 Julio Nikola

Scenery 1:

Endpoint consumption without filtering parameters: https://api.jimyx,dev/users?size=2

Headers obtained: Link: https://api.jimyx,dev/users?page=0&size=2; rel="first" Link: https://api.jimyx,dev/users?page=1&size=2; rel="last" Link: https://api.jimyx,dev/users?page=1&size=2; rel="next"

Scenery 2:

Endpoint consumption with filtering parameters: https://api.jimyx,dev/users?size=2&createdBy=Nikola

Headers obtained: Link: https://api.jimyx,dev/users?page=0&size=2; rel="first" Link: https://api.jimyx,dev/users?page=0&size=2; rel="last"

Actual behavior

Records in my database:

id name createdBy
1 Jimy Nikola
2 Jaime Steve
3 Julio Nikola

Scenery 1:

Endpoint consumption without filtering parameters: https://api.jimyx,dev/users?size=2

Headers obtained: Link: https://api.jimyx,dev/users?page=0&size=2; rel="first" Link: https://api.jimyx,dev/users?page=1&size=2; rel="last" Link: https://api.jimyx,dev/users?page=1&size=2; rel="next"

Scenery 2:

Endpoint consumption with filtering parameters: https://api.jimyx,dev/users?size=2&createdBy=Nikola

Headers obtained: Link: https://api.jimyx,dev/users?page=0&size=2; rel="first" Link: https://api.jimyx,dev/users?page=1&size=2; rel="last" Link: https://api.jimyx,dev/users?page=1&size=2; rel="next"

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

No response

Quarkus version or git rev

No response

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

gsmet commented 6 days ago

@j1myx looks like you did all the work :). Would you be interested in providing a patch with a test?

j1myx commented 3 days ago

Hi @gsmet, ok. I will do it!