From 7a28ace3fb4822542c21c33565ae0cb27ba17110 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 28 Jun 2021 16:07:13 -0500 Subject: [PATCH] SONAR-14944 NPE when updating a Permission Template created via WS without a description --- .../ws/template/UpdateTemplateAction.java | 12 +++++++---- .../ws/template/UpdateTemplateActionTest.java | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java index f555d90684a..816324eca3d 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java @@ -39,7 +39,6 @@ import org.sonar.server.user.UserSession; import org.sonarqube.ws.Permissions.PermissionTemplate; import org.sonarqube.ws.Permissions.UpdateTemplateWsResponse; -import static com.google.common.base.MoreObjects.firstNonNull; import static java.lang.String.format; import static java.util.Objects.requireNonNull; import static org.apache.commons.lang.StringUtils.isBlank; @@ -131,14 +130,19 @@ public class UpdateTemplateAction implements PermissionsWsAction { private PermissionTemplateDto getAndBuildTemplateToUpdate(DbSession dbSession, String uuid, @Nullable String newName, @Nullable String newDescription, @Nullable String newProjectKeyPattern) { PermissionTemplateDto templateToUpdate = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(uuid, null)); - templateToUpdate.setName(firstNonNull(newName, templateToUpdate.getName())); - templateToUpdate.setDescription(firstNonNull(newDescription, templateToUpdate.getDescription())); - templateToUpdate.setKeyPattern(firstNonNull(newProjectKeyPattern, templateToUpdate.getKeyPattern())); + templateToUpdate.setName(coalesce(newName, templateToUpdate.getName())); + templateToUpdate.setDescription(coalesce(newDescription, templateToUpdate.getDescription())); + templateToUpdate.setKeyPattern(coalesce(newProjectKeyPattern, templateToUpdate.getKeyPattern())); templateToUpdate.setUpdatedAt(new Date(system.now())); return templateToUpdate; } + @CheckForNull + private static String coalesce(@Nullable String s1, @Nullable String s2) { + return s1 != null ? s1 : s2; + } + private PermissionTemplateDto updateTemplate(DbSession dbSession, PermissionTemplateDto templateToUpdate) { return dbClient.permissionTemplateDao().update(dbSession, templateToUpdate); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java index 4e18165ddd8..d373115c36d 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java @@ -95,6 +95,26 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest