Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

More robust visualization settings normalization (#69860)

Cam Saul984cf44eca51
diff --git a/src/metabase/models/interface.clj b/src/metabase/models/interface.clj
index 16cc261031c..a567f4264a1 100644
--- a/src/metabase/models/interface.clj
+++ b/src/metabase/models/interface.clj
@@ -347,6 +347,9 @@
[:= {:decode/normalize keyword} :name]
:string])
+;;; TODO (Cam 2026-02-18) move this out of the `models` module, it should either go into its own
+;;; `visualization-settings` module or into `queries` (so it can live with Saved Questions and friends). See
+;;; also [[metabase.models.visualization-settings]].
(defn normalize-visualization-settings
"The frontend uses JSON-serialized versions of MBQL clauses as keys in `:column_settings`. This normalizes them
to MBQL 4 clauses so things work correctly."
@@ -360,11 +363,18 @@
(not (sequential? x)) x
(= (first x) "ref") (lib/normalize ::viz-settings-ref x)
(= (first x) "name") (lib/normalize ::viz-settings-name x)
- :else (mbql.normalize/normalize x))))
+ :else (try
+ (mbql.normalize/normalize x)
+ (catch Throwable e
+ (log/debugf e "Error normalizing column settings key %s" (pr-str x))
+ nil)))))
json/encode))
(normalize-column-settings [column-settings]
- (into {} (for [[k v] column-settings]
- [(normalize-column-settings-key k) (walk/keywordize-keys v)])))
+ (into {} (for [[k v] column-settings
+ ;; remove nil (bad) keys
+ :let [k (normalize-column-settings-key k)]
+ :when (some? k)]
+ [k (walk/keywordize-keys v)])))
(mbql-field-clause? [form]
(and (vector? form)
(#{"field-id"
diff --git a/src/metabase/models/visualization_settings.cljc b/src/metabase/models/visualization_settings.cljc
index 0d91c9e5a6e..d60d256776b 100644
--- a/src/metabase/models/visualization_settings.cljc
+++ b/src/metabase/models/visualization_settings.cljc
@@ -18,7 +18,10 @@
In general, conversion functions in this namespace (i.e. those that convert various pieces from one form to the other)
will be prefixed with either `db->norm` or `norm->db`, depending on which direction they implement.
- "
+
+ TODO (Cam 2026-02-18) move this out of the `models` module, it should either go into its own
+ `visualization-settings` module or into `queries` (so it can live with Saved Questions and friends). See
+ also [[metabase.models.interface/normalize-visualization-settings]]."
(:require
#?@(:clj [[metabase.util.json :as json]])
[clojure.set :as set]
diff --git a/test/metabase/models/interface_test.clj b/test/metabase/models/interface_test.clj
index 6e3a6b8c5d1..6d20ae9bf1a 100644
--- a/test/metabase/models/interface_test.clj
+++ b/test/metabase/models/interface_test.clj
@@ -176,6 +176,15 @@
:graph.dimensions ["CREATED_AT"]}
(mi/normalize-visualization-settings viz-settings)))))
+(deftest ^:parallel normalize-invalid-visualization-settings-test
+ (testing "Unknown keys in `:column_settings` should be removed rather than causing normalization to fail (#69626)"
+ (let [viz-settings {"column_settings" {"[\"ref\",[\"expression\",\"expression\"]]" {:number_style "x"}
+ "[\"REF\",[\"expression\",\"expression\"]]" {:number_style "y"}
+ "[\"bad-clause\",\"a\"]" {:number_style "z"}}}]
+ (is (= {:column_settings
+ {"[\"ref\",[\"expression\",\"expression\"]]" {:number_style "x"}}}
+ (mi/normalize-visualization-settings viz-settings))))))
+
(deftest ^:parallel json-in-with-eliding
(is (= "{}" (#'mi/json-in-with-eliding {})))
(is (= (json/encode {:a "short"}) (#'mi/json-in-with-eliding {:a "short"})))
diff --git a/test/metabase/models/visualization_settings_test.cljc b/test/metabase/models/visualization_settings_test.cljc
index 9f489961f37..3bc1c2c6503 100644
--- a/test/metabase/models/visualization_settings_test.cljc
+++ b/test/metabase/models/visualization_settings_test.cljc
@@ -1,5 +1,9 @@
(ns metabase.models.visualization-settings-test
- "Tests for the shared visualization-settings namespace functions"
+ "Tests for the shared visualization-settings namespace functions
+
+ TODO (Cam 2026-02-18) move this out of the `models` module, it should either go into its own
+ `visualization-settings` module or into `queries` (so it can live with Saved Questions and friends). See
+ also [[metabase.models.interface/normalize-visualization-settings]]."
(:require #?@(:cljs [[goog.string :as gstring]]
:default [])
[clojure.test :as t]