-
Notifications
You must be signed in to change notification settings - Fork 30
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
SONARJAVA-5288 Create rule S7186: Methods returning "Page" or "Slice" must take "Pageable" as an input parameter #4620
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3194e35
Create rule S7186
erwan-serandour 1265f85
first draft Methods returning Page or Slice must take Pageable as an …
erwan-serandour 9612ca4
fix typos
erwan-serandour a142f6a
fix typos
erwan-serandour 099678d
update after code review
erwan-serandour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 query methods 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 @@ | ||
{ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By query methods you mean methods inside a class extending spring repository interfaces? Maybe we should be clearer on this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍