]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8004 WS settings/set better message when JSON is incorrect 1198/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 31 Aug 2016 16:52:53 +0000 (18:52 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 1 Sep 2016 14:01:34 +0000 (16:01 +0200)
server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java
server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java

index 51a4644933776bb91ba3082753529dd9644ca6c4..f99b1e24aaa3c5851a3d3f6958111d594d3fb07b 100644 (file)
@@ -22,7 +22,8 @@ package org.sonar.server.setting.ws;
 
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
-import com.google.gson.JsonParseException;
+import com.google.gson.Gson;
+import com.google.gson.JsonSyntaxException;
 import com.google.gson.reflect.TypeToken;
 import java.lang.reflect.Type;
 import java.util.Collections;
@@ -319,10 +320,11 @@ public class SetAction implements SettingsWsAction {
   private static Map<String, String> readOneFieldValues(String json, String key) {
     Type type = new TypeToken<Map<String, String>>() {
     }.getType();
+    Gson gson = GsonHelper.create();
     try {
-      return (Map<String, String>) GsonHelper.create().fromJson(json, type);
-    } catch (JsonParseException e) {
-      throw new BadRequestException(String.format("Invalid JSON '%s' for setting '%s'", json, key));
+      return (Map<String, String>) gson.fromJson(json, type);
+    } catch (JsonSyntaxException e) {
+      throw new BadRequestException(String.format("JSON '%s' does not respect expected format for setting '%s'. Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}", json, key));
     }
   }
 
index ca8d5820e4ef66eea36acf042434f459209f3609..3904252ece24f210fa9f722a2d7c48eaf8aa291c 100644 (file)
@@ -585,11 +585,36 @@ public class SetActionTest {
       .build());
 
     expectedException.expect(BadRequestException.class);
-    expectedException.expectMessage("Invalid JSON 'incorrectJson:incorrectJson' for setting 'my.key'");
+    expectedException.expectMessage("JSON 'incorrectJson:incorrectJson' does not respect expected format for setting 'my.key'. " +
+      "Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}");
 
     callForGlobalPropertySet("my.key", newArrayList("incorrectJson:incorrectJson"));
   }
 
+  @Test
+  public void fail_when_property_set_with_json_of_the_wrong_format() {
+    propertyDefinitions.addComponent(PropertyDefinition
+      .builder("my.key")
+      .name("foo")
+      .description("desc")
+      .category("cat")
+      .subCategory("subCat")
+      .type(PropertyType.PROPERTY_SET)
+      .defaultValue("default")
+      .fields(newArrayList(
+        PropertyFieldDefinition.build("field")
+          .name("Field")
+          .type(PropertyType.STRING)
+          .build()))
+      .build());
+
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("JSON '[{\"field\":\"v1\"}, {\"field\":\"v2\"}]' does not respect expected format for setting 'my.key'. " +
+      "Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}");
+
+    callForGlobalPropertySet("my.key", newArrayList("[{\"field\":\"v1\"}, {\"field\":\"v2\"}]"));
+  }
+
   @Test
   public void fail_when_property_set_on_component_of_global_setting() {
     propertyDefinitions.addComponent(PropertyDefinition