SetPreferences: Trim input for my menus

Users often got trailing whitespace by mistake when the my menu input
was copied and pasted.

Bug: Issue 8050
Change-Id: I437657ca36d2b01beda4e1c7d80b2c8bbb4fb2ef
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin 2017-12-29 13:12:40 +01:00
parent 10d45357a0
commit 7b0caf1bf8
2 changed files with 16 additions and 5 deletions

View File

@ -191,4 +191,14 @@ public class GeneralPreferencesIT extends AbstractDaemonTest {
exception.expectMessage("URL for menu item is required");
gApi.accounts().id(user42.getId().toString()).setPreferences(i);
}
@Test
public void trimMyMenuInput() throws Exception {
GeneralPreferencesInfo i = GeneralPreferencesInfo.defaults();
i.my = new ArrayList<>();
i.my.add(new MenuItem(" name\t", " url\t", " _blank\t", " id\t"));
GeneralPreferencesInfo o = gApi.accounts().id(user42.getId().toString()).setPreferences(i);
assertThat(o.my).containsExactly(new MenuItem("name", "url", "_blank", "id"));
}
}

View File

@ -24,6 +24,7 @@ import static com.google.gerrit.server.git.UserConfigSections.KEY_URL;
import static com.google.gerrit.server.git.UserConfigSections.URL_ALIAS;
import com.google.common.base.Strings;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
import com.google.gerrit.extensions.client.MenuItem;
import com.google.gerrit.extensions.config.DownloadScheme;
@ -146,11 +147,11 @@ public class SetPreferences implements RestModifyView<AccountResource, GeneralPr
}
}
private static void set(Config cfg, String section, String key, String val) {
if (Strings.isNullOrEmpty(val)) {
cfg.unset(UserConfigSections.MY, section, key);
private static void set(Config cfg, String section, String key, @Nullable String val) {
if (val == null || val.trim().isEmpty()) {
cfg.unset(UserConfigSections.MY, section.trim(), key);
} else {
cfg.setString(UserConfigSections.MY, section, key, val);
cfg.setString(UserConfigSections.MY, section.trim(), key, val.trim());
}
}
@ -180,7 +181,7 @@ public class SetPreferences implements RestModifyView<AccountResource, GeneralPr
private static void checkRequiredMenuItemField(String value, String name)
throws BadRequestException {
if (Strings.isNullOrEmpty(value)) {
if (value == null || value.trim().isEmpty()) {
throw new BadRequestException(name + " for menu item is required");
}
}