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

Backport: Fix possible NPEs during tag binding #4595

Merged
merged 1 commit into from
Jan 28, 2025
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 @@ -249,11 +249,17 @@ public boolean bind(final NotificationRule notificationRule, final Collection<Ta
return callInTransaction(() -> {
boolean modified = false;

if (notificationRule.getTags() == null) {
notificationRule.setTags(new ArrayList<>());
}

if (!keepExisting) {
for (final Tag existingTag : notificationRule.getTags()) {
if (!tags.contains(existingTag)) {
notificationRule.getTags().remove(existingTag);
existingTag.getNotificationRules().remove(notificationRule);
if (existingTag.getNotificationRules() != null) {
existingTag.getNotificationRules().remove(notificationRule);
}
modified = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,11 +841,17 @@ public boolean bind(final Policy policy, final Collection<Tag> tags, final boole
return callInTransaction(() -> {
boolean modified = false;

if (policy.getTags() == null) {
policy.setTags(new ArrayList<>());
}

if (!keepExisting) {
for (final Tag existingTag : policy.getTags()) {
if (!tags.contains(existingTag)) {
policy.getTags().remove(existingTag);
existingTag.getPolicies().remove(policy);
if (existingTag.getPolicies() != null) {
existingTag.getPolicies().remove(policy);
}
modified = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,11 +948,17 @@ public boolean bind(final Project project, final Collection<Tag> tags, final boo
return callInTransaction(() -> {
boolean modified = false;

if (project.getTags() == null) {
project.setTags(new ArrayList<>());
}

if (!keepExisting) {
for (final Tag existingTag : project.getTags()) {
if (!tags.contains(existingTag)) {
project.getTags().remove(existingTag);
existingTag.getProjects().remove(project);
if (existingTag.getProjects() != null) {
existingTag.getProjects().remove(project);
}
modified = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,21 @@ public void getProjectByUnknownTagTest() {

@Test
public void createProjectTest(){
Project project = new Project();
project.setName("Acme Example");
project.setVersion("1.0");
project.setDescription("Test project");
Response response = jersey.target(V1_PROJECT)
.request()
.header(X_API_KEY, apiKey)
.put(Entity.entity(project, MediaType.APPLICATION_JSON));
.put(Entity.json("""
{
"name": "Acme Example",
"version": "1.0",
"description": "Test project",
"tags": [
{
"name": "foo"
}
]
}
"""));
Assert.assertEquals(201, response.getStatus(), 0);
JsonObject json = parseJsonObject(response);
Assert.assertNotNull(json);
Expand All @@ -483,6 +490,8 @@ public void createProjectTest(){
Assert.assertEquals("Test project", json.getString("description"));
Assert.assertTrue(json.getBoolean("active"));
Assert.assertTrue(UuidUtil.isValidUUID(json.getString("uuid")));
assertThat(json.getJsonArray("tags").getValuesAs(JsonObject.class)).satisfiesExactly(
jsonObject -> assertThat(jsonObject.getString("name")).isEqualTo("foo"));
}

@Test
Expand Down
Loading