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

Limit.of(0) gives exception #3102

Open
dearoneesama opened this issue Jun 3, 2024 · 1 comment
Open

Limit.of(0) gives exception #3102

dearoneesama opened this issue Jun 3, 2024 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@dearoneesama
Copy link

dearoneesama commented Jun 3, 2024

Adding the Limit parameter to the method declaration limits the number of results. However, this max currently cannot be 0 as when the query is executed an exception is thrown.

List<Foo> r = findByName(String name, Limit.of(0));
// org.springframework.dao.InvalidDataAccessApiUsageException: Page size must not be less than one

This behaviour is not documented anywhere near the Limit class. There's a vague explanation which does not explain things:

* {@link Limit} itself does not make assumptions about the actual {@link #max()} value sign. This means that a negative
* value may be valid within a defined context.
*
* @author Christoph Strobl
* @author Oliver Drotbohm
* @since 3.2
*/
public sealed interface Limit permits Limited, Unlimited {

Instead, Pageable and PageRequest mention the "size" must be non zero:

* @param pageSize the size of the page to be returned, must be greater than 0.
* @return a new {@link Pageable}.
* @since 2.5
*/
static Pageable ofSize(int pageSize) {

My question: should the documentation of Limit be updated to reflect this, or we should indeed allow the limit to be zero, which should return an empty result list. This'd be consistent with how usual SQL statements are expected to behave,

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 3, 2024
@mp911de
Copy link
Member

mp911de commented Jun 4, 2024

This is intended, however, it isn't explicitly documented as such. Negative limits are useful in stores such as Redis where -4 means give me the last four elements from a list.

@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 17, 2024
@mp911de mp911de self-assigned this Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants