Skip to content

Consider sort defined by sortBy(…) in fluent Specification slice and unpaged queries. - #4298

Open
kmdy7991 wants to merge 1 commit into
spring-projects:mainfrom
kmdy7991:gh-4297-fluent-sort
Open

Consider sort defined by sortBy(…) in fluent Specification slice and unpaged queries.#4298
kmdy7991 wants to merge 1 commit into
spring-projects:mainfrom
kmdy7991:gh-4297-fluent-sort

Conversation

@kmdy7991

@kmdy7991 kmdy7991 commented Jul 6, 2026

Copy link
Copy Markdown

FetchableFluentQueryBySpecification uses pageable.getSort() in readSlice(…) and in the unpaged branches of slice(…), page(…), and page(…, countSpec), so a Pageable without a sort drops the sort configured through sortBy(…). The Querydsl counterpart, FetchableFluentQueryByPredicate, uses pageable.getSortOr(this.sort) in the analogous page/slice paths and keeps the fluent sort as the fallback.

This change applies the same getSortOr(…) fallback to the Specification variant and adds regression tests for the unsorted-PageRequest slice path and the unpaged terminals. The existing GH-3762 tests only use PageRequests that carry a sort (the override case), which is why the missing fallback went unnoticed.

Tests:

  • ./mvnw.cmd -pl spring-data-jpa -Dtest=UserRepositoryTests test

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

Closes #4297

…ries.

FetchableFluentQueryBySpecification now uses Pageable.getSortOr(…) to fall back to the sort defined via sortBy(…) when the given Pageable carries no sort, aligning slice(…) and the unpaged page(…) variants with FetchableFluentQueryByPredicate.

Closes spring-projects#4297

Signed-off-by: kmdy7991 <kmdy7991@naver.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 6, 2026
@mp911de

mp911de commented Jul 29, 2026

Copy link
Copy Markdown
Member

This is intentional behavior:

* {@literal null}. The given {@link Pageable} will override any previously specified {@link Sort sort} if
* the {@link Sort} object is not {@link Sort#isUnsorted()}. Any potentially specified {@link #limit(int)}
* will be overridden by {@link Pageable#getPageSize()}.
* @param countSpec specification used to count results.
* @return
*/
default Page<T> page(Pageable pageable, PredicateSpecification<?> countSpec) {

and this is part of the contract:

https://github.com/spring-projects/spring-data-commons/blob/c64558c964c86903a3b9143b8961323d932058fe/src/main/java/org/springframework/data/repository/query/FluentQuery.java#L231-L235

With that in mind, we need to revise FetchableFluentQueryByPredicate to align with the contract. Pagination may introduce two potentially conflicting sort definitions. Since Pageable already encapsulates a Sort, it should override any previously configured sort.

@kmdy7991

Copy link
Copy Markdown
Author

@mp911de

Thank you for the clarification — that makes sense, and I'm happy to rework this in that direction.

Two things I noticed while looking into what the alignment would touch, just to scope it correctly:

  1. The JpaSpecificationExecutor Javadoc for page(Pageable, PredicateSpecification) currently states the conditional rule ("…will override any previously specified sort if the Sort object is not Sort#isUnsorted()"), so it would need updating to the unconditional wording along with the code.
  2. readPage(…) in FetchableFluentQueryBySpecification also uses pageable.getSortOr(this.sort) today, and a few existing tests assert the fallback behavior (findByFluentSpecificationPageCustomCountSpec / Support custom countSpec in SimpleJpaRepository.findAll #3727 with an unsorted PageRequest, and findByFluentPredicateSlice / Add support for findBy(…) using Querydsl Predicate that returns a Slice #3764 on the Querydsl side) — those would need adjusting as well.

Would you like me to rework this PR to do that — align FetchableFluentQueryByPredicate (and readPage(…)) on pageable.getSort(), update the Javadoc, and adjust the affected tests — or would you prefer to handle it on your side? Either way works for me.

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

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FetchableFluentQueryBySpecification ignores sortBy for slice and unpaged page

3 participants