korbit-ai[bot] commented on code in PR #34418: URL: https://github.com/apache/superset/pull/34418#discussion_r2244419129
########## superset/commands/dashboard/importers/v1/utils.py: ########## @@ -139,9 +139,41 @@ def update_id_refs( # pylint: disable=too-many-locals # noqa: C901 native_filter["scope"]["excluded"] = [ id_map[old_id] for old_id in scope_excluded if old_id in id_map ] - + fixed=update_cross_filter_scoping(fixed,id_map) return fixed +def update_cross_filter_scoping(config: dict[str, Any], + id_map: dict[int, int] + ) -> dict[str, Any]: + # fix cross filter references + cross_filter_global_config = config.get("metadata", {}).get( + "global_chart_configuration", {} + ) + scope_excluded = cross_filter_global_config.get("scope", {}).get("excluded", []) + if scope_excluded: + cross_filter_global_config["scope"]["excluded"] = [ + id_map[old_id] for old_id in scope_excluded if old_id in id_map + ] + metadata = config.get("metadata", {}) + if "chart_configuration" in metadata: + # in cross_filter_scopes the key is the chart ID as a string; we need to update + # them to be the new ID as a string: + metadata["chart_configuration"] = { + str(id_map[int(old_id)]): columns + for old_id, columns in metadata["chart_configuration"].items() + if int(old_id) in id_map + } + # now update scope excluded to use new IDs: + for excluded_charts in metadata["chart_configuration"].values(): + if excluded_charts["id"] in id_map: + excluded_charts["id"]=id_map[excluded_charts["id"]] + if isinstance(excluded_charts["crossFilters"]["scope"],dict): + scope_excluded=excluded_charts["crossFilters"]["scope"]["excluded"] Review Comment: ### Unsafe Nested Dictionary Access <sub></sub> <details> <summary>Tell me more</summary> ###### What is the issue? Accessing nested dictionary keys 'crossFilters' and 'scope' without checking their existence can lead to KeyError exceptions. ###### Why this matters If any of the nested keys are missing, the code will crash during the dashboard import process, failing to gracefully handle missing cross-filter configurations. ###### Suggested change ∙ *Feature Preview* Add checks for all nested keys before accessing them: ```python if ("crossFilters" in excluded_charts and "scope" in excluded_charts["crossFilters"] and isinstance(excluded_charts["crossFilters"]["scope"], dict)): scope_excluded = excluded_charts["crossFilters"]["scope"].get("excluded", []) ``` ###### Provide feedback to improve future suggestions [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/cf8c3243-14f2-4492-9b69-c865ab6c6834/upvote) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/cf8c3243-14f2-4492-9b69-c865ab6c6834?what_not_true=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/cf8c3243-14f2-4492-9b69-c865ab6c6834?what_out_of_scope=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/cf8c3243-14f2-4492-9b69-c865ab6c6834?what_not_in_standard=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/cf8c3243-14f2-4492-9b69-c865ab6c6834) </details> <sub> 💬 Looking for more details? Reply to this comment to chat with Korbit. </sub> <!--- korbi internal id:ac73ddd9-0241-4b59-b8d8-46de43be8fc3 --> [](ac73ddd9-0241-4b59-b8d8-46de43be8fc3) -- 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-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org