diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java index 2c374e939f0..1c01d2bc11c 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java @@ -334,10 +334,19 @@ public abstract class DashboardRepository { applyTags(dashboard); } - private void updateChartRelationships(Dashboard dashboard) { - // Add relationship from dashboard to chart + private void updateChartRelationships(Dashboard dashboard) throws IOException { String dashboardId = dashboard.getId().toString(); + + // Add relationship from dashboard to chart if (dashboard.getCharts() != null) { + // Remove any existing charts associated with this dashboard + List existingCharts = getCharts(dashboard); + if (existingCharts != null) { + for (Chart chart: existingCharts) { + relationshipDAO().delete(dashboardId, chart.getId().toString(), Relationship.CONTAINS.ordinal()); + } + } + for (EntityReference chart : dashboard.getCharts()) { relationshipDAO().insert(dashboardId, chart.getId().toString(), Entity.DASHBOARD, Entity.CHART, Relationship.CONTAINS.ordinal()); diff --git a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/dashboards/DashboardResourceTest.java b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/dashboards/DashboardResourceTest.java index e991000abcd..4344044dfad 100644 --- a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/dashboards/DashboardResourceTest.java +++ b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/dashboards/DashboardResourceTest.java @@ -384,6 +384,23 @@ public class DashboardResourceTest extends CatalogApplicationTest { assertEquals("newDescription", dashboard.getDescription()); } + @Test + public void put_AddRemoveDashboardChartsUpdate_200(TestInfo test) throws HttpResponseException { + CreateDashboard request = create(test).withService(SUPERSET_REFERENCE).withDescription(null); + createAndCheckDashboard(request, adminAuthHeaders()); + + Dashboard dashboard = updateAndCheckDashboard(request + .withDescription("newDescription").withCharts(CHART_REFERENCES), + OK, adminAuthHeaders()); + validateDashboardCharts(dashboard, CHART_REFERENCES); + // remove a chart + CHART_REFERENCES.remove(0); + dashboard = updateAndCheckDashboard(request + .withDescription("newDescription").withCharts(CHART_REFERENCES), + OK, adminAuthHeaders()); + validateDashboardCharts(dashboard, CHART_REFERENCES); + } + @Test public void get_nonExistentDashboard_404_notFound() { HttpResponseException exception = assertThrows(HttpResponseException.class, () ->