Skip to content

Commit

Permalink
Fix NullPointerException when updating a project (DependencyTrack#2319
Browse files Browse the repository at this point in the history
)

* Fix `NullPointerException` in ProjectQueryManager

Fixes the `NullPointerException` which occurs when updating a project
where `project.isActive() == null`

Signed-off-by: RBickert <[email protected]>

* Prevent NullPointerException in NotificationRouter

Signed-off-by: RBickert <[email protected]>

Signed-off-by: RBickert <[email protected]>
Signed-off-by: mulder999 <[email protected]>
  • Loading branch information
rbt-mm authored and mulder999 committed Dec 23, 2022
1 parent 71c9d29 commit b898e96
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private boolean checkIfChildrenAreAffected(Project parent, UUID uuid) {
return false;
}
for (Project child : parent.getChildren()) {
if ((child.getUuid().equals(uuid) && child.isActive()) || isChild) {
if ((child.getUuid().equals(uuid) && Boolean.TRUE.equals(child.isActive())) || isChild) {
return true;
}
isChild = checkIfChildrenAreAffected(child, uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,8 @@ public Project updateProject(UUID uuid, String name, String description, String
project.setVersion(version);
project.setPurl(purl);

if (!active && project.isActive() && hasActiveChild(project)){
if (!active && Boolean.TRUE.equals(project.isActive()) && hasActiveChild(project)){
throw new IllegalArgumentException("Project cannot be set to inactive, if active children are present.");
} else {
project.setActive(active);
}
project.setActive(active);

Expand Down Expand Up @@ -522,10 +520,8 @@ public Project updateProject(Project transientProject, boolean commitIndex) {
project.setPurl(transientProject.getPurl());
project.setSwidTagId(transientProject.getSwidTagId());

if (project.isActive() && !Boolean.TRUE.equals(transientProject.isActive()) && hasActiveChild(project)){
if (Boolean.TRUE.equals(project.isActive()) && !Boolean.TRUE.equals(transientProject.isActive()) && hasActiveChild(project)){
throw new IllegalArgumentException("Project cannot be set to inactive if active children are present.");
} else {
project.setActive(transientProject.isActive());
}
project.setActive(transientProject.isActive());

Expand Down Expand Up @@ -1091,7 +1087,7 @@ private static boolean hasActiveChild(Project project) {
boolean hasActiveChild = false;
if (project.getChildren() != null){
for (Project child: project.getChildren()) {
if (child.isActive() || hasActiveChild) {
if (Boolean.TRUE.equals(child.isActive()) || hasActiveChild) {
return true;
} else {
hasActiveChild = hasActiveChild(child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,24 @@ public void updateProjectTest() {
Assert.assertEquals("Test project", json.getString("description"));
}

@Test
public void updateProjectTestIsActiveEqualsNull() {
Project project = qm.createProject("ABC", null, "1.0", null, null, null, true, false);
project.setDescription("Test project");
project.setActive(null);
Assert.assertNull(project.isActive());
Response response = target(V1_PROJECT)
.request()
.header(X_API_KEY, apiKey)
.post(Entity.entity(project, MediaType.APPLICATION_JSON));
Assert.assertEquals(200, response.getStatus(), 0);
JsonObject json = parseJsonObject(response);
Assert.assertNotNull(json);
Assert.assertEquals("ABC", json.getString("name"));
Assert.assertEquals("1.0", json.getString("version"));
Assert.assertEquals("Test project", json.getString("description"));
}

@Test
public void updateProjectTagsTest() {
final var tags = Stream.of("tag1", "tag2").map(qm::createTag).collect(Collectors.toUnmodifiableList());
Expand Down

0 comments on commit b898e96

Please sign in to comment.