Skip to content

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

Improve input_type assignments in /actions-v2/execute-form (#62097)

Chris Truterda57ec87d063
diff --git a/enterprise/backend/src/metabase_enterprise/action_v2/api.clj b/enterprise/backend/src/metabase_enterprise/action_v2/api.clj
index 74d53d1f5bf..111fcb4608b 100644
--- a/enterprise/backend/src/metabase_enterprise/action_v2/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/action_v2/api.clj
@@ -45,19 +45,6 @@
[:action-kw :keyword]
[:mapping [:maybe :map]]]])
-(mr/def ::execute-form
- "A description of the form to render for executing an action"
- [:map
- [:title :string]
- ;; TODO fully define the shape of a parameter
- [:parameters [:sequential [:map {:closed false}
- [:id :string]
- [:display_name :string]
- [:input_type :string]
- [:optional :boolean]
- [:readonly :boolean]
- [:value {:optional true} :any]]]]])
-
(mu/defn- fetch-unified-action :- ::action-expression
"Resolve various flavors of action-id into plain data, making it easier to dispatch on. Fetch config etc."
[scope :- ::types/scope.hydrated
@@ -200,7 +187,8 @@
[{}
{}
;; TODO support for bulk actions
- {:keys [action scope input]}] :- ::execute-form
+ {:keys [action scope input]}]
+ :- :metabase-enterprise.action-v2.execute-form/action-description
;; This check should be redundant in practice with the permission checks within perform-action!
;; Since test coverage is light and the logic is so simple, we've decided to be extra cautious for now.
(api/check-superuser)
diff --git a/enterprise/backend/src/metabase_enterprise/action_v2/execute_form.clj b/enterprise/backend/src/metabase_enterprise/action_v2/execute_form.clj
index 7d01829fccd..7ff6ee915e9 100644
--- a/enterprise/backend/src/metabase_enterprise/action_v2/execute_form.clj
+++ b/enterprise/backend/src/metabase_enterprise/action_v2/execute_form.clj
@@ -7,12 +7,33 @@
[metabase.util.malli.registry :as mr]
[toucan2.core :as t2]))
+(def ^:private strip-namespace-hack (comp keyword name))
+
+(mr/def ::input-type
+ ;; TODO this is a clumsy workaround due to the api encoders not being run for some reason
+ (mapv
+ #(if (keyword? %) (strip-namespace-hack %) %)
+
+ [:enum
+ {:encode/api name
+ :decode/api #(keyword "input" %)}
+
+ :input/boolean
+ :input/date
+ :input/datetime
+ :input/dropdown
+ :input/float
+ :input/integer
+ :input/text
+ :input/textarea
+ :input/time]))
+
(mr/def ::describe-param
[:map #_{:closed true}
[:id :string]
[:display_name :string]
[:field_id {:optional true} pos-int?]
- [:input_type [:enum "dropdown" "textarea" "date" "datetime" "text"]]
+ [:input_type ::input-type]
[:semantic_type {:optional true} :keyword]
[:optional :boolean]
;; TODO in practice this should never be null (and we strip nils current)
@@ -35,16 +56,26 @@
(defn- field-input-type
[field field-values]
- (case (:type field-values)
- (:list :auto-list :search) "dropdown"
- (condp #(isa? %2 %1) (:semantic_type field)
- :type/Description "textarea"
- :type/Category "dropdown"
- :type/FK "dropdown"
- (condp #(isa? %2 %1) (:base_type field)
- :type/Date "date"
- :type/DateTime "datetime"
- "text"))))
+ (condp #(isa? %2 %1) (:semantic_type field)
+ :type/Name :input/text
+ :type/Title :input/text
+ :type/Source :input/text
+ :type/Description :input/textarea
+ :type/Category :input/dropdown
+ :type/FK :input/dropdown
+ :type/PK :input/dropdown
+ (condp #(isa? %2 %1) (:base_type field)
+ :type/Boolean :input/boolean
+ :type/Integer :input/integer
+ :type/BigInteger :input/integer
+ :type/Float :input/float
+ :type/Decimal :input/float
+ :type/Date :input/date
+ :type/DateTime :input/datetime
+ :type/Time :input/time
+ (if (#{:list :auto-list :search} (:type field-values))
+ :input/dropdown
+ :input/text))))
(defn- describe-table-action
[{:keys [action-kw
@@ -96,7 +127,8 @@
{:id (:name field)
:display_name (:display_name field)
:semantic_type (:semantic_type field)
- :input_type (field-input-type field field-values)
+ ;; TODO we are manually removing the namespace due to an issue with encoder/api not running
+ :input_type (strip-namespace-hack (field-input-type field field-values))
:field_id (:id field)
:human_readable_field_id (-> field :dimensions first :human_readable_field_id)
:optional (not required)
@@ -123,8 +155,8 @@
;; TODO remove assumption that all primitives are table actions
(:action-kw action-def)
(describe-table-action
- {:action-kw (:action-kw action-def)
- :table-id (:table-id partial-input)
- :param-map (:param-map action-def)})
+ {:action-kw (:action-kw action-def)
+ :table-id (:table-id partial-input)
+ :param-map (:param-map action-def)})
:else
(throw (ex-info "Not able to execute given action yet" {:status-code 500 :scope scope :action-def action-def}))))
diff --git a/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj b/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
index 0ff38d17237..22929eea0d1 100644
--- a/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
@@ -4,6 +4,7 @@
[clojure.java.jdbc :as jdbc]
[clojure.set :as set]
[clojure.test :refer :all]
+ [flatland.ordered.map :refer [ordered-map]]
[metabase-enterprise.action-v2.api]
[metabase-enterprise.action-v2.coerce :as coerce]
[metabase-enterprise.action-v2.data-editing :as data-editing]
@@ -576,12 +577,16 @@
[:message])))))))
(testing "Non auto-incrementing pk"
- (action-v2.tu/with-test-tables! [table-id [{:id [:int]
+ (action-v2.tu/with-test-tables! [table-id [(ordered-map
+ :id [:int]
:text [:text]
:int [:int]
+ :bool [:boolean]
+ :float_val [:float]
:timestamp [:timestamp]
:date [:date]
- :inactive [:text]}
+ :time_val [:time]
+ :inactive [:text])
{:primary-key [:id]}]]
;; This inactive field should not show up
(t2/update! :model/Field {:table_id table-id, :name "inactive"} {:active false})
@@ -592,38 +597,48 @@
scope {:table-id table-id}]
(testing "create"
- (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "text" :optional false :readonly false}
- {:id "text" :display_name "Text" :input_type "text" :optional true :readonly false}
- {:id "int" :display_name "Int" :input_type "text" :optional true :readonly false}
- {:id "timestamp" :display_name "Timestamp" :input_type "datetime" :optional true :readonly false}
- {:id "date" :display_name "Date" :input_type "date" :optional true :readonly false}]}
+ (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "dropdown" :optional false :readonly false}
+ {:id "text" :display_name "Text" :input_type "text" :optional true :readonly false}
+ {:id "int" :display_name "Int" :input_type "integer" :optional true :readonly false}
+ {:id "bool" :display_name "Bool" :input_type "boolean" :optional true :readonly false}
+ {:id "float_val" :display_name "Float Val" :input_type "float" :optional true :readonly false}
+ {:id "timestamp" :display_name "Timestamp" :input_type "datetime" :optional true :readonly false}
+ {:id "date" :display_name "Date" :input_type "date" :optional true :readonly false}
+ {:id "time_val" :display_name "Time Val" :input_type "time" :optional true :readonly false}]}
(mt/user-http-request :crowberto :post 200 execute-form-url
{:scope scope
:action create-id}))))
(testing "update"
- (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "text" :optional false :readonly true}
- {:id "text" :display_name "Text" :input_type "text" :optional true :readonly false}
- {:id "int" :display_name "Int" :input_type "text" :optional true :readonly false}
- {:id "timestamp" :display_name "Timestamp" :input_type "datetime" :optional true :readonly false}
- {:id "date" :display_name "Date" :input_type "date" :optional true :readonly false}]}
+ (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "dropdown" :optional false :readonly true}
+ {:id "text" :display_name "Text" :input_type "text" :optional true :readonly false}
+ {:id "int" :display_name "Int" :input_type "integer" :optional true :readonly false}
+ {:id "bool" :display_name "Bool" :input_type "boolean" :optional true :readonly false}
+ {:id "float_val" :display_name "Float Val" :input_type "float" :optional true :readonly false}
+ {:id "timestamp" :display_name "Timestamp" :input_type "datetime" :optional true :readonly false}
+ {:id "date" :display_name "Date" :input_type "date" :optional true :readonly false}
+ {:id "time_val" :display_name "Time Val" :input_type "time" :optional true :readonly false}]}
(mt/user-http-request :crowberto :post 200 execute-form-url
{:scope scope
:action update-id}))))
(testing "delete"
- (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "text" :optional false :readonly true}]}
+ (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "dropdown" :optional false :readonly true}]}
(mt/user-http-request :crowberto :post 200 execute-form-url
{:scope scope
:action delete-id}))))))))
(testing "Auto incrementing pk"
- (action-v2.tu/with-test-tables! [table-id [{:id 'auto-inc-type
+ (action-v2.tu/with-test-tables! [table-id [(ordered-map
+ :id 'auto-inc-type
:text [:text]
:int [:int]
+ :bool [:boolean]
+ :float_val [:float]
:timestamp [:timestamp]
- :date [:date]
- :inactive [:text]}
+ :date [:date]
+ :time_val [:time]
+ :inactive [:text])
{:primary-key [:id]}]]
;; This inactive field should not show up
(t2/update! :model/Field {:table_id table-id, :name "inactive"} {:active false})
@@ -634,26 +649,32 @@
scope {:table-id table-id}]
(testing "create"
- (is (=? {:parameters [{:id "text" :display_name "Text" :input_type "text", :optional true, :readonly false}
- {:id "int" :display_name "Int" :input_type "text", :optional true, :readonly false}
- {:id "timestamp" :display_name "Timestamp" :input_type "datetime", :optional true, :readonly false}
- {:id "date" :display_name "Date" :input_type "date", :optional true, :readonly false}]}
+ (is (=? {:parameters [{:id "text" :display_name "Text" :input_type "text", :optional true, :readonly false}
+ {:id "int" :display_name "Int" :input_type "integer", :optional true, :readonly false}
+ {:id "bool" :display_name "Bool" :input_type "boolean", :optional true, :readonly false}
+ {:id "float_val" :display_name "Float Val" :input_type "float", :optional true, :readonly false}
+ {:id "timestamp" :display_name "Timestamp" :input_type "datetime", :optional true, :readonly false}
+ {:id "date" :display_name "Date" :input_type "date", :optional true, :readonly false}
+ {:id "time_val" :display_name "Time Val" :input_type "time", :optional true, :readonly false}]}
(mt/user-http-request :crowberto :post 200 execute-form-url
{:scope scope
:action create-id}))))
(testing "update"
- (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "text", :optional false, :readonly true}
- {:id "text" :display_name "Text" :input_type "text", :optional true, :readonly false}
- {:id "int" :display_name "Int" :input_type "text", :optional true, :readonly false}
- {:id "timestamp" :display_name "Timestamp" :input_type "datetime", :optional true, :readonly false}
- {:id "date" :display_name "Date" :input_type "date", :optional true, :readonly false}]}
+ (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "dropdown", :optional false, :readonly true}
+ {:id "text" :display_name "Text" :input_type "text", :optional true, :readonly false}
+ {:id "int" :display_name "Int" :input_type "integer", :optional true, :readonly false}
+ {:id "bool" :display_name "Bool" :input_type "boolean", :optional true, :readonly false}
+ {:id "float_val" :display_name "Float Val" :input_type "float", :optional true, :readonly false}
+ {:id "timestamp" :display_name "Timestamp" :input_type "datetime", :optional true, :readonly false}
+ {:id "date" :display_name "Date" :input_type "date", :optional true, :readonly false}
+ {:id "time_val" :display_name "Time Val" :input_type "time", :optional true, :readonly false}]}
(mt/user-http-request :crowberto :post 200 execute-form-url
{:scope scope
:action update-id}))))
(testing "delete"
- (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "text", :optional false, :readonly true}]}
+ (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "dropdown", :optional false, :readonly true}]}
(mt/user-http-request :crowberto :post 200 execute-form-url
{:scope scope
:action delete-id})))))))))))