Skip to content

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

metadata.cljs: always prefer `lib/source` over `source` (#66858)

Timothy S. Deanbd89348a1118
diff --git a/src/metabase/lib/js/metadata.cljs b/src/metabase/lib/js/metadata.cljs
index 96a1438967c..31c6f2a149e 100644
--- a/src/metabase/lib/js/metadata.cljs
+++ b/src/metabase/lib/js/metadata.cljs
@@ -36,20 +36,23 @@
"Convert a JS object of *any* class to a ClojureScript object."
([xform obj]
(obj->clj xform obj {}))
- ([xform obj {:keys [use-plain-object?] :or {use-plain-object? true}}]
+ ([xform obj {:keys [use-plain-object? skip-keys]
+ :or {use-plain-object? true
+ skip-keys #{}}}]
(if (map? obj)
;; already a ClojureScript object.
- (into {} xform obj)
+ (into {} xform (m/remove-keys skip-keys obj))
;; has a plain-JavaScript `_plainObject` attached: apply `xform` to it and call it a day
(if-let [plain-object (when use-plain-object?
(some-> (object-get obj "_plainObject")
js->clj
not-empty))]
- (into {} xform plain-object)
+ (into {} xform (m/remove-keys skip-keys plain-object))
;; otherwise do things the hard way and convert an arbitrary object into a Cljs map. (`js->clj` doesn't work on
;; arbitrary classes other than `Object`)
(into {}
(comp
+ (remove (set skip-keys))
(map (fn [k]
[k (object-get obj k)]))
;; ignore values that are functions
@@ -299,13 +302,18 @@
;; land, and avoid the issue.
:field-ref (to-array v)
:lib/source (case v
+ ;; These are the legacy values from the "source" key
"aggregation" :source/aggregations
;; TODO (Cam 7/1/25) -- if we wanted to be smarter we could use `source =
;; breakout` to populate `:lib/breakout?` but I don't really think that's
;; super necessary.
"breakout" nil
"fields" nil
- (keyword "source" v))
+ ;; For other values: if already prefixed with "source/" (from the modern
+ ;; "lib/source" key), just keywordize it directly. Otherwise add the prefix.
+ (if (str/starts-with? v "source/")
+ (keyword v)
+ (keyword "source" v)))
:metabase.lib.field/temporal-unit (keyword v)
:inherited-temporal-unit (keyword v)
:semantic-type (keyword v)
@@ -319,9 +327,18 @@
(defmethod parse-object-fn* :field
[object-type opts]
- (let [f ((get-method parse-object-fn* :default) object-type opts)]
+ (let [default-impl (get-method parse-object-fn* :default)]
(fn [unparsed]
- (let [{{dimension-type :lib/type, :as dimension} ::dimension, ::keys [field-values], :as parsed} (f unparsed)]
+ (let [;; If the JS object has both "source" and "lib/source" keys, the default parsing would
+ ;; rename "source" -> :lib/source and process both, with last-one-wins semantics.
+ ;; Since the key ordering in the JS object is non-deterministic, this causes flaky behavior.
+ ;; When "lib/source" exists, we skip the legacy "source" key entirely.
+ ;; See issue #66721 for details. Will be resolved by QUE-1404.
+ opts (if (object-get unparsed "lib/source")
+ (update opts :skip-keys (fnil conj #{}) "source")
+ opts)
+ f (default-impl object-type opts)
+ {{dimension-type :lib/type, :as dimension} ::dimension, ::keys [field-values], :as parsed} (f unparsed)]
(-> (case dimension-type
:metadata.column.remapping/external
(assoc parsed :lib/external-remap dimension)
diff --git a/test/metabase/lib/js/metadata_test.cljs b/test/metabase/lib/js/metadata_test.cljs
index 1c6a6015607..b6182c38913 100644
--- a/test/metabase/lib/js/metadata_test.cljs
+++ b/test/metabase/lib/js/metadata_test.cljs
@@ -1,6 +1,6 @@
(ns metabase.lib.js.metadata-test
(:require
- [clojure.test :refer [deftest is]]
+ [clojure.test :refer [deftest is testing]]
[metabase.lib.core :as lib]
[metabase.lib.js.metadata :as lib.js.metadata]
[metabase.lib.metadata :as lib.metadata]))
@@ -68,3 +68,24 @@
query {:lib/type :mbql/query, :database 1, :lib/metadata mp, :stages [{:lib/type :mbql.stage/mbql, :source-table 2}]}]
(is (= query
(lib/normalize query)))))
+
+(deftest ^:parallel parse-column-lib-source-test
+ ;; See issue #66721 - when both "source" and "lib/source" exist in a JS column object,
+ ;; we should prefer "lib/source" and parse it correctly.
+ ;; Will be resolved by QUE-1404.
+ (testing "legacy 'source' key only"
+ (let [column (lib.js.metadata/parse-column #js {"name" "count", "source" "aggregation"})]
+ (is (= :source/aggregations (:lib/source column)))))
+ (testing "modern 'lib/source' key only"
+ (let [column (lib.js.metadata/parse-column #js {"name" "count", "lib/source" "source/aggregations"})]
+ (is (= :source/aggregations (:lib/source column)))))
+ (testing "both keys present - 'lib/source' should win"
+ ;; This was the bug: when both keys exist, JS object iteration order is non-deterministic,
+ ;; causing flaky behavior. Now we skip 'source' when 'lib/source' exists.
+ (let [column (lib.js.metadata/parse-column #js {"name" "count"
+ "source" "aggregation"
+ "lib/source" "source/aggregations"})]
+ (is (= :source/aggregations (:lib/source column)))))
+ (testing "other lib/source values are handled correctly"
+ (let [column (lib.js.metadata/parse-column #js {"name" "id", "lib/source" "source/table-defaults"})]
+ (is (= :source/table-defaults (:lib/source column))))))