You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "mikebridge (via GitHub)" <gi...@apache.org> on 2026/10/19 18:18:09 UTC
[PR] fix(dashboard): copy a layout that omits the source's current charts [superset]
mikebridge opened a new pull request, #45472:
URL: https://github.com/apache/superset/pull/45472
### SUMMARY
**TL;DR:** Copying a dashboard with a layout that leaves out some of the source dashboard's current charts failed with a generic `400 Arguments are not correct`, and nothing was logged. This PR fixes the copy and logs the cause when a copy fails.
**Background.** In version history, "Open as new dashboard" forks an older version by posting that version's layout to `POST /api/v1/dashboard/<id>/copy/` (without `duplicate_slices`). The copy endpoint rebuilds the new dashboard's charts from `json_metadata.positions`.
**Problem.** `DashboardDAO.copy_dashboard` first linked all of the source's current charts to the new dashboard (`dash.slices = original_dash.slices`). Then `set_dash_metadata` replaced that list with the charts in the layout. For each chart the layout dropped, the chart's unloaded `Slice.dashboards` backref collection was left holding a queued append followed by a remove. On flush, SQLAlchemy issued `DELETE FROM dashboard_slices` for a row that was never inserted. That raised `StaleDataError`, which the command's transaction handler turned into `DashboardCopyError`, and the API returned the generic 400. `on_error` logs only exceptions that carry an `exception` attribute, so nothing was logged.
Any older version that doesn't contain every chart the dashboard has today hits this. It also hits an empty layout. It happens on PostgreSQL and SQLite alike.
**Changes.**
- `DashboardDAO.copy_dashboard` links the source's charts only when no layout is sent. With a layout, `set_dash_metadata` remains the only place that sets membership. The `duplicate_slices` path is unchanged, and the integration test covers it with partial and empty layouts too.
- `copy_dash` logs `DashboardCopyError` with `exc_info`, so the underlying database error appears in the logs. The response is unchanged. The log sits in the REST route so the shared `on_error` helper, which every command's transaction uses, keeps its behavior. The MCP duplicate tool still reports the error without logging its cause.
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A (API behavior).
Before: `POST /api/v1/dashboard/<id>/copy/` with a layout that omits a current chart returns `400 {"message": "Arguments are not correct"}`. After: it returns `200`, and the new dashboard has exactly the charts in the layout. The source dashboard is unchanged.
### TESTING INSTRUCTIONS
- Unit (SQLite): `pytest tests/unit_tests/dao/test_copy_dashboard_legacy_ids.py -k dropped_from_layout`. It fails with `StaleDataError` before the fix.
- Integration (run on PostgreSQL): `pytest tests/integration_tests/dashboards/api_tests.py -k "copy_dashboard_layout_without_current_charts or copy_dashboard_logs_database_error"`. Both fail before the fix.
- Manually: with version history on, add a chart to a dashboard, then open an earlier version in version history and choose "Open as new dashboard". The new dashboard opens with that version's charts.
### ADDITIONAL INFORMATION
- [ ] Has associated issue:
- [ ] Required feature flags:
- [ ] Changes UI
- [ ] Includes DB Migration (follow approval process in [SIP-59](https://github.com/apache/superset/issues/13351))
- [ ] Migration is atomic, supports rollback & is backwards-compatible
- [ ] Confirm DB migration upgrade and downgrade tested
- [ ] Runtime estimates and downtime expectations provided
- [ ] Introduces new feature or API
- [ ] Removes existing feature or API
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dashboard): copy a layout that omits the source's current charts [superset]
Posted by "mikebridge (via GitHub)" <gi...@apache.org>.
mikebridge commented on code in PR #45472:
URL: https://github.com/apache/superset/pull/45472#discussion_r4327056853
##########
superset/daos/dashboard.py:
##########
@@ -857,7 +857,10 @@ def copy_dashboard(
]
)
value["meta"]["chartId"] = new_id
- else:
+ elif metadata.get("positions") is None:
Review Comment:
Agreed. This predates the PR (PUT on a dashboard you own has the same gap), so I've kept it out of this fix and it's tracked as a follow-up: limit the layout's chart IDs to the source's members, plus charts from version history the caller can access, on the server.
##########
superset/daos/dashboard.py:
##########
@@ -857,7 +857,10 @@ def copy_dashboard(
]
)
value["meta"]["chartId"] = new_id
- else:
+ elif metadata.get("positions") is None:
+ # With a layout, set_dash_metadata links exactly the layout's charts.
+ # Linking the source's charts first and then dropping some makes the
+ # flush delete dashboard_slices rows that were never inserted.
dash.slices = original_dash.slices
dash.params = original_dash.params
Review Comment:
Right, the copy starts from the source's current params and only overlays the posted keys, so a version fork inherits today's keys and per-chart entries for charts the layout dropped. Pre-existing, so not changing it here; tracked as a follow-up to build the fork's metadata from the snapshot and prune per-chart keys to the layout's charts.
##########
tests/integration_tests/dashboards/api_tests.py:
##########
@@ -5381,6 +5382,96 @@ def test_copy_dashboard_layout_error_returns_422_without_writes(
db.session.delete(other)
db.session.commit()
+ @parameterized.expand([(True, False), (False, False), (True, True), (False, True)])
+ def test_copy_dashboard_layout_without_current_charts(
+ self, keep_first: bool, duplicate_slices: bool
+ ) -> None:
+ """Copy a layout that omits charts the source has, as a version fork does."""
+ from superset.connectors.sqla.models import SqlaTable
+
+ admin: User = self.get_user(ADMIN_USERNAME)
+ dataset_id: int = (
+ db.session.query(SqlaTable.id).order_by(SqlaTable.id).first()[0]
+ )
+ first: Slice = self.insert_chart("copy-subset-first", [admin.id], dataset_id)
+ second: Slice = self.insert_chart("copy-subset-second", [admin.id], dataset_id)
+ dashboard: Dashboard = self.insert_dashboard(
+ "copy-subset-source", None, [admin.id], slices=[first, second]
+ )
+ source_members: set[int] = {first.id, second.id}
+ positions: dict[str, Any] = (
+ {
+ "CHART-first": {
+ "id": "CHART-first",
+ "type": "CHART",
+ "meta": {"chartId": first.id, "width": 4, "height": 50},
+ }
+ }
+ if keep_first
+ else {}
+ )
+ self.login(ADMIN_USERNAME)
+ copied_id: int | None = None
+ try:
+ response: TestResponse = self.client.post(
+ f"/api/v1/dashboard/{dashboard.id}/copy/",
+ json={
+ "dashboard_title": "copy-subset-result",
+ "duplicate_slices": duplicate_slices,
+ "json_metadata": json.dumps({"positions": positions}),
+ },
+ )
+ assert response.status_code == 200, response.data
+ copied_id = response.json["result"]["id"]
+ db.session.expire_all()
+ copied: Dashboard = db.session.get(Dashboard, copied_id)
+ expected: set[str] = {"copy-subset-first"} if keep_first else set()
+ assert {chart.slice_name for chart in copied.slices} == expected
+ assert {chart.id for chart in dashboard.slices} == source_members
+ finally:
+ db.session.rollback()
+ if copied_id is not None:
+ db.session.delete(db.session.get(Dashboard, copied_id))
+ db.session.delete(dashboard)
+ # Clones carry the source names, including ones the layout left out.
Review Comment:
Good catch. The clone loop has always cloned every non-archived member regardless of the layout, so that's pre-existing too. Tracked as a follow-up to skip clones whose original isn't in the layout and assert the orphan count in this test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dashboard): copy a layout that omits the source's current charts [superset]
Posted by "codeant-ai-for-open-source[bot] (via GitHub)" <gi...@apache.org>.
codeant-ai-for-open-source[bot] commented on PR #45472:
URL: https://github.com/apache/superset/pull/45472#issuecomment-6275463962
## CodeAnt PR Risk: Low Risk
- The PR appears safe to merge; when a layout is supplied, the copied dashboard links only the charts referenced by that layout.
- Regression tests cover layouts omitting some or all source charts, with and without chart duplication, and verify the source membership remains intact.
- Copy failures keep the generic 400 response while logging the source ID and exception traceback.
**Assessed commit:** `d3498f8ac646`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dashboard): copy a layout that omits the source's current charts [superset]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #45472:
URL: https://github.com/apache/superset/pull/45472#issuecomment-6274538321
## [Codecov](https://app.codecov.io/gh/apache/superset/pull/45472?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 64.93%. Comparing base ([`99ecfe5`](https://app.codecov.io/gh/apache/superset/commit/99ecfe5801c4cbb79eedd475003b134b6ba7ff28?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`d3498f8`](https://app.codecov.io/gh/apache/superset/commit/d3498f8ac646de312c3545e1fd10121a491318b1?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 1 commits behind head on master.
> :exclamation: There is a different number of reports uploaded between BASE (99ecfe5) and HEAD (d3498f8). Click for more details.
>
> <details><summary>HEAD has 4 uploads less than BASE</summary>
>
>| Flag | BASE (99ecfe5) | HEAD (d3498f8) |
>|------|------|------|
>|python|7|3|
></details>
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #45472 +/- ##
===========================================
- Coverage 83.56% 64.93% -18.63%
===========================================
Files 3048 3048
Lines 199836 199839 +3
Branches 46613 46614 +1
===========================================
- Hits 166984 129766 -37218
- Misses 29610 67133 +37523
+ Partials 3242 2940 -302
```
| [Flag](https://app.codecov.io/gh/apache/superset/pull/45472/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [hive](https://app.codecov.io/gh/apache/superset/pull/45472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `34.95% <0.00%> (-0.01%)` | :arrow_down: |
| [mysql](https://app.codecov.io/gh/apache/superset/pull/45472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [postgres](https://app.codecov.io/gh/apache/superset/pull/45472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [presto](https://app.codecov.io/gh/apache/superset/pull/45472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `36.70% <0.00%> (-0.01%)` | :arrow_down: |
| [python](https://app.codecov.io/gh/apache/superset/pull/45472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `52.86% <100.00%> (-34.24%)` | :arrow_down: |
| [sqlite](https://app.codecov.io/gh/apache/superset/pull/45472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `52.74% <100.00%> (+<0.01%)` | :arrow_up: |
| [unit](https://app.codecov.io/gh/apache/superset/pull/45472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/superset/pull/45472?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dashboard): copy a layout that omits the source's current charts [superset]
Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi merged PR #45472:
URL: https://github.com/apache/superset/pull/45472
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dashboard): copy a layout that omits the source's current charts [superset]
Posted by "fitzee (via GitHub)" <gi...@apache.org>.
fitzee commented on code in PR #45472:
URL: https://github.com/apache/superset/pull/45472#discussion_r4321912194
##########
superset/daos/dashboard.py:
##########
@@ -857,7 +857,10 @@ def copy_dashboard(
]
)
value["meta"]["chartId"] = new_id
- else:
+ elif metadata.get("positions") is None:
+ # With a layout, set_dash_metadata links exactly the layout's charts.
+ # Linking the source's charts first and then dropping some makes the
+ # flush delete dashboard_slices rows that were never inserted.
dash.slices = original_dash.slices
dash.params = original_dash.params
Review Comment:
Non-blocking, pre-existing. `set_dash_metadata` starts from `dash.params`, which is the source's *current* metadata here, and only overlays the keys in the payload. For a version fork, any key the old snapshot lacks comes from today's dashboard. Keys that are carried through also aren't pruned to the layout's charts. With a layout that keeps only chart A, `chart_configuration`, `timed_refresh_immune_slices` and `expanded_slices` entries for the omitted chart B survive in the copy's `json_metadata`. Only `filter_scopes` and `default_filters` are filtered by `slice_ids`. The frontend seems to tolerate dangling cross-filter entries, but the fork isn't a faithful copy of the version.
##########
tests/integration_tests/dashboards/api_tests.py:
##########
@@ -5381,6 +5382,96 @@ def test_copy_dashboard_layout_error_returns_422_without_writes(
db.session.delete(other)
db.session.commit()
+ @parameterized.expand([(True, False), (False, False), (True, True), (False, True)])
+ def test_copy_dashboard_layout_without_current_charts(
+ self, keep_first: bool, duplicate_slices: bool
+ ) -> None:
+ """Copy a layout that omits charts the source has, as a version fork does."""
+ from superset.connectors.sqla.models import SqlaTable
+
+ admin: User = self.get_user(ADMIN_USERNAME)
+ dataset_id: int = (
+ db.session.query(SqlaTable.id).order_by(SqlaTable.id).first()[0]
+ )
+ first: Slice = self.insert_chart("copy-subset-first", [admin.id], dataset_id)
+ second: Slice = self.insert_chart("copy-subset-second", [admin.id], dataset_id)
+ dashboard: Dashboard = self.insert_dashboard(
+ "copy-subset-source", None, [admin.id], slices=[first, second]
+ )
+ source_members: set[int] = {first.id, second.id}
+ positions: dict[str, Any] = (
+ {
+ "CHART-first": {
+ "id": "CHART-first",
+ "type": "CHART",
+ "meta": {"chartId": first.id, "width": 4, "height": 50},
+ }
+ }
+ if keep_first
+ else {}
+ )
+ self.login(ADMIN_USERNAME)
+ copied_id: int | None = None
+ try:
+ response: TestResponse = self.client.post(
+ f"/api/v1/dashboard/{dashboard.id}/copy/",
+ json={
+ "dashboard_title": "copy-subset-result",
+ "duplicate_slices": duplicate_slices,
+ "json_metadata": json.dumps({"positions": positions}),
+ },
+ )
+ assert response.status_code == 200, response.data
+ copied_id = response.json["result"]["id"]
+ db.session.expire_all()
+ copied: Dashboard = db.session.get(Dashboard, copied_id)
+ expected: set[str] = {"copy-subset-first"} if keep_first else set()
+ assert {chart.slice_name for chart in copied.slices} == expected
+ assert {chart.id for chart in dashboard.slices} == source_members
+ finally:
+ db.session.rollback()
+ if copied_id is not None:
+ db.session.delete(db.session.get(Dashboard, copied_id))
+ db.session.delete(dashboard)
+ # Clones carry the source names, including ones the layout left out.
Review Comment:
Non-blocking. This cleanup shows the side effect of the `duplicate_slices=True` cases. The clone loop clones every non-archived member of the source, whether or not the layout references it. Charts the layout leaves out become orphan clones: owned by the caller, on no dashboard, and recorded as added-then-removed in the new dashboard's association history. The test only checks `copied.slices`, so it passes while leaving those orphans behind. Skipping clones whose original ID isn't in the layout's chart IDs would fix this. The remap and `filter_scopes` mapping already only use IDs from the layout. At minimum, assert the orphan count so a later change to this behavior is deliberate.
##########
superset/daos/dashboard.py:
##########
@@ -857,7 +857,10 @@ def copy_dashboard(
]
)
value["meta"]["chartId"] = new_id
- else:
+ elif metadata.get("positions") is None:
Review Comment:
Non-blocking, pre-existing. With this change the posted layout is the only thing that decides the copy's membership. Nothing on the server checks that the layout's chart IDs belong to the source or that the caller can access them. A non-duplicate copy whose layout references a chart from an unrelated dashboard links that chart to the new dashboard (confirmed in a SQLite DAO harness). `createDashboardFromSnapshot` filters unreachable charts through `fetchReachableChartIds`, but that runs in the browser and isn't access control. `PUT` on a dashboard you own has the same gap, so this PR doesn't widen it. That matters because dashboard membership feeds the `dashboardId` datasource grant in `raise_for_access` (viewer-promiscuous / embedded paths). It's worth a follow-up that limits layout chart IDs to the source's members, plus charts from version history the caller can access.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org