Skip to content

Commit 53e6068

Browse files
fix: guard against a null comment when updating it - EXO-88900
SonarCloud reported a possible NullPointerException (javabugs:S2259) on the update endpoint: CommentStorageImpl.updateComment() returns the result of StorageUtil.commentToDto(), which is null when the DAO update returns null, and the endpoint dereferenced it right away. Since the refactoring, the endpoint no longer loads the comment beforehand, which made that path reachable. The endpoint now answers a server error instead of failing with a NPE, and the case is covered by a test. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 1f9b274 commit 53e6068

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

services/src/main/java/org/exoplatform/task/rest/TaskRestService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,10 @@ public Response updateComment(@Parameter(description = "Comment text", required
10981098
commentText = commentText.replace("+", "%2b");
10991099
commentText = URLDecoder.decode(commentText, "UTF-8");
11001100
CommentDto updatedComment = commentService.updateComment(commentId, commentText, currentUserIdentity);
1101+
if (updatedComment == null) {
1102+
LOG.error("Comment {} could not be updated", commentId);
1103+
return Response.serverError().build();
1104+
}
11011105
transformHtml(updatedComment, currentUserIdentity);
11021106
CommentEntity commentEntity = new CommentEntity(updatedComment,
11031107
userService.loadUser(updatedComment.getAuthor()),

services/src/test/java/org/exoplatform/task/rest/TestTaskRestService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,11 @@ public void testUpdateComment() throws Exception {
904904
any())).thenThrow(new RuntimeException("Unexpected error"));
905905
response = taskRestService.updateComment("brokenText", 1);
906906
assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
907+
908+
// The service returns no comment at all
909+
when(commentService.updateComment(eq(1L), eq("noResultText"), any())).thenReturn(null);
910+
response = taskRestService.updateComment("noResultText", 1);
911+
assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
907912
}
908913

909914
@Test

0 commit comments

Comments
 (0)