Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NullPointerException when updating a project #2319

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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