From: Teryk Bellahsene Date: Wed, 31 Aug 2016 16:52:53 +0000 (+0200) Subject: SONAR-8004 WS settings/set better message when JSON is incorrect X-Git-Tag: 6.1-RC1~245 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d65ffe61517e311faf355a4d4c8134d3e73091e5;p=sonarqube.git SONAR-8004 WS settings/set better message when JSON is incorrect --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java index 51a46449337..f99b1e24aaa 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java @@ -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 readOneFieldValues(String json, String key) { Type type = new TypeToken>() { }.getType(); + Gson gson = GsonHelper.create(); try { - return (Map) GsonHelper.create().fromJson(json, type); - } catch (JsonParseException e) { - throw new BadRequestException(String.format("Invalid JSON '%s' for setting '%s'", json, key)); + return (Map) 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)); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java index ca8d5820e4e..3904252ece2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java @@ -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