Repository navigation
fix(dashboard): don't clone charts a copied layout leaves out - #45528
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
fitzee
left a comment
There was a problem hiding this comment.
LGTM, no blocking issues. This applies the fix suggested on #45472: clones are limited to charts whose IDs appear in the reconciled layout. The clone loop still iterates only original_dash.slices, so a layout chart that isn't a member of the source is never cloned. It still fails the remap with DashboardLayoutInvalidError, and zero charts are cloned, so the access gap noted on #45472 isn't widened. The duplicate_slices=False path is unchanged.
Verified:
- Reverting only the
continuecondition makestest_copy_without_charts_dropped_from_layout[*-True]fail withIntegrityError: UNIQUE constraint failed: dashboard_slices_version.... That is the history-capture failure from the description. With the fix, both changed unit files pass (28/28). - A layout that references the same chart twice (int and legacy string ID) produces one clone, and both slots point to it.
- A markdown-only layout produces zero clones. A malformed
CHARTnode is still rejected with a 422. - Missing or null
positionswithduplicate_slicesstill ends in the same 400 as before. It now fails before any clone is flushed. - The removed lines are mock-based tests. They had to go: with
positions: {}the fixed loop clones nothing, soclone.viewerswould never be set. The persisted-fixture replacements exercise the real loop. - CI on 0099a02 is green (postgres, mysql, sqlite, unit, pre-commit). The skipped jobs are frontend/docs only.
Two non-blocking metadata follow-ups are inline.
| # Duplicating slices as well, mapping old ids to new ones | ||
| for slc in original_dash.slices: | ||
| if slc.id in archived_ids: | ||
| if slc.id in archived_ids or slc.id not in layout_chart_ids: |
There was a problem hiding this comment.
Non-blocking. A side effect of skipping omitted charts: old_to_new_slice_ids no longer has entries for them, so _remap_filter_scopes leaves their IDs as the source's IDs. Before this change they were rewritten to orphan-clone IDs. Example: source has A=1 and B=2, the layout keeps only A, and A is cloned to 4. The copy's json_metadata ends up with:
chart_configurationkeys"2"and"4". The"2"entry belongs to the source's live chart B.chart_configuration["4"].crossFilters.chartsInScope == [2].- A native filter's
chartsInScope == [4, 2]andscope.excluded == [2].
The references were dangling before this change too. Now they point at a live chart on another dashboard. If B is later added to the copy, it picks up the stale cross-filter config. The follow-up suggested on #45472 (prune metadata to the layout's charts) is now the natural complement: after the remap, drop any ID in these structures that isn't in old_to_new_slice_ids.values().
There was a problem hiding this comment.
| # the remap below skips that slot because it has no chartId. | ||
| reconcile_position_json(metadata.get("positions"), original_dash.id) | ||
| chart_id: int | None | ||
| layout_chart_ids: set[int] = { |
There was a problem hiding this comment.
Non-blocking, pre-existing. timed_refresh_immune_slices and expanded_slices are never remapped on the duplicate path, even for charts that are cloned. In the A=1 to clone 4 case above, the copy keeps timed_refresh_immune_slices: [1, 2] and expanded_slices: {"1": true, "2": true}. As a result, every duplicated copy loses refresh immunity and expanded state for its charts. _remap_filter_scopes covers neither key. Out of scope here, but worth handling in the same follow-up: remap through old_to_new_slice_ids and prune to the layout. (default_filters has a similar problem: its old-ID keys are filtered against the new slice_ids, so they are all dropped.)
fitzee
left a comment
There was a problem hiding this comment.
No blocking issues; LGTM. The clone loop now skips charts the copied layout leaves out. It does this before any Slice row is written, the change is limited to the duplicate_slices=True path, and no blocking issues turned up.
Paths I checked that hold up:
- Which charts count as "in the layout".
superset/daos/dashboard.py:973readsCHARTnodes from the payload layout after the archived-slot replacement andreconcile_position_json. Hard-deleted and archived slots are already markdown at that point, so they can't add an ID. Tabs, rows and markdown carry nochartId. Native filters target datasets, not charts. No chart is rendered from outsidepositions, so a membership chart missing from the layout has nowhere to appear, and skipping its clone is correct. - Legacy dashboards with null
position_json. The UI copy (dashboardState.tscopyPayload) always sends the client layout. For a legacy dashboard that layout is built fromslices, so it includes every chart and these copies don't lose charts. An API call withduplicate_slices=Trueand nopositionsraises aKeyErrorat:973. Base already raised the sameKeyErrorat the remap loop (:995), so this is not a regression. It only fails sooner, before any clones are written. - Charts from other dashboards. The loop still iterates only
original_dash.slices. A layout chart that isn't a source member gets no clone, and the remap at:995raisesDashboardLayoutInvalidError. The copy can't clone a chart the source doesn't own. Theduplicate_slices=Falsepath is unchanged. It still links any chart the payload layout names without checking access. That gap is older than this PR and is what #45538 addresses. - Mutation test. I reverted only the
:980condition.test_copy_without_charts_dropped_from_layout[kept0-True]and[kept1-True]then fail withIntegrityError: UNIQUE constraint failed: dashboard_slices_version..., the history-capture failure described in the PR body. With the fix, all 28 tests in the two changed unit files pass. Thetest_creator_group_call_sites.pyrewrite was needed: withpositions: {}the old mocks would now clone nothing, and the newlen(copied.slices) == 3assertion proves the loop really runs. - Interaction with the related PRs. #45530 is stacked directly on this branch. It contains this hunk unchanged and handles the remaining chart-ID cleanup in metadata (see below). This PR merges cleanly with #45538, and their semantics agree: with a subset layout,
allowed_chart_idsbecomes the clone IDs; with an empty layout, it falls back to the source members, which is harmless. #45530 and #45538 do conflict with each other textually arounddash.params = original_dash.params, so whichever merges second needs a rebase.
Already raised in the existing threads and deferred to #45530:
- Out-of-layout chart IDs left in metadata. These are
chart_configurationkeys and theexcluded/chartsInScope/chartIdfields of filter scopes. They now stay as the source's chart IDs, where before they pointed at orphan clones. They are dangling either way and don't render. #45530 prunes them.
Nits (non-blocking):
superset/dashboards/schemas.py:488-492: theduplicate_slicesdescription still reads "copy all charts on the dashboard". It now copies only the charts in the supplied layout. Updating it also means regenerating the OpenAPI spec.tests/integration_tests/dashboards/api_tests.py:5447: the comment "Clones carry the source names, including ones the layout left out" no longer matches. Charts the layout leaves out aren't cloned anymore. The cleanup itself is still a reasonable safety net.
EnxDev
left a comment
There was a problem hiding this comment.
Read through this one. Limiting the clone loop to the reconciled layout's chart IDs is the right fix, it runs before any Slice row is flushed, and the parametrised unit and API tests pin both the chart count and the orphan count for the subset and empty layouts. The mock-to-fixture rewrite in the creator-group tests is a real improvement too, since the old mocks would have passed with the loop cloning nothing.
One note inline on the docs page that still says live charts are always cloned.
| # Duplicating slices as well, mapping old ids to new ones | ||
| for slc in original_dash.slices: | ||
| if slc.id in archived_ids: | ||
| if slc.id in archived_ids or slc.id not in layout_chart_ids: |
There was a problem hiding this comment.
This changes what "Open as new" does for a version whose layout predates some of the current charts, and the docs still describe the old behaviour.
docs/docs/using-superset/version-history.mdx:321 says "Live charts are still cloned" under copying with charts. Worth a clause there saying only the charts in the copied layout are cloned, so a user opening an older version isn't surprised when newer charts don't come along.
There was a problem hiding this comment.
Thanks. Updated the version-history docs to say that only live charts in the copied layout are cloned, so opening an older version won't bring along charts added later.
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
aminghadersohi
left a comment
There was a problem hiding this comment.
LGTM. Reviewed the diff and the thread replies; the fixes and regression tests cover the reported cases.
SUMMARY
What users saw. Copying a dashboard with “duplicate charts” could create unused charts when the copied layout left out charts from the source dashboard, including when opening an older version as a new dashboard. With history capture enabled, the copy could also fail.
Why, in plain terms. The copy created every source chart first, then removed charts that the copied layout did not use. Those charts had already been saved.
What this changes. Only charts present in the copied layout are duplicated. Existing handling of archived or deleted charts and legacy chart IDs is preserved.
The clone loop uses the reconciled layout's normalized chart IDs before creating any chart rows. Regression tests assert total chart counts and orphan counts for subset and empty layouts.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Not applicable: this corrects backend copy behavior without a UI change.
TESTING INSTRUCTIONS
Validated locally: 311 DAO/dashboard unit tests, all 11 dashboard-copy API tests using isolated SQLite, and changed-file pre-commit checks including MyPy. The new duplicate-copy regressions fail before the fix.
ADDITIONAL INFORMATION