Repository navigation
Description
Bug description
When importing a dashboard bundle, ImportDashboardsCommand._import (superset/commands/dashboard/importers/v1/__init__.py, lines 178–196) intentionally does not require datasets referenced only by native_filter_configuration to exist in the bundle, noting:
# Layout and native-filter UUIDs can outlive their exported assets.
# Only require dependencies of files that will actually be imported.
However, when ImportDashboardsCommand._import (line 329), ImportAssetsCommand._import (superset/commands/importers/v1/assets.py, line 218), or ImportExamplesCommand._import (superset/commands/importers/v1/examples.py, line 260) calls update_id_refs(config, chart_ids, dataset_info) in superset/commands/dashboard/importers/v1/utils.py, the native-filter target remapping performs an unchecked dictionary lookup on dataset_info (lines 142–151):
# superset/commands/dashboard/importers/v1/utils.py (lines 141-151)
# fix native filter references
native_filter_configuration = fixed.get("metadata", {}).get(
"native_filter_configuration", []
)
for native_filter in native_filter_configuration:
targets = native_filter.get("targets", [])
for target in targets:
dataset_uuid = target.pop("datasetUuid", None)
if dataset_uuid:
target["datasetId"] = dataset_info[dataset_uuid]["datasource_id"]If dataset_uuid is not present in dataset_info (for example, a native filter whose referenced dataset was deleted or omitted from a partial/hand-edited bundle), dataset_info[dataset_uuid] raises an unhandled KeyError. Inside ImportDashboardsCommand.run(), import_error_context("dashboard bundle") (superset/commands/importers/v1/errors.py, lines 310–335) catches this KeyError as an unexpected failure and aborts the entire dashboard import with HTTP 500 ("Could not import dashboard bundle. An unexpected error occurred...").
In the very same update_id_refs function, chart_customization_config (lines 164–183) already handles missing/unresolvable datasetUuid entries safely by checking dataset_info.get(dataset_uuid), removing any stale integer datasetId so the control does not silently bind to an unrelated dataset in the destination environment, logging a warning, and guarding nullable lists with or []:
# superset/commands/dashboard/importers/v1/utils.py (lines 164-183)
# fix display control dataset references
for customization in (
fixed.get("metadata", {}).get("chart_customization_config") or []
):
for target in customization.get("targets") or []:
dataset_uuid = target.pop("datasetUuid", None)
if dataset_uuid:
info = dataset_info.get(dataset_uuid)
if info:
target["datasetId"] = info["datasource_id"]
else:
# UUID present but unresolvable — remove stale integer ID
# so the control fails visibly rather than binding to
# whatever dataset happens to own that ID in this environment
target.pop("datasetId", None)
logger.warning(
"Display control target references unknown dataset UUID %s; "
"datasetId will not be restored",
dataset_uuid,
)Note also that lines 142–147 use .get("native_filter_configuration", []) and .get("targets", []) without or [], which raises TypeError: 'NoneType' object is not iterable if those keys are explicitly null in the imported YAML/JSON metadata.
Steps to reproduce
- Call
update_id_refs()(or runImportDashboardsCommandon a dashboard bundle whosedashboards/*.yamlpasses preflight validation because no chart requires the missing dataset) with a native filter target referencing adatasetUuidabsent fromdataset_info:
from superset.commands.dashboard.importers.v1.utils import update_id_refs
config = {
"position": {},
"metadata": {
"native_filter_configuration": [
{
"id": "NATIVE_FILTER-1",
"targets": [{"datasetUuid": "missing-uuid", "datasetId": 99}],
}
]
},
}
update_id_refs(config, {}, {})- Expected: Following the
chart_customization_configpattern,update_id_refslogs a warning, removesdatasetUuidand any staledatasetIdon that target instead of raisingKeyError, and allows the dashboard import to proceed (matchingImportDashboardsCommand._import's rule that native-filter UUIDs do not block import when unbundled). - Actual: Raises
KeyError: 'missing-uuid'atsuperset/commands/dashboard/importers/v1/utils.py:150, aborting dashboard/assets import with HTTP500inImportDashboardsCommand.run().
Screenshots/recordings
No response
Superset version
master / latest-dev
Python version
3.11
Node version
16
Browser
Chrome
Additional context
Additional context
- Code pointers (
master):superset/commands/dashboard/importers/v1/utils.pylines 141–151 (buggynative_filter_configurationtarget lookup) vs. lines 164–183 (chart_customization_confighandling).superset/commands/dashboard/importers/v1/__init__.pylines 139–141, 178–196 (ImportDashboardsCommand._importintentionally omits native-filter dataset UUIDs fromrequired_datasets), and line 329.superset/commands/importers/v1/assets.pyline 218 andsuperset/commands/importers/v1/examples.pyline 260 (other callers ofupdate_id_refs).superset/commands/importers/v1/errors.pylines 310–335 (import_error_context/_asset_errorconverts unhandledKeyErrorinto HTTP500).
- Existing related tests:
tests/unit_tests/dashboards/commands/importers/v1/utils_test.pylines 718–776 covers unresolvabledatasetUuidand staledatasetIdremoval forchart_customization_config, with no equivalent test fornative_filter_configuration.tests/unit_tests/dashboards/commands/importers/v1/import_command_test.pylines 448–460 tests layout-only missing chart UUIDs (test_layout_only_chart_reference_does_not_reject_import).
- Proposed fix:
- Update
native_filter_configurationhandling inupdate_id_refs()(superset/commands/dashboard/importers/v1/utils.py, lines 142–151) to mirrorchart_customization_config:for native_filter in ( fixed.get("metadata", {}).get("native_filter_configuration") or [] ): for target in native_filter.get("targets") or []: dataset_uuid = target.pop("datasetUuid", None) if dataset_uuid: info = dataset_info.get(dataset_uuid) if info: target["datasetId"] = info["datasource_id"] else: target.pop("datasetId", None) logger.warning( "Native filter target references unknown dataset UUID %s; " "datasetId will not be restored", dataset_uuid, )
- Add unit tests in
tests/unit_tests/dashboards/commands/importers/v1/utils_test.py(andimport_command_test.py) covering missingdatasetUuidinnative_filter_configuration, staledatasetIdremoval, andnullnative_filter_configuration/targetsvalues.
- Update
Checklist
- I have searched Superset docs and Slack and didn't find a solution to my problem.
- 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.
Activity