Skip to content

Dashboard import fails with unhandled KeyError when native_filter_configuration targets an unresolvable datasetUuid #46642

Backlog

Description

@vishalmore90

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

  1. Call update_id_refs() (or run ImportDashboardsCommand on a dashboard bundle whose dashboards/*.yaml passes preflight validation because no chart requires the missing dataset) with a native filter target referencing a datasetUuid absent from dataset_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, {}, {})
  1. Expected: Following the chart_customization_config pattern, update_id_refs logs a warning, removes datasetUuid and any stale datasetId on that target instead of raising KeyError, and allows the dashboard import to proceed (matching ImportDashboardsCommand._import's rule that native-filter UUIDs do not block import when unbundled).
  2. Actual: Raises KeyError: 'missing-uuid' at superset/commands/dashboard/importers/v1/utils.py:150, aborting dashboard/assets import with HTTP 500 in ImportDashboardsCommand.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.py lines 141–151 (buggy native_filter_configuration target lookup) vs. lines 164–183 (chart_customization_config handling).
    • superset/commands/dashboard/importers/v1/__init__.py lines 139–141, 178–196 (ImportDashboardsCommand._import intentionally omits native-filter dataset UUIDs from required_datasets), and line 329.
    • superset/commands/importers/v1/assets.py line 218 and superset/commands/importers/v1/examples.py line 260 (other callers of update_id_refs).
    • superset/commands/importers/v1/errors.py lines 310–335 (import_error_context / _asset_error converts unhandled KeyError into HTTP 500).
  • Existing related tests:
    • tests/unit_tests/dashboards/commands/importers/v1/utils_test.py lines 718–776 covers unresolvable datasetUuid and stale datasetId removal for chart_customization_config, with no equivalent test for native_filter_configuration.
    • tests/unit_tests/dashboards/commands/importers/v1/import_command_test.py lines 448–460 tests layout-only missing chart UUIDs (test_layout_only_chart_reference_does_not_reject_import).
  • Proposed fix:
    • Update native_filter_configuration handling in update_id_refs() (superset/commands/dashboard/importers/v1/utils.py, lines 142–151) to mirror chart_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 (and import_command_test.py) covering missing datasetUuid in native_filter_configuration, stale datasetId removal, and null native_filter_configuration / targets values.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    #bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions