From: Guillaume Jambet Date: Fri, 5 Jan 2018 10:47:01 +0000 (+0100) Subject: SONAR-10245 Limit page size of api/components/search X-Git-Tag: 7.0-RC1~66 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=db035e56b216401f4fb27bc74bc8861cb4874802;p=sonarqube.git SONAR-10245 Limit page size of api/components/search --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java index c6f95f7d8ab..053e0bc610b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.sonar.api.i18n.I18n; import org.sonar.api.resources.Languages; import org.sonar.api.resources.ResourceTypes; @@ -43,18 +45,16 @@ import org.sonar.server.ws.WsUtils; import org.sonarqube.ws.Components; import org.sonarqube.ws.Components.SearchWsResponse; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.toMap; import static org.sonar.core.util.Protobuf.setNullable; import static org.sonar.core.util.stream.MoreCollectors.toHashSet; +import static org.sonar.server.es.SearchOptions.MAX_LIMIT; import static org.sonar.server.util.LanguageParamUtils.getExampleValue; import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys; -import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext; 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; import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_SEARCH; import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_LANGUAGE; @@ -84,7 +84,7 @@ public class SearchAction implements ComponentsWsAction { WebService.NewAction action = context.createAction(ACTION_SEARCH) .setSince("6.3") .setDescription("Search for components") - .addPagingParams(100) + .addPagingParams(100, MAX_LIMIT) .setResponseExample(getClass().getResource("search-components-example.json")) .setHandler(this); diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java index a23bc3338c2..3e539e8a20d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java @@ -42,6 +42,7 @@ import org.sonar.db.user.UserDto; import org.sonar.server.component.index.ComponentIndex; import org.sonar.server.component.index.ComponentIndexDefinition; import org.sonar.server.component.index.ComponentIndexer; +import org.sonar.server.component.ws.SearchAction.SearchRequest; import org.sonar.server.es.EsTester; import org.sonar.server.i18n.I18nRule; import org.sonar.server.organization.TestDefaultOrganizationProvider; @@ -50,10 +51,9 @@ import org.sonar.server.permission.index.PermissionIndexerTester; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; -import org.sonarqube.ws.MediaTypes; import org.sonarqube.ws.Components.Component; import org.sonarqube.ws.Components.SearchWsResponse; -import org.sonar.server.component.ws.SearchAction.SearchRequest; +import org.sonarqube.ws.MediaTypes; import static java.util.Arrays.asList; import static java.util.Collections.emptySet; @@ -123,6 +123,12 @@ public class SearchActionTest { assertThat(action.params()).hasSize(6); + WebService.Param pageSize = action.param("ps"); + assertThat(pageSize.isRequired()).isFalse(); + assertThat(pageSize.defaultValue()).isEqualTo("100"); + assertThat(pageSize.maximumValue()).isEqualTo(500); + assertThat(pageSize.description()).isEqualTo("Page size. Must be greater than 0 and less than 500"); + WebService.Param qualifiers = action.param("qualifiers"); assertThat(qualifiers.isRequired()).isTrue();