]> source.dussan.org Git - sonarqube.git/commitdiff
Do not allow to declare WS parameters with both required flag and default value
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 6 Jan 2015 22:19:10 +0000 (23:19 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 6 Jan 2015 22:19:10 +0000 (23:19 +0100)
13 files changed:
server/sonar-server/src/main/java/org/sonar/server/issue/ws/SetTagsAction.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/TagsAction.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/HashAction.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/SourcesWs.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueTagsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/ws/ListingWsTest.java
server/sonar-server/src/test/resources/org/sonar/server/ws/ListingWsTest/list.json
server/sonar-server/src/test/resources/org/sonar/server/ws/ListingWsTest/list_including_internals.json
sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java
sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java

index 215e85b4dcb1c4b3d303f0f30bcae75953019c7e..1119fc96a6f593a7b0d09a022752206d58f8f8c2 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.issue.ws;
 
 import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableSet;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
@@ -56,16 +57,14 @@ public class SetTagsAction implements RequestHandler {
       .setExampleValue("5bccd6e8-f525-43a2-8d76-fcb13dde79ef")
       .setRequired(true);
     action.createParam("tags")
-      .setDescription("A comma separated list of tags")
-      .setExampleValue("security,cwe,misra-c")
-      .setRequired(true)
-      .setDefaultValue("");
+      .setDescription("Comma-separated list of tags. All tags are removed if parameter is empty or not set.")
+      .setExampleValue("security,cwe,misra-c");
   }
 
   @Override
   public void handle(Request request, Response response) throws Exception {
     String key = request.mandatoryParam("key");
-    String tags = request.mandatoryParam("tags");
+    String tags = Strings.nullToEmpty(request.param("tags"));
     Collection<String> resultTags = service.setTags(key, ImmutableSet.copyOf(WS_TAGS_SPLITTER.split(tags)));
     JsonWriter json = response.newJsonWriter().beginObject().name("tags").beginArray();
     for (String tag : resultTags) {
index 136972767d4c6f25b736cd9d601457ff97398132..a657e5cdd392adae8d9876cdad4f0c3c22a88028 100644 (file)
@@ -53,7 +53,6 @@ public class TagsAction implements RequestHandler {
     action.createParam("ps")
       .setDescription("The size of the list to return")
       .setExampleValue("25")
-      .setRequired(true)
       .setDefaultValue("10");
   }
 
index 7f50ef279aef6927a125484d4fa6e95f804af8a3..5b22eddc1b452c511d6bd029f714c441008de223 100644 (file)
@@ -61,8 +61,7 @@ public class HashAction implements RequestHandler {
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-    DbSession session = dbClient.openSession(false);
-    try {
+    try (DbSession session = dbClient.openSession(false)) {
       String componentKey = request.mandatoryParam("key");
       UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, componentKey);
       ComponentDto component = dbClient.componentDao().getByKey(session, componentKey);
@@ -70,12 +69,10 @@ public class HashAction implements RequestHandler {
       if (lineHashes == null) {
         response.noContent();
       } else {
-        OutputStream output = response.stream().setMediaType("text/plain").output();
-        output.write(lineHashes.getBytes(Charsets.UTF_8));
-        output.close();
+        try (OutputStream output = response.stream().setMediaType("text/plain").output()) {
+          output.write(lineHashes.getBytes(Charsets.UTF_8));
+        }
       }
-    } finally {
-      session.close();
     }
   }
 }
index 0057b5f1d93f4cc87b9fe3597a568fe98e5d336e..fd94bb125f66f7e5786d997bf05fb265ab087557 100644 (file)
@@ -61,13 +61,11 @@ public class IndexAction implements RequestHandler {
 
     action
       .createParam("from")
-      .setRequired(true)
       .setDefaultValue(1)
       .setDescription("First line");
 
     action
       .createParam("to")
-      .setRequired(false)
       .setDescription("Last line (excluded). If not specified, all lines are returned until end of file");
   }
 
@@ -77,19 +75,16 @@ public class IndexAction implements RequestHandler {
     UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
     Integer from = request.mandatoryParamAsInt("from");
     Integer to = request.paramAsInt("to");
-    DbSession session = dbClient.openSession(false);
-    try {
+    try (DbSession session = dbClient.openSession(false)) {
       ComponentDto componentDto = dbClient.componentDao().getByKey(session, fileKey);
       List<String> lines = sourceService.getLinesAsTxt(componentDto.uuid(), from, to == null ? null : to - 1);
       JsonWriter json = response.newJsonWriter().beginArray().beginObject();
       Integer lineCounter = from;
-      for (String line: lines) {
+      for (String line : lines) {
         json.prop(lineCounter.toString(), line);
-        lineCounter ++;
+        lineCounter++;
       }
       json.endObject().endArray().close();
-    } finally {
-      session.close();
     }
   }
 }
index 155eac9c4d8e13ae2875e54c60189a40395cb001..84fde01fedfea9966d6c5d9a6a5f5c880dddb8c4 100644 (file)
@@ -65,16 +65,13 @@ public class RawAction implements RequestHandler {
   public void handle(Request request, Response response) {
     String fileKey = request.mandatoryParam("key");
     UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
-    DbSession session = dbClient.openSession(false);
-    try {
+    try (DbSession session = dbClient.openSession(false)) {
       ComponentDto componentDto = dbClient.componentDao().getByKey(session, fileKey);
       List<String> lines = sourceService.getLinesAsTxt(componentDto.uuid(), null, null);
       response.stream().setMediaType("text/plain");
       IOUtils.writeLines(lines, "\n", response.stream().output(), Charsets.UTF_8);
     } catch (IOException e) {
       throw new IllegalStateException("Fail to write raw source of file " + fileKey, e);
-    } finally {
-      session.close();
     }
   }
 }
index 259aef1ec6694598d13ba0432d9b2efb4b5ffac2..f7dc54420fbcadcfc6fddff4cec49eea32ff1445 100644 (file)
@@ -25,15 +25,15 @@ import org.sonar.api.server.ws.WebService;
 public class SourcesWs implements WebService {
 
   private final ShowAction showAction;
-  private final LinesAction show2Action;
+  private final LinesAction linesAction;
   private final RawAction rawAction;
   private final ScmAction scmAction;
   private final HashAction hashAction;
   private final IndexAction indexAction;
 
-  public SourcesWs(ShowAction showAction, RawAction rawAction, ScmAction scmAction, LinesAction show2Action, HashAction hashAction, IndexAction indexAction) {
+  public SourcesWs(ShowAction showAction, RawAction rawAction, ScmAction scmAction, LinesAction linesAction, HashAction hashAction, IndexAction indexAction) {
     this.showAction = showAction;
-    this.show2Action = show2Action;
+    this.linesAction = linesAction;
     this.rawAction = rawAction;
     this.scmAction = scmAction;
     this.hashAction = hashAction;
@@ -46,7 +46,7 @@ public class SourcesWs implements WebService {
       .setSince("4.2")
       .setDescription("Display sources information");
     showAction.define(controller);
-    show2Action.define(controller);
+    linesAction.define(controller);
     rawAction.define(controller);
     scmAction.define(controller);
     hashAction.define(controller);
index 897e71e24de2804914671d2bb95ea28633cd04af..3e5cca639bba7c6f676a8a29175778984edc7c29 100644 (file)
@@ -68,7 +68,7 @@ public class IssueTagsActionTest {
     assertThat(query.description()).isNotEmpty();
     assertThat(query.exampleValue()).isNotEmpty();
     Param pageSize = action.param("ps");
-    assertThat(pageSize.isRequired()).isTrue();
+    assertThat(pageSize.isRequired()).isFalse();
     assertThat(pageSize.defaultValue()).isEqualTo("10");
     assertThat(pageSize.description()).isNotEmpty();
     assertThat(pageSize.exampleValue()).isNotEmpty();
index 37ceace6f3de8c29cae8bb56838894a383554c1c..f54e1f6adee4caf27fdfaff25ec37827a4b530d2 100644 (file)
@@ -68,8 +68,8 @@ public class SetTagsActionTest {
     assertThat(query.description()).isNotEmpty();
     assertThat(query.exampleValue()).isNotEmpty();
     Param pageSize = action.param("tags");
-    assertThat(pageSize.isRequired()).isTrue();
-    assertThat(pageSize.defaultValue()).isEqualTo("");
+    assertThat(pageSize.isRequired()).isFalse();
+    assertThat(pageSize.defaultValue()).isNull();
     assertThat(pageSize.description()).isNotEmpty();
     assertThat(pageSize.exampleValue()).isNotEmpty();
   }
index 9df9a5f17729f27ce0d844933ca4287dd8f095b9..7e1faf62fad9b1fe7a635a53e3d92439622f6cba 100644 (file)
@@ -108,7 +108,7 @@ public class ListingWsTest {
       create
         .createParam("severity")
         .setDescription("Severity")
-        .setRequired(true)
+        .setRequired(false)
         .setPossibleValues("BLOCKER", "INFO")
         .setExampleValue("INFO")
         .setDefaultValue("BLOCKER");
index 82e5c0f2d10ff3396ba9badde8a4a34d8093dcb7..141eae58b76bee59df1b6fc4be89508b70da7b2c 100644 (file)
@@ -19,7 +19,7 @@
           {
             "key": "severity",
             "description": "Severity",
-            "required": true,
+            "required": false,
             "defaultValue": "BLOCKER",
             "exampleValue": "INFO",
             "possibleValues": ["BLOCKER", "INFO"]
index bc74b0d6c087c6bc3b1ff4fab5c3b978857fea93..c57b4471021eed78fe32c7dc5346a85eb5c949b7 100644 (file)
@@ -19,7 +19,7 @@
           {
             "key": "severity",
             "description": "Severity",
-            "required": true,
+            "required": false,
             "defaultValue": "BLOCKER",
             "exampleValue": "INFO",
             "possibleValues": ["BLOCKER", "INFO"]
index 5b56ef5fef724c3a2d790a02e02f509bdabf77c3..bd5fa2368a5f676e93e831a0909b0a0e129d6925 100644 (file)
@@ -349,7 +349,7 @@ public interface WebService extends ServerExtension {
 
       ImmutableMap.Builder<String, Param> paramsBuilder = ImmutableMap.builder();
       for (NewParam newParam : newAction.newParams.values()) {
-        paramsBuilder.put(newParam.key, new Param(newParam));
+        paramsBuilder.put(newParam.key, new Param(this, newParam));
       }
       this.params = paramsBuilder.build();
     }
@@ -535,7 +535,7 @@ public interface WebService extends ServerExtension {
     private final boolean required;
     private final Set<String> possibleValues;
 
-    public Param(NewParam newParam) {
+    public Param(Action action, NewParam newParam) {
       this.key = newParam.key;
       this.deprecatedKey = newParam.deprecatedKey;
       this.description = newParam.description;
@@ -543,6 +543,9 @@ public interface WebService extends ServerExtension {
       this.defaultValue = newParam.defaultValue;
       this.required = newParam.required;
       this.possibleValues = newParam.possibleValues;
+      if (required && defaultValue != null) {
+        throw new IllegalArgumentException(String.format("Default value must not be set on parameter '%s?%s' as it's marked as required", action, key));
+      }
     }
 
     public String key() {
index aed3c7d78c637f12d19020ad53c597c546b03d2a..133be1d050096ac0b253b7a94916ee72c328e864 100644 (file)
@@ -328,6 +328,25 @@ public class WebServiceTest {
     assertThat(action.param("max").possibleValues()).isNull();
   }
 
+  @Test
+  public void fail_if_required_param_has_default_value() {
+    try {
+      new WebService() {
+        @Override
+        public void define(Context context) {
+          NewController controller = context.createController("api/rule");
+          NewAction action = controller.createAction("create").setHandler(mock(RequestHandler.class));
+          action.createParam("key").setRequired(true).setDefaultValue("abc");
+          controller.done();
+        }
+      }.define(context);
+      fail();
+    } catch (IllegalArgumentException e) {
+      assertThat(e).hasMessage("Default value must not be set on parameter 'api/rule/create?key' as it's marked as required");
+    }
+  }
+
+
   @Test
   public void fail_if_duplicated_action_parameters() {
     try {