Skip to content

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

fix: pivoting json values (#65183)

Edward Pagetcb2510a55d47
diff --git a/frontend/src/metabase/lib/data_grid.js b/frontend/src/metabase/lib/data_grid.js
index 407add09357..fcac8cc420f 100644
--- a/frontend/src/metabase/lib/data_grid.js
+++ b/frontend/src/metabase/lib/data_grid.js
@@ -49,7 +49,7 @@ export function multiLevelPivot(data, settings) {
indexes.map((index) =>
_.memoize(
(value) => formatValue(value, columnSettings[index]),
- (value) => [value, index].join(),
+ (value) => JSON.stringify(value) + String(index),
),
),
);
diff --git a/src/metabase/pivot/core.cljc b/src/metabase/pivot/core.cljc
index e55fadb0184..2e9c2643ae4 100644
--- a/src/metabase/pivot/core.cljc
+++ b/src/metabase/pivot/core.cljc
@@ -315,7 +315,11 @@
(mapv
(fn [{:keys [value children] :as node}]
(assoc node
- :value (formatter value)
+ :value #?(:clj (formatter value)
+ ;; if we're in clojurescript these formatting functions are JS-based which means
+ ;; they cannot handle clojure data types so we need to convert collections into js
+ ;; types. We do it only for collections so as not to convert unnecessarily
+ :cljs (formatter (cond-> value (coll? value) perf/clj->js)))
:children (format-values-in-tree children (rest formatters) (rest cols) (rest col-indexes))
:rawValue value
:clicked {:value value
diff --git a/test/metabase/pivot/core_test.cljc b/test/metabase/pivot/core_test.cljc
index 0e4d2befe98..87fd7246299 100644
--- a/test/metabase/pivot/core_test.cljc
+++ b/test/metabase/pivot/core_test.cljc
@@ -588,6 +588,83 @@
:maxDepthBelow 0}]
result)))))
+#?(:cljs
+ (deftest format-values-in-tree-converts-collections-to-js-test
+ (testing "format-values-in-tree converts Clojure collections to JavaScript before formatting"
+ (testing "Converts vectors to JavaScript arrays"
+ (let [received-values (atom [])
+ mock-formatter (fn [v]
+ (swap! received-values conj v)
+ (str "formatted: " v))
+ tree [{:value [1 2 3] :children []}]
+ formatters [mock-formatter]
+ cols [{:name "col0"}]
+ col-indexes [0]
+ result (#'pivot/format-values-in-tree tree formatters cols col-indexes)]
+ (is (= 1 (count @received-values))
+ "Formatter should be called once")
+ (is (array? (first @received-values))
+ "Formatter should receive a JavaScript array, not a Clojure vector")
+ (is (= [1 2 3] (vec (first @received-values)))
+ "JavaScript array should contain the correct values")
+ (is (= "formatted: 1,2,3" (:value (first result)))
+ "Result should contain the formatted value")))
+ (testing "Converts maps to JavaScript objects"
+ (let [received-values (atom [])
+ mock-formatter (fn [v]
+ (swap! received-values conj v)
+ (str "formatted: " (pr-str v)))
+ tree [{:value {:a 1 :b 2} :children []}]
+ formatters [mock-formatter]
+ cols [{:name "col0"}]
+ col-indexes [0]]
+ (#'pivot/format-values-in-tree tree formatters cols col-indexes)
+ (is (= 1 (count @received-values))
+ "Formatter should be called once")
+ (is (object? (first @received-values))
+ "Formatter should receive a JavaScript object, not a Clojure map")
+ (is (= 1 (.-a (first @received-values)))
+ "JavaScript object should have correct property 'a'")
+ (is (= 2 (.-b (first @received-values)))
+ "JavaScript object should have correct property 'b'")))
+ (testing "Does not convert non-collection values"
+ (let [received-values (atom [])
+ mock-formatter (fn [v]
+ (swap! received-values conj v)
+ (str "formatted: " v))
+ tree [{:value "string-value" :children []}
+ {:value 42 :children []}
+ {:value nil :children []}]
+ formatters [mock-formatter mock-formatter mock-formatter]
+ cols [{:name "col0"} {:name "col1"} {:name "col2"}]
+ col-indexes [0 1 2]]
+ (#'pivot/format-values-in-tree tree formatters cols col-indexes)
+ (is (= 3 (count @received-values))
+ "Formatter should be called three times")
+ (is (= "string-value" (first @received-values))
+ "String values should be passed through without conversion")
+ (is (= 42 (second @received-values))
+ "Number values should be passed through without conversion")
+ (is (nil? (nth @received-values 2))
+ "Nil values should be passed through without conversion")))
+ (testing "Handles nested collections recursively"
+ (let [received-values (atom [])
+ mock-formatter (fn [v]
+ (swap! received-values conj v)
+ "formatted")
+ tree [{:value [1 2]
+ :children [{:value {:x 10} :children []}]}]
+ formatters [mock-formatter mock-formatter]
+ cols [{:name "col0"} {:name "col1"}]
+ col-indexes [0 1]]
+ (#'pivot/format-values-in-tree tree formatters cols col-indexes)
+ (is (= 2 (count @received-values))
+ "Formatter should be called for both parent and child")
+ (is (array? (first @received-values))
+ "Parent formatter should receive a JavaScript array")
+ (is (object? (second @received-values))
+ "Child formatter should receive a JavaScript object"))))))
+
#?(:clj
(deftest ^:parallel ensure-is-int-test
(testing "ensure-is-int properly converts different numeric types for bitwise operations"