Skip to content

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

Fix typed schema action template tag handling (#82488)

Phoomparin Mano0694a11c901e
diff --git a/src/metabase/typed_schemas/build.clj b/src/metabase/typed_schemas/build.clj
index ab9c86c5ae0..8197aa12944 100644
--- a/src/metabase/typed_schemas/build.clj
+++ b/src/metabase/typed_schemas/build.clj
@@ -48,9 +48,8 @@
[:include-metric-library? {:optional true}
[:boolean {:description "Whether to include the root metrics library."}]]
[:include-models? {:optional true}
- [:boolean {:description (str "Whether to include readable models with actions when no "
- "database scope is given. A `:database` scope always includes "
- "that database's models, regardless of this option.")}]]])
+ [:boolean {:description (str "Include models with actions. Database scope filters models; "
+ "library scope does not. Without a scope, returns models only.")}]]])
(def Items
"Fetched schema entities, ready for pure assembly by [[create-schema]].
@@ -92,15 +91,6 @@
{:tables (source/tables source nil table-ids)
:metrics metrics}))
-(defn- models-for-scope
- "Returns `{:models [...] :errors [...]}` scoped to `database-ids`, or all readable
- models when requested without a database scope."
- [source database-ids include-models?]
- (cond
- database-ids (source/models source database-ids)
- include-models? (source/models source nil)
- :else {:models [] :errors []}))
-
(defn fetch-items
"Fetches the schema entities selected by [[SemanticSchemaOptions]].
@@ -129,7 +119,10 @@
:include-metric-library? include-metric-library?})
database-ids (source/database-ids source database)
{model-schemas :models
- model-errors :errors} (models-for-scope source database-ids include-models?)]
+ model-errors :errors} (if include-models?
+ ;; database-ids scopes the models; nil reads all of them
+ (source/models source database-ids)
+ {:models [] :errors []})]
(if (or library-scope
(and include-models? (nil? database-ids)))
(let [{:keys [tables metrics]} (when library-scope
diff --git a/src/metabase/typed_schemas/schema/model.clj b/src/metabase/typed_schemas/schema/model.clj
index 71937f050e7..d3411ddd32c 100644
--- a/src/metabase/typed_schemas/schema/model.clj
+++ b/src/metabase/typed_schemas/schema/model.clj
@@ -4,6 +4,7 @@
[clojure.string :as str]
[medley.core :as m]
[metabase.actions.core :as actions]
+ [metabase.lib.core :as lib]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.typed-schemas.common :as common]
[metabase.typed-schemas.db :as typed-schemas.db]
@@ -94,35 +95,20 @@
"boolean" "boolean"
nil))))
-(defn- template-tag-name-from-target
- "Returns the template-tag name referenced by an action parameter target."
- [target]
- (when (sequential? target)
- (let [[op inner] target]
- (when (and (or (= op :variable) (= op "variable"))
- (sequential? inner))
- (let [[tag-op tag-name] inner]
- (when (or (= tag-op :template-tag) (= tag-op "template-tag"))
- (cond
- (string? tag-name) tag-name
- (keyword? tag-name) (clojure.core/name tag-name)
- :else nil)))))))
+(defn- template-tag-value-type
+ "Returns the type a template tag's values carry: a field filter's `:type` is always `:dimension`, so its
+ `:widget-type` (e.g. `:date/single`) is what tells us the value type."
+ [{:keys [type widget-type]}]
+ (if (= type :dimension)
+ widget-type
+ type))
(defn- query-action-template-tag-types
- "Returns template-tag types for query action parameters."
+ "Returns template-tag value types for query action parameters, or nil when the action has no usable query."
[{:keys [type dataset_query]}]
- (when (and (= (lib.schema.common/normalize-keyword type) :query) dataset_query)
- (let [stage-tags (some-> dataset_query :stages first :template-tags)
- native-tags (some-> dataset_query :native :template-tags)
- tags (or stage-tags native-tags)]
- (into {}
- (for [[tag-key tag] tags]
- [(or (:name tag)
- (cond
- (string? tag-key) tag-key
- (keyword? tag-key) (clojure.core/name tag-key)
- :else nil))
- (:type tag)])))))
+ (when (= (lib.schema.common/normalize-keyword type) :query)
+ ;; a stored query that failed to deserialize comes back as {}, which Lib rejects
+ (some-> dataset_query not-empty lib/all-template-tags-map (update-vals template-tag-value-type))))
(defn- model-action-error-message
"Returns the error message for model action schema failures."
@@ -155,7 +141,7 @@
resolved-type (or (param-type->js-type type)
(param-type->js-type
(get tag-types
- (template-tag-name-from-target target)))
+ (some-> target lib/parameter-target-template-tag-name)))
"unknown")]
(m/assoc-some
{:slug resolved-slug
@@ -183,11 +169,7 @@
"Returns action details from the actions module, preserving lookup error context."
[model]
(try
- (actions/select-actions
- nil
- :model_id (:id model)
- :archived false
- :type [:not= "http"])
+ (actions/select-actions-non-http-for-models [model] #{(:id model)})
(catch Exception exception
(throw (ex-info (model-action-error-message model (ex-message exception))
(error-data-with-cause-message
diff --git a/src/metabase/typed_schemas_rest/api.clj b/src/metabase/typed_schemas_rest/api.clj
index 39452bbe877..8d39f1ca0bb 100644
--- a/src/metabase/typed_schemas_rest/api.clj
+++ b/src/metabase/typed_schemas_rest/api.clj
@@ -35,8 +35,8 @@
[:maybe {:description "Whether to include the entire metric library."}
:boolean]]
[:include-models {:optional true}
- [:maybe {:description (str "Whether to include all readable models with executable actions. "
- "Database scope applies when provided.")}
+ [:maybe {:description (str "Include models with actions. Database scope filters models; "
+ "library scope does not. Without a scope, returns models only.")}
:boolean]]])
(api.macros/defendpoint :get "/v1/typescript" :- :any
diff --git a/test/metabase/typed_schemas/core_test.clj b/test/metabase/typed_schemas/core_test.clj
index 20a88738dad..620ac218f31 100644
--- a/test/metabase/typed_schemas/core_test.clj
+++ b/test/metabase/typed_schemas/core_test.clj
@@ -120,6 +120,30 @@
:message "Failed to build action schemas for model \"Broken\" (card 7): boom"}]}
(build/fetch-items {:include-models? true} source))))))
+(deftest fetch-items-includes-models-only-when-requested-test
+ (let [model-calls (atom [])
+ source (reify source/SchemaSource
+ (database-ids [_ _] #{26})
+ (collection-ids [_ _] nil)
+ (library-scope [_ _] nil)
+ (questions [_ _ _] [])
+ (models [_ database-ids]
+ (swap! model-calls conj database-ids)
+ {:models [] :errors []})
+ (metrics [_ _ _] [])
+ (tables [_ _ _] [])
+ (library-tables [_ _] []))]
+ (testing "database scope does not include models by default"
+ (is (= {:models [], :tables [], :metrics [], :errors []}
+ (build/fetch-items {:database {:id 26}} source)))
+ (is (empty? @model-calls)))
+ (testing "include-models scopes models to the database"
+ (is (= {:models [], :tables [], :metrics [], :errors []}
+ (build/fetch-items {:database {:id 26}
+ :include-models? true}
+ source)))
+ (is (= [#{26}] @model-calls)))))
+
(deftest create-schema-includes-errors-when-present-test
(testing "errors are omitted from a healthy schema and included when present"
(let [base-items {:models [], :tables [], :metrics []}]
@@ -161,7 +185,9 @@
:model/Action action {:name "Update order", :model_id (:id model), :type :implicit}
:model/ImplicitAction _ {:action_id (:id action), :kind "row/update"}]
(mt/with-current-user (mt/user->id :crowberto)
- (let [schema (typed-schemas/build-semantic-schema {:database {:id (mt/id)}} test-info)
+ (let [schema (typed-schemas/build-semantic-schema {:database {:id (mt/id)}
+ :include-models? true}
+ test-info)
body (typed-schemas/render-typescript schema)]
(testing "every entity kind lands in the schema with its real relationships"
(is (=? {:generatedAt "2026-01-01T00:00:00Z"
diff --git a/test/metabase/typed_schemas/schema/model_test.clj b/test/metabase/typed_schemas/schema/model_test.clj
index 799c4dd4fa7..c63e010cf3b 100644
--- a/test/metabase/typed_schemas/schema/model_test.clj
+++ b/test/metabase/typed_schemas/schema/model_test.clj
@@ -2,6 +2,8 @@
(:require
[clojure.test :refer :all]
[metabase.actions.core :as actions]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
[metabase.test :as mt]
[metabase.typed-schemas.schema.common :as schema.common]
[metabase.typed-schemas.schema.model :as schema.model]))
@@ -18,6 +20,67 @@
{:id 42
:name "Orders model"})))))
+(deftest model-schema-supports-persisted-mbql5-template-tags-test
+ (let [mp (mt/metadata-provider)
+ action-query (lib/native-query mp "UPDATE birds SET name = {{name}}")]
+ (mt/with-actions [model {:name "Bird model"
+ :type :model
+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :categories)))}
+ {action-id :action-id} {:name "Update bird"
+ :database_id (mt/id)
+ :dataset_query action-query
+ ;; :category has no JS type of its own, so the result depends on the
+ ;; persisted template tag's type
+ :parameters [{:id "name"
+ :name "Name"
+ :type :category
+ :target [:variable [:template-tag "name"]]}]}]
+ (let [action (actions/select-action :id action-id)]
+ (is (sequential? (get-in action [:dataset_query :stages 0 :template-tags])))
+ (is (=? [{:slug "name", :displayName "Name", :jsType "string"}]
+ (get-in (schema.model/model-schema model)
+ [:actions "updateBird" :parameters])))))))
+
+(deftest model-schema-resolves-field-filter-widget-type-test
+ (testing "a field filter's value type comes from its widget type, through a :dimension parameter target"
+ (let [mp (mt/metadata-provider)
+ action-query (-> (lib/native-query mp "UPDATE birds SET name = 'x' WHERE {{created_at}}")
+ (lib/with-template-tags
+ {"created_at" {:name "created_at"
+ :display-name "Created At"
+ :type :dimension
+ :widget-type :date/single
+ :dimension (lib/ref (lib.metadata/field mp (mt/id :categories :name)))}}))]
+ (mt/with-actions [model {:name "Bird model"
+ :type :model
+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :categories)))}
+ {action-id :action-id} {:name "Update bird"
+ :database_id (mt/id)
+ :dataset_query action-query
+ :parameters [{:id "created_at"
+ :name "Created At"
+ :type :category
+ :target [:dimension [:template-tag "created_at"]]}]}]
+ (is (= :dimension (get-in (actions/select-action :id action-id) [:dataset_query :stages 0 :template-tags 0 :type])))
+ (is (=? [{:slug "created_at", :displayName "Created At", :jsType "Date"}]
+ (get-in (schema.model/model-schema model)
+ [:actions "updateBird" :parameters])))))))
+
+(deftest model-schema-tolerates-empty-action-query-test
+ (testing "an action whose stored query degraded to {} still builds instead of failing the whole model"
+ (mt/with-actions [model {:name "Bird model"
+ :type :model
+ :dataset_query (let [mp (mt/metadata-provider)]
+ (lib/query mp (lib.metadata/table mp (mt/id :categories))))}
+ {action-id :action-id} {:name "Update bird"
+ :database_id (mt/id)
+ :dataset_query {}
+ :parameters [{:id "name", :name "Name", :type :text}]}]
+ (is (= {} (:dataset_query (actions/select-action :id action-id))))
+ (is (=? [{:slug "name", :displayName "Name", :jsType "string"}]
+ (get-in (schema.model/model-schema model)
+ [:actions "updateBird" :parameters]))))))
+
(deftest model-schemas-includes-only-actionable-models-test
(with-redefs [schema.common/select-schema-cards
(constantly [{:id 42 :name "Model 42"}
@@ -82,18 +145,22 @@
{:id 43 :name "Broken model"}])
;; bulk lookup blows up for the whole batch
actions/select-actions-non-http-for-models
- (fn [& _] (throw (ex-info "bulk lookup exploded" {})))
+ (fn [_known-models model-ids]
+ (cond
+ (< 1 (count model-ids))
+ (throw (ex-info "bulk lookup exploded" {}))
+
+ (= model-ids #{42})
+ [{:id 5 :model_id 42 :name "Create" :type :query :parameters []}]
+
+ :else
+ (throw (ex-info "action lookup failed" {:status-code 500}))))
;; per-model fallback: model 42 resolves, model 43 still fails
schema.model/action-rows
(fn [model-ids]
(if (contains? model-ids 42)
[{:id 5 :model_id 42 :name "Create" :type :query}]
- []))
- actions/select-actions
- (fn [_ & {:keys [model_id]}]
- (if (= model_id 42)
- [{:id 5 :model_id 42 :name "Create" :type :query :parameters []}]
- (throw (ex-info "action lookup failed" {:status-code 500}))))]
+ []))]
(let [{:keys [models errors]} (schema.model/model-schemas #{1} nil)]
(is (= ["model42"] (map :key models)))
(is (=? [{:type "modelError"
@@ -136,9 +203,10 @@
(deftest model-schema-surfaces-action-selection-errors-test
(with-redefs [schema.model/action-rows (constantly [])
- actions/select-actions (fn [& _]
- (throw (ex-info "action lookup failed"
- {:status-code 500})))]
+ actions/select-actions-non-http-for-models
+ (fn [& _]
+ (throw (ex-info "action lookup failed"
+ {:status-code 500})))]
(let [exception (is (thrown? clojure.lang.ExceptionInfo
(schema.model/model-schema {:id 100
:name "Broken model"})))]
@@ -151,9 +219,10 @@
(ex-data exception))))))
(deftest model-schema-surfaces-action-rendering-errors-test
- (with-redefs [actions/select-actions (constantly [{:id 200
- :name "Broken action"
- :type :query}])
+ (with-redefs [actions/select-actions-non-http-for-models
+ (constantly [{:id 200
+ :name "Broken action"
+ :type :query}])
schema.model/action-rows (constantly [{:id 200
:name "Broken action"
:type :query}])
@@ -178,7 +247,7 @@
(with-redefs [schema.model/action-rows (constantly [{:id 200
:name "Broken action"
:type :broken}])
- actions/select-actions (constantly [])]
+ actions/select-actions-non-http-for-models (constantly [])]
(let [exception (is (thrown? clojure.lang.ExceptionInfo
(schema.model/model-schema {:id 100
:name "Broken model"})))]
diff --git a/test/metabase/typed_schemas_rest/api_test.clj b/test/metabase/typed_schemas_rest/api_test.clj
index 1b8b11213e5..f8744683541 100644
--- a/test/metabase/typed_schemas_rest/api_test.clj
+++ b/test/metabase/typed_schemas_rest/api_test.clj
@@ -2,6 +2,8 @@
(:require
[clojure.string :as str]
[clojure.test :refer :all]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.typed-schemas.core :as typed-schemas]
@@ -63,6 +65,29 @@
(is (str/includes? missing-schema "const tables = { }"))
(is (str/includes? missing-schema "const metrics = { }")))))
+(deftest database-scope-includes-models-only-when-requested-test
+ (mt/with-actions-enabled
+ (mt/with-temp [:model/Database other-db {}
+ :model/Card model {:name "Order model", :database_id (mt/id), :table_id (mt/id :orders)
+ :type :model
+ :dataset_query (let [mp (mt/metadata-provider)]
+ (lib/query mp (lib.metadata/table mp (mt/id :orders))))
+ :result_metadata [{:name "total", :display_name "Total"
+ :base_type :type/Float
+ :field_ref [:field (mt/id :orders :total) nil]
+ :id (mt/id :orders :total)}]}
+ :model/Action action {:name "Update order", :model_id (:id model), :type :implicit}
+ :model/ImplicitAction _ {:action_id (:id action), :kind "row/update"}]
+ (let [schema (fn [& query-params]
+ (:body (apply mt/user-http-request-full-response
+ :crowberto :get 200 "typed-schemas/v1/typescript" query-params)))]
+ (testing "a database scope leaves models out unless asked for"
+ (is (not (str/includes? (schema :database (mt/id)) "orderModel"))))
+ (testing "include-models adds the scoped database's models"
+ (is (str/includes? (schema :database (mt/id) :include-models true) "orderModel")))
+ (testing "include-models keeps models of other databases out"
+ (is (not (str/includes? (schema :database (:id other-db) :include-models true) "orderModel"))))))))
+
(deftest collection-and-database-query-params-are-mutually-exclusive-test
(mt/user-http-request-full-response
:crowberto