spring-projects / spring-data-commons

Spring Data Commons. Interfaces and code shared between the various datastore specific implementations.
https://spring.io/projects/spring-data
Apache License 2.0
756 stars 662 forks source link

`Limit.of(0)` gives exception #3102

Open dearoneesama opened 1 month ago

dearoneesama commented 1 month ago

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:

https://github.com/spring-projects/spring-data-commons/blob/0a603f2e57dc253a268d665401238b7ef2fb2a40/src/main/java/org/springframework/data/domain/Limit.java#L30-L37

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

https://github.com/spring-projects/spring-data-commons/blob/0a603f2e57dc253a268d665401238b7ef2fb2a40/src/main/java/org/springframework/data/domain/Pageable.java#L54-L58

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,

mp911de commented 1 month ago

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.