From cc10a68715444305f7a8d8e1181b39b5a79a2343 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Wed, 5 Aug 2020 10:19:21 -0500 Subject: [PATCH] SONAR-13151 api/issues/search page size parameter negative value should not be allowed --- .../org/sonar/server/es/SearchOptions.java | 16 ++--- .../sonar/server/es/SearchOptionsTest.java | 63 +++++++++---------- .../server/rule/index/RuleIndexTest.java | 6 -- .../sonar/server/issue/index/IssueQuery.java | 4 +- .../server/issue/index/IssueIndexTest.java | 6 +- .../server/component/ws/SearchAction.java | 4 +- .../server/issue/ws/BulkChangeAction.java | 4 +- .../sonar/server/issue/ws/SearchAction.java | 4 +- .../measure/custom/ws/SearchAction.java | 4 +- .../sonar/server/metric/ws/SearchAction.java | 4 +- .../organization/ws/SearchMembersAction.java | 4 +- .../qualityprofile/ws/ChangelogAction.java | 4 +- .../sonar/server/rule/ws/SearchAction.java | 6 +- .../sonar/server/user/ws/SearchAction.java | 3 +- .../server/usergroups/ws/SearchAction.java | 4 +- .../webhook/ws/WebhookDeliveriesAction.java | 4 +- .../server/issue/ws/SearchActionTest.java | 19 ++---- .../qualityprofile/QProfileRuleImplTest.java | 4 +- .../paging_with_page_size_to_minus_one.json | 5 -- 19 files changed, 69 insertions(+), 99 deletions(-) delete mode 100644 server/sonar-webserver-webapi/src/test/resources/org/sonar/server/issue/ws/SearchActionTest/paging_with_page_size_to_minus_one.json diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/SearchOptions.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/SearchOptions.java index e29e7fc2c51..26f5104cf19 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/SearchOptions.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/SearchOptions.java @@ -38,7 +38,7 @@ public class SearchOptions { public static final int DEFAULT_OFFSET = 0; public static final int DEFAULT_LIMIT = 10; - public static final int MAX_LIMIT = 500; + public static final int MAX_PAGE_SIZE = 500; private static final int MAX_RETURNABLE_RESULTS = 10_000; private int offset = DEFAULT_OFFSET; @@ -63,15 +63,14 @@ public class SearchOptions { } /** - * Set offset and limit according to page approach. If pageSize is negative, then - * {@link #MAX_LIMIT} is used. + * Set offset and limit according to page approach */ public SearchOptions setPage(int page, int pageSize) { checkArgument(page >= 1, "Page must be greater or equal to 1 (got " + page + ")"); + setLimit(pageSize); int lastResultIndex = page * pageSize; checkArgument(lastResultIndex <= MAX_RETURNABLE_RESULTS, "Can return only the first %s results. %sth result asked.", MAX_RETURNABLE_RESULTS, lastResultIndex); - setLimit(pageSize); - setOffset((page * this.limit) - this.limit); + setOffset(lastResultIndex - pageSize); return this; } @@ -90,11 +89,8 @@ public class SearchOptions { * Sets the limit on the number of results to return. */ public SearchOptions setLimit(int limit) { - if (limit <= 0) { - this.limit = MAX_LIMIT; - } else { - this.limit = Math.min(limit, MAX_LIMIT); - } + checkArgument(limit > 0 && limit <= MAX_PAGE_SIZE, "Page size must be between 1 and " + MAX_PAGE_SIZE + " (got " + limit + ")"); + this.limit = limit; return this; } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/SearchOptionsTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/SearchOptionsTest.java index ea5ae4854bd..f0f7f7a27f2 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/SearchOptionsTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/es/SearchOptionsTest.java @@ -27,7 +27,6 @@ import org.sonar.api.utils.text.JsonWriter; import org.sonar.test.JsonAssert; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; public class SearchOptionsTest { @@ -64,43 +63,49 @@ public class SearchOptionsTest { } @Test - public void with_zero_page_size() { - SearchOptions options = new SearchOptions().setPage(1, 0); - assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); - assertThat(options.getOffset()).isEqualTo(0); - assertThat(options.getPage()).isEqualTo(1); + public void fail_if_page_is_not_strictly_positive() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Page must be greater or equal to 1 (got 0)"); + new SearchOptions().setPage(0, 10); } @Test - public void page_must_be_strictly_positive() { - try { - new SearchOptions().setPage(0, 10); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Page must be greater or equal to 1 (got 0)"); - } + public void fail_if_ps_is_zero() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Page size must be between 1 and 500 (got 0)"); + new SearchOptions().setPage(1, 0); } @Test - public void use_max_limit_if_negative() { - SearchOptions options = new SearchOptions().setPage(2, -1); - assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); + public void fail_if_ps_is_negative() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Page size must be between 1 and 500 (got -1)"); + new SearchOptions().setPage(2, -1); } @Test - public void max_limit() { - SearchOptions options = new SearchOptions().setLimit(42); - assertThat(options.getLimit()).isEqualTo(42); + public void fail_if_ps_is_over_limit() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Page size must be between 1 and 500 (got 510)"); + new SearchOptions().setPage(3, SearchOptions.MAX_PAGE_SIZE + 10); + } + + @Test + public void fail_if_result_after_first_10_000() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Can return only the first 10000 results. 10500th result asked."); - options.setLimit(SearchOptions.MAX_LIMIT + 10); - assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); + underTest.setPage(21, 500); } @Test - public void max_page_size() { - SearchOptions options = new SearchOptions().setPage(3, SearchOptions.MAX_LIMIT + 10); - assertThat(options.getOffset()).isEqualTo(SearchOptions.MAX_LIMIT * 2); - assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); + public void max_limit() { + SearchOptions options = new SearchOptions().setLimit(42); + assertThat(options.getLimit()).isEqualTo(42); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Page size must be between 1 and 500 (got 510)"); + options.setLimit(SearchOptions.MAX_PAGE_SIZE + 10); } @Test @@ -113,12 +118,4 @@ public class SearchOptionsTest { JsonAssert.assertJson(json.toString()).isSimilarTo("{\"total\": 42, \"p\": 3, \"ps\": 10}"); } - - @Test - public void fail_if_result_after_first_10_000() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Can return only the first 10000 results. 10500th result asked."); - - underTest.setPage(21, 500); - } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java index b52303120b4..87f92a06403 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java @@ -1115,12 +1115,6 @@ public class RuleIndexTest { results = underTest.search(new RuleQuery(), options); assertThat(results.getTotal()).isEqualTo(3); assertThat(results.getUuids()).hasSize(1); - - // from 2 to 11 included - options.setOffset(2).setLimit(0); - results = underTest.search(new RuleQuery(), options); - assertThat(results.getTotal()).isEqualTo(3); - assertThat(results.getUuids()).hasSize(1); } @Test diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQuery.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQuery.java index 577dc6dcaec..950d132b2ec 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQuery.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQuery.java @@ -31,7 +31,7 @@ import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.sonar.db.rule.RuleDefinitionDto; import static com.google.common.base.Preconditions.checkArgument; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; /** * @since 3.6 @@ -500,7 +500,7 @@ public class IssueQuery { public IssueQuery build() { if (issueKeys != null) { - checkArgument(issueKeys.size() <= MAX_LIMIT, "Number of issue keys must be less than " + MAX_LIMIT + " (got " + issueKeys.size() + ")"); + checkArgument(issueKeys.size() <= MAX_PAGE_SIZE, "Number of issue keys must be less than " + MAX_PAGE_SIZE + " (got " + issueKeys.size() + ")"); } return new IssueQuery(this); } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java index 2f078e10610..b4ba5f4af5d 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java @@ -108,7 +108,7 @@ public class IssueIndexTest { assertThat(result.getHits().getHits()).hasSize(5); assertThat(result.getHits().getTotalHits()).isEqualTo(12); - result = underTest.search(IssueQuery.builder().build(), new SearchOptions().setOffset(2).setLimit(0)); + result = underTest.search(IssueQuery.builder().build(), new SearchOptions().setOffset(2).setLimit(10)); assertThat(result.getHits().getHits()).hasSize(10); assertThat(result.getHits().getTotalHits()).isEqualTo(12); } @@ -125,8 +125,8 @@ public class IssueIndexTest { indexIssues(issues.toArray(new IssueDoc[] {})); IssueQuery.Builder query = IssueQuery.builder(); - SearchResponse result = underTest.search(query.build(), new SearchOptions().setLimit(Integer.MAX_VALUE)); - assertThat(result.getHits().getHits()).hasSize(SearchOptions.MAX_LIMIT); + SearchResponse result = underTest.search(query.build(), new SearchOptions().setLimit(500)); + assertThat(result.getHits().getHits()).hasSize(SearchOptions.MAX_PAGE_SIZE); } @Test diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchAction.java index 3ab1df7d6aa..1910c399947 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchAction.java @@ -57,7 +57,7 @@ import static org.sonar.api.resources.Qualifiers.PROJECT; import static org.sonar.api.resources.Qualifiers.SUBVIEW; import static org.sonar.api.resources.Qualifiers.VIEW; import static org.sonar.core.util.stream.MoreCollectors.toHashSet; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter; import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -89,7 +89,7 @@ public class SearchAction implements ComponentsWsAction { WebService.NewAction action = context.createAction(ACTION_SEARCH) .setSince("6.3") .setDescription("Search for components") - .addPagingParams(100, MAX_LIMIT) + .addPagingParams(100, MAX_PAGE_SIZE) .setChangelog( new Change("8.4", "Param 'language' has been removed"), new Change("8.4", String.format("The use of 'DIR','FIL','UTS' as values for parameter '%s' is no longer supported", PARAM_QUALIFIERS)), diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java index 7742ad5d2ff..8cf16f92007 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java @@ -91,7 +91,7 @@ import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02; import static org.sonar.core.util.stream.MoreCollectors.toSet; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.issue.AbstractChangeTagsAction.TAGS_PARAMETER; import static org.sonar.server.issue.AssignAction.ASSIGNEE_PARAMETER; import static org.sonar.server.issue.CommentAction.COMMENT_KEY; @@ -356,7 +356,7 @@ public class BulkChangeAction implements IssuesWsAction { this.propertiesByActions = toPropertiesByActions(request); List issueKeys = request.mandatoryParamAsStrings(PARAM_ISSUES); - checkArgument(issueKeys.size() <= MAX_LIMIT, "Number of issues is limited to %s", MAX_LIMIT); + checkArgument(issueKeys.size() <= MAX_PAGE_SIZE, "Number of issues is limited to %s", MAX_PAGE_SIZE); List allIssues = dbClient.issueDao().selectByKeys(dbSession, issueKeys) .stream() .filter(issueDto -> SECURITY_HOTSPOT.getDbConstant() != issueDto.getType()) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchAction.java index dc143332796..032af08365a 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchAction.java @@ -76,7 +76,7 @@ import static org.sonar.api.issue.Issue.STATUS_TO_REVIEW; import static org.sonar.api.server.ws.WebService.Param.FACETS; import static org.sonar.api.utils.Paging.forPageIndex; import static org.sonar.core.util.stream.MoreCollectors.toSet; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.issue.index.IssueIndex.FACET_ASSIGNED_TO_ME; import static org.sonar.server.issue.index.IssueIndex.FACET_PROJECTS; import static org.sonar.server.issue.index.IssueQuery.SORT_BY_ASSIGNEE; @@ -211,7 +211,7 @@ public class SearchAction implements IssuesWsAction { new Change("5.5", "response field 'debt' is renamed 'effort'")) .setResponseExample(getClass().getResource("search-example.json")); - action.addPagingParams(100, MAX_LIMIT); + action.addPagingParams(100, MAX_PAGE_SIZE); action.createParam(FACETS) .setDescription("Comma-separated list of the facets to be computed. No facet is computed by default.") .setPossibleValues(SUPPORTED_FACETS); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/custom/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/custom/ws/SearchAction.java index 76c450dba46..b099ec51479 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/custom/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/custom/ws/SearchAction.java @@ -44,7 +44,7 @@ import org.sonar.server.es.SearchOptions; import org.sonar.server.user.UserSession; import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.measure.custom.ws.CustomMeasureValidator.checkPermissions; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; @@ -74,7 +74,7 @@ public class SearchAction implements CustomMeasuresWsAction { .setSince("5.2") .setDeprecatedSince("7.4") .addFieldsParam(CustomMeasureJsonWriter.OPTIONAL_FIELDS) - .addPagingParams(100, MAX_LIMIT) + .addPagingParams(100, MAX_PAGE_SIZE) .setResponseExample(getClass().getResource("example-search.json")) .setHandler(this); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/metric/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/metric/ws/SearchAction.java index 0a0e6eeb32a..ecd8616d2b3 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/metric/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/metric/ws/SearchAction.java @@ -34,7 +34,7 @@ import org.sonar.db.metric.MetricDto; import org.sonar.server.es.SearchOptions; import static com.google.common.collect.Sets.newHashSet; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.metric.ws.MetricJsonWriter.FIELD_ID; import static org.sonar.server.metric.ws.MetricJsonWriter.FIELD_KEY; @@ -61,7 +61,7 @@ public class SearchAction implements MetricsWsAction { .setSince("5.2") .setDescription("Search for metrics") .setResponseExample(getClass().getResource("example-search.json")) - .addPagingParams(100, MAX_LIMIT) + .addPagingParams(100, MAX_PAGE_SIZE) .setChangelog( new Change("8.4", "Field 'id' in the response is deprecated"), new Change("7.7", "Field 'custom' in the response is deprecated")) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/organization/ws/SearchMembersAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/organization/ws/SearchMembersAction.java index 754e72863d4..4dd265b8068 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/organization/ws/SearchMembersAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/organization/ws/SearchMembersAction.java @@ -52,7 +52,7 @@ import static com.google.common.base.Strings.emptyToNull; import static java.util.Optional.ofNullable; import static org.sonar.api.server.ws.WebService.SelectionMode.SELECTED; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_ORGANIZATION; import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -86,7 +86,7 @@ public class SearchMembersAction implements OrganizationsWsAction { action.createSearchQuery("orwe", "names", "logins") .setMinimumLength(2); - action.addPagingParams(50, MAX_LIMIT); + action.addPagingParams(50, MAX_PAGE_SIZE); action.createParam(Param.SELECTED) .setDescription("Depending on the value, show only selected items (selected=selected) or deselected items (selected=deselected).") diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java index 5d7e74f6330..c95c8ff77e9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java @@ -48,7 +48,7 @@ import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime; import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime; import static org.sonar.core.util.stream.MoreCollectors.toSet; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SINCE; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TO; @@ -78,7 +78,7 @@ public class ChangelogAction implements QProfileWsAction { QProfileReference.defineParams(wsAction, languages); - wsAction.addPagingParams(50, MAX_LIMIT); + wsAction.addPagingParams(50, MAX_PAGE_SIZE); wsAction.createParam(PARAM_SINCE) .setDescription("Start date for the changelog.
" + diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java index cbd2e4db4a0..cca26501e20 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java @@ -63,7 +63,7 @@ import static org.sonar.api.server.ws.WebService.Param.FACETS; import static org.sonar.api.server.ws.WebService.Param.FIELDS; import static org.sonar.api.server.ws.WebService.Param.PAGE; import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.rule.index.RuleIndex.ALL_STATUSES_EXCEPT_REMOVED; import static org.sonar.server.rule.index.RuleIndex.FACET_ACTIVE_SEVERITIES; import static org.sonar.server.rule.index.RuleIndex.FACET_CWE; @@ -129,7 +129,7 @@ public class SearchAction implements RulesWsAction { @Override public void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction(ACTION) - .addPagingParams(100, MAX_LIMIT) + .addPagingParams(100, MAX_PAGE_SIZE) .setHandler(this) .setChangelog(new Change("7.1", "The field 'scope' has been added to the response")) .setChangelog(new Change("7.1", "The field 'scope' has been added to the 'f' parameter")) @@ -227,7 +227,7 @@ public class SearchAction implements RulesWsAction { context.addFacets(request.getFacets()); } if (pageSize < 1) { - context.setPage(Integer.parseInt(request.getP()), 0).setLimit(MAX_LIMIT); + context.setPage(Integer.parseInt(request.getP()), 0).setLimit(MAX_PAGE_SIZE); } else { context.setPage(Integer.parseInt(request.getP()), pageSize); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java index 12d4991043c..c997de68110 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java @@ -54,7 +54,6 @@ import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; import static org.sonar.api.utils.DateUtils.formatDateTime; import static org.sonar.api.utils.Paging.forPageIndex; import static org.sonar.core.util.stream.MoreCollectors.toList; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.Users.SearchWsResponse.Groups; import static org.sonarqube.ws.Users.SearchWsResponse.ScmAccounts; @@ -101,7 +100,7 @@ public class SearchAction implements UsersWsAction { .setHandler(this) .setResponseExample(getClass().getResource("search-example.json")); - action.addPagingParams(50, MAX_LIMIT); + action.addPagingParams(50, SearchOptions.MAX_PAGE_SIZE); action.createParam(TEXT_QUERY) .setMinimumLength(2) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/SearchAction.java index 4a2b5cb15f8..2d9455fc5e0 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/SearchAction.java @@ -44,7 +44,7 @@ import static java.util.Optional.ofNullable; import static org.apache.commons.lang.StringUtils.defaultIfBlank; import static org.sonar.api.utils.Paging.forPageIndex; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_ORGANIZATION_KEY; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.UserGroups.Group; @@ -78,7 +78,7 @@ public class SearchAction implements UserGroupsWsAction { .setResponseExample(getClass().getResource("search-example.json")) .setSince("5.2") .addFieldsParam(ALL_FIELDS) - .addPagingParams(100, MAX_LIMIT) + .addPagingParams(100, MAX_PAGE_SIZE) .addSearchQuery("sonar-users", "names") .setChangelog( new Change("8.4", "Field 'id' in the response is deprecated. Format changes from integer to string."), diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java index af45ff0cd5e..5349a9d3b98 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java @@ -48,7 +48,7 @@ import static org.sonar.api.server.ws.WebService.Param.PAGE; import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; import static org.sonar.api.utils.Paging.offset; import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.webhook.ws.WebhookWsSupport.copyDtoToProtobuf; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -92,7 +92,7 @@ public class WebhookDeliveriesAction implements WebhooksWsAction { "auto-generated value that can be obtained through api/webhooks/create or api/webhooks/list") .setExampleValue(UUID_EXAMPLE_02); - action.addPagingParamsSince(10, MAX_LIMIT, "7.1"); + action.addPagingParamsSince(10, MAX_PAGE_SIZE, "7.1"); } @Override diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java index 3fb46b34b0a..fac01ee626a 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java @@ -450,7 +450,7 @@ public class SearchActionTest { ComponentDto project = db.components().insertComponent(ComponentTesting.newPublicProjectDto(organization, "PROJECT_ID").setDbKey("PROJECT_KEY")); indexPermissions(); ComponentDto file = db.components().insertComponent(newFileDto(project, null, "FILE_ID").setDbKey("FILE_KEY")); - for (int i = 0; i < SearchOptions.MAX_LIMIT + 1; i++) { + for (int i = 0; i < SearchOptions.MAX_PAGE_SIZE + 1; i++) { IssueDto issue = newDto(rule, file, project).setAssigneeUuid(null); dbClient.issueDao().insert(session, issue); } @@ -1038,23 +1038,12 @@ public class SearchActionTest { @Test public void paging_with_page_size_to_minus_one() { - RuleDto rule = newIssueRule(); - OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org-1")); - ComponentDto project = db.components().insertComponent(ComponentTesting.newPublicProjectDto(organization, "PROJECT_ID").setDbKey("PROJECT_KEY")); - indexPermissions(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null, "FILE_ID").setDbKey("FILE_KEY")); - for (int i = 0; i < 12; i++) { - IssueDto issue = newDto(rule, file, project); - dbClient.issueDao().insert(session, issue); - } - session.commit(); - indexIssues(); - + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Page size must be between 1 and 500 (got -1)"); ws.newRequest() .setParam(WebService.Param.PAGE, "1") .setParam(WebService.Param.PAGE_SIZE, "-1") - .execute() - .assertJson(this.getClass(), "paging_with_page_size_to_minus_one.json"); + .execute(); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileRuleImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileRuleImplTest.java index f6307c0622f..46b540966b0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileRuleImplTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileRuleImplTest.java @@ -707,7 +707,7 @@ public class QProfileRuleImplTest { @Test public void bulk_activation() { - int bulkSize = SearchOptions.MAX_LIMIT + 10 + new Random().nextInt(100); + int bulkSize = SearchOptions.MAX_PAGE_SIZE + 10 + new Random().nextInt(100); String language = randomAlphanumeric(10); String repositoryKey = randomAlphanumeric(10); QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(language)); @@ -734,7 +734,7 @@ public class QProfileRuleImplTest { @Test public void bulk_deactivation() { - int bulkSize = SearchOptions.MAX_LIMIT + 10 + new Random().nextInt(100); + int bulkSize = SearchOptions.MAX_PAGE_SIZE + 10 + new Random().nextInt(100); String language = randomAlphanumeric(10); String repositoryKey = randomAlphanumeric(10); QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(language)); diff --git a/server/sonar-webserver-webapi/src/test/resources/org/sonar/server/issue/ws/SearchActionTest/paging_with_page_size_to_minus_one.json b/server/sonar-webserver-webapi/src/test/resources/org/sonar/server/issue/ws/SearchActionTest/paging_with_page_size_to_minus_one.json deleted file mode 100644 index 04e89e27fc3..00000000000 --- a/server/sonar-webserver-webapi/src/test/resources/org/sonar/server/issue/ws/SearchActionTest/paging_with_page_size_to_minus_one.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "total": 12, - "p": 1, - "ps": 500 -} -- 2.39.5