-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-5288 Create rule S7186: Methods returning "Page" or "Slice"…
… must take "Pageable" as an input parameter (#4620)
- Loading branch information
1 parent
5e12d3b
commit d873f6b
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"title": "Methods returning \"Page\" or \"Slice\" must take \"Pageable\" as an input parameter", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"spring" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7186", | ||
"sqKey": "S7186", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW", | ||
"RELIABILITY": "HIGH", | ||
"SECURITY": "LOW" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
== Why is this an issue? | ||
|
||
Spring Data Repository supports paging for queries, allowing you to return results in small, manageable chunks rather than retrieving an entire large result set. | ||
|
||
The conventional approach to paginating data in Spring is to use the `Pageable` interface to control pagination and to store the query results into a `Page` or `Slice`. | ||
If a query method in a `Repository` returns a `Page` or `Slice` without taking a `Pageable` as an input, it raises a runtime exception. | ||
|
||
This rule raises an issue on queries in a `Repository` that return a `Page` or `Slice` without taking a `Pageable` as an input. | ||
|
||
== How to fix it | ||
|
||
Ensure that query methods returning a `Page` or `Slice` include a `Pageable` parameter in their method signature. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=1,diff-type=noncompliant] | ||
---- | ||
public Page<Item> findItems() { //non compliant, no Pageable parameter | ||
// query | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=1,diff-type=compliant] | ||
---- | ||
public Page<Item> findItems(Pageable pageable) { | ||
// query | ||
} | ||
---- | ||
|
||
== Resources | ||
=== Documentation | ||
* Spring - https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html[JPA Query Methods] | ||
* Spring - https://docs.spring.io/spring-data/jpa/reference/repositories/query-methods-details.html#repositories.paging-and-sorting[Defining Query Methods] | ||
|
||
=== Articles & blog posts | ||
* Spring Guides - https://reflectoring.io/spring-boot-paging/[Paging with Spring Boot] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |