From: Teryk Bellahsene Date: Fri, 4 Mar 2016 08:58:14 +0000 (+0100) Subject: SONAR-7129 WS api/components/tree search query parameter must have at least 3 characters X-Git-Tag: 5.5-M7^2~2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a971c12d8336e158b69bcc22dfa351fec242cfe9;p=sonarqube.git SONAR-7129 WS api/components/tree search query parameter must have at least 3 characters --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java index 09a7efdb496..292b77400e7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java @@ -73,6 +73,7 @@ import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_STR public class TreeAction implements ComponentsWsAction { private static final int MAX_SIZE = 500; + private static final int QUERY_MINIMUM_LENGTH = 3; private static final String ALL_STRATEGY = "all"; private static final String CHILDREN_STRATEGY = "children"; private static final String LEAVES_STRATEGY = "leaves"; @@ -125,10 +126,11 @@ public class TreeAction implements ComponentsWsAction { .setExampleValue(NAME_SORT + ", " + PATH_SORT); action.createParam(Param.TEXT_QUERY) - .setDescription("Limit search to: " + + "Must have at least %d characters", QUERY_MINIMUM_LENGTH)) .setExampleValue("FILE_NAM"); createQualifiersParameter(action, newQualifierParameterContext(userSession, i18n, resourceTypes)); @@ -292,6 +294,9 @@ public class TreeAction implements ComponentsWsAction { .setPage(request.mandatoryParamAsInt(Param.PAGE)) .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)); checkRequest(treeWsRequest.getPageSize() <= MAX_SIZE, "The '%s' parameter must be less than %d", Param.PAGE_SIZE, MAX_SIZE); + String searchQuery = treeWsRequest.getQuery(); + checkRequest(searchQuery == null || searchQuery.length() >= QUERY_MINIMUM_LENGTH, + "The '%s' parameter must have at least %d characters", Param.TEXT_QUERY, QUERY_MINIMUM_LENGTH); return treeWsRequest; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java index b2616105467..fa5008f95d5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java @@ -307,6 +307,19 @@ public class TreeActionTest { .execute(); } + @Test + public void fail_when_search_query_has_less_than_3_characters() { + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("The 'q' parameter must have at least 3 characters"); + componentDb.insertComponent(newProjectDto("project-uuid")); + db.commit(); + + ws.newRequest() + .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid") + .setParam(Param.TEXT_QUERY, "fi") + .execute(); + } + @Test public void fail_when_sort_is_unknown() { expectedException.expect(IllegalArgumentException.class);