SEPURI-SAI-KRISHNA opened a new issue, #45300:
URL: https://github.com/apache/superset/issues/45300

   ### Bug description
   
   Importing a dashboard fails with "Import dashboard failed for an unknown 
reason" when the dashboard's `json_metadata` names a chart ID that the import 
cannot look up. The message gives no hint about which chart or which field is 
at fault.
   
   `update_id_refs` in `superset/commands/dashboard/importers/v1/utils.py` 
remaps old chart IDs to new ones. Four metadata fields carry chart IDs, and two 
of them index the map directly:
   
   ```python
   if "timed_refresh_immune_slices" in metadata:
       metadata["timed_refresh_immune_slices"] = [
           id_map[old_id] for old_id in metadata["timed_refresh_immune_slices"]
       ]
   ...
   if "expanded_slices" in metadata:
       metadata["expanded_slices"] = {
           str(id_map[int(old_id)]): value
           for old_id, value in metadata["expanded_slices"].items()
       }
   ```
   
   The other two, `filter_scopes` and `default_filters`, check first, because 
#17741 added that check for this exact case. The test it shipped still states 
the intent:
   
   > A chart might be removed from a dashboard but still remain in the list of 
charts immune to filters. The missing chart ID should be simply ignored when 
the dashboard is imported.
   
   `timed_refresh_immune_slices` and `expanded_slices` never got the same 
treatment.
   
   **Reproduction**
   
   Call `update_id_refs` with one chart in the layout, which is how the 
existing tests for it are written:
   
   ```python
   config = {
       "position": {
           "CHART1": {"id": "CHART1", "type": "CHART",
                      "meta": {"chartId": 101, "uuid": "uuid1"}},
       },
       "metadata": {"timed_refresh_immune_slices": [101, 999]},
   }
   update_id_refs(config, {"uuid1": 1}, {})
   ```
   
   Results on master, where 101 is in the bundle and 999 is not:
   
   | metadata | result |
   | --- | --- |
   | `timed_refresh_immune_slices: [101, 999]` | `KeyError: 999` |
   | `timed_refresh_immune_slices: ["101"]` | `KeyError: '101'` |
   | `expanded_slices: {"101": true, "999": false}` | `KeyError: 999` |
   | `expanded_slices: {"abc": true}` | `ValueError: invalid literal for int()` 
|
   | `filter_scopes: {"999": {}}` | fine, entry dropped |
   | `default_filters: '{"999": {}}'` | fine, entry dropped |
   
   The second row is a reference to a chart that *is* in the bundle. 
`timed_refresh_immune_slices` can hold string-typed IDs: 
`superset/commands/dashboard/export.py` coerces these fields with `int()` on 
the way out, and the MCP `remove_chart_from_dashboard` tool has a test named 
"timed_refresh_immune_slices entries may be string-typed IDs". The import side 
does no coercion, so a string entry misses the integer-keyed map and raises.
   
   The exception is caught by the blanket `except Exception` in 
`superset/commands/importers/v1/__init__.py` and re-raised as 
`DashboardImportError`, which is where the "unknown reason" message comes from.
   
   **How an unresolvable ID gets there**
   
   Nothing prunes these two fields when a chart is deleted or leaves a 
dashboard. `DeleteChartCommand` does not touch dashboard metadata, and the only 
pruning code in the tree is the MCP `remove_chart_from_dashboard` tool. So 
stale entries are normal. Three ways one then meets an incomplete map:
   
   1. A chart in the layout is skipped at import when its dataset is not 
resolvable in the bundle. 
`superset/commands/dashboard/importers/v1/__init__.py` only imports a chart if 
it has a `datasource_ref` or a `dataset_uuid` present in `dataset_info`, and 
`dataset_info` only receives datasets whose database is also in the bundle. 
Validation does not require either, so the chart is dropped quietly and its old 
ID never enters the map.
   2. `/api/v1/assets/import/` goes through the same function and lets you 
import a dashboard without every chart yaml it references.
   3. The exporter only started pruning these two fields in #40588 (June 2026). 
Bundles produced before that keep their stale entries, and importing older 
bundles is supported.
   
   ### Superset version
   
   master / latest-dev
   
   ### Python version
   
   3.11
   
   ### Node version
   
   Not applicable
   
   ### Browser
   
   Not applicable
   
   ### Additional context
   
   Verified on master at `ffee0a843f`. The failures above come from calling 
`update_id_refs` directly, so there is no server stacktrace to paste. The call 
chain that reaches it is:
   
   - `superset/commands/importers/v1/__init__.py:103` calls `self._import(...)` 
inside a `try`
   - `superset/commands/dashboard/importers/v1/__init__.py:277` calls 
`update_id_refs(config, chart_ids, dataset_info)`
   - `superset/commands/dashboard/importers/v1/utils.py:90` raises `KeyError` 
from `id_map[old_id]`
   - `superset/commands/importers/v1/__init__.py:107` catches it with `except 
Exception` and raises `DashboardImportError`, whose message is "Import 
dashboard failed for an unknown reason"
   
   PR #37042 found the two unguarded sites in January and was closed by its own 
author with no human review. It also bundled an unrelated change to 
`DeleteChartCommand`. This report covers the import side only, and adds the 
string-typed ID case that PR did not.
   
   ### Checklist
   
   - [x] I have searched Superset docs and Slack and didn't find a solution to 
my problem.
   - [x] I have searched the GitHub issue tracker and didn't find a similar bug 
report.
   - [ ] I have checked Superset's logs for errors and if I found a relevant 
Python stacktrace, I included it here as text in the "additional context" 
section.
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to