From: Duarte Meneses Date: Mon, 28 Jun 2021 21:07:13 +0000 (-0500) Subject: SONAR-14944 NPE when updating a Permission Template created via WS without a description X-Git-Tag: 9.0.0.45539~29 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7a28ace3fb4822542c21c33565ae0cb27ba17110;p=sonarqube.git SONAR-14944 NPE when updating a Permission Template created via WS without a description --- 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