Skip to content

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

Declare two request params the handlers read but never declared (#79594)

Alexander Polyankin97e8d670363c
diff --git a/src/metabase/queries_rest/api/card.clj b/src/metabase/queries_rest/api/card.clj
index 6c65ee08714..9ced165f1c3 100644
--- a/src/metabase/queries_rest/api/card.clj
+++ b/src/metabase/queries_rest/api/card.clj
@@ -1044,7 +1044,8 @@
{:keys [parameters ignore_cache dashboard_id]
:or {ignore_cache false}} :- [:map
[:ignore_cache {:optional true} [:maybe :boolean]]
- [:dashboard_id {:optional true} [:maybe ms/PositiveInt]]]]
+ [:dashboard_id {:optional true} [:maybe ms/PositiveInt]]
+ [:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter-with-value]]]]]
(let [card (api/check-404 (t2/select-one :model/Card card-id))]
(when dashboard_id
(api/read-check :model/Dashboard dashboard_id))
diff --git a/src/metabase/sync/api/notify.clj b/src/metabase/sync/api/notify.clj
index 65f8c773db2..eba73e9f732 100644
--- a/src/metabase/sync/api/notify.clj
+++ b/src/metabase/sync/api/notify.clj
@@ -33,7 +33,8 @@
{:keys [table_id table_name scan synchronous?]} :- [:map
[:table_id {:optional true} [:maybe ms/PositiveInt]]
[:table_name {:optional true} [:maybe ms/NonBlankString]]
- [:scan {:optional true} [:maybe [:enum "full" "schema"]]]]]
+ [:scan {:optional true} [:maybe [:enum "full" "schema"]]]
+ [:synchronous? {:optional true} [:maybe :boolean]]]]
(let [schema? (when scan (#{"schema" :schema} scan))
table-sync-fn (if schema? sync-metadata/sync-table-metadata! sync/sync-table!)
db-sync-fn (if schema? sync-metadata/sync-db-metadata! sync/sync-database!)]
diff --git a/test/metabase/queries_rest/api/card_test.clj b/test/metabase/queries_rest/api/card_test.clj
index 69f715549f1..e09de78e1f8 100644
--- a/test/metabase/queries_rest/api/card_test.clj
+++ b/test/metabase/queries_rest/api/card_test.clj
@@ -3269,6 +3269,24 @@
(is (= ["MS" "Organic" "Gizmo" 0 16 42] (nth rows 445)))
(is (= [nil nil nil 7 18760 69540] (last rows))))))))))
+(deftest ^:parallel pivot-card-parameters-test
+ (testing "POST /api/card/pivot/:card-id/query"
+ (testing "should respect `:parameters`"
+ ;; the sibling assertion for the non-pivot route lives in `run-query-with-parameters-test`; the
+ ;; template tag is `:required`, so a dropped `:parameters` cannot even produce a result
+ (with-temp-native-card-with-params [{db-id :id} {card-id :id}]
+ ;; the card's `{{category}}` tag is `:required`, so the query can only run if the value in the
+ ;; request body reaches the query processor — were `:parameters` dropped, this 500s
+ (is (=? {:database_id db-id
+ :status "completed"
+ :row_count 1}
+ (mt/user-http-request
+ :rasta :post 202 (format "card/pivot/%d/query" card-id)
+ {:parameters [{:id "_CATEGORY_"
+ :type :number
+ :target [:variable [:template-tag :category]]
+ :value 2}]})))))))
+
(deftest ^:parallel model-card-test
(testing "Setting a question to a dataset makes it viz type table"
(mt/with-temp [:model/Card card {:display :bar
diff --git a/test/metabase/sync/api/notify_test.clj b/test/metabase/sync/api/notify_test.clj
index a703b228dd9..2e30f56ce9a 100644
--- a/test/metabase/sync/api/notify_test.clj
+++ b/test/metabase/sync/api/notify_test.clj
@@ -105,6 +105,30 @@
(is (= {:scan "nullable enum of full, schema"}
(:errors (post {:scan :unrecognized} 400))))))))
+(deftest synchronous-flag-test
+ (testing "POST /api/notify/db/:id withholds the response until the sync finishes when :synchronous? is true"
+ (let [sync-started (promise)
+ release-sync (promise)
+ ;; realize the test database before the redef is installed — creating it runs a sync of its
+ ;; own, which would otherwise block on `release-sync`
+ db-id (mt/id)]
+ (mt/with-dynamic-fn-redefs [sync/sync-database! (fn [_db]
+ (deliver sync-started true)
+ (deref release-sync 10000 :timed-out))]
+ (mt/with-temporary-setting-values [api-key "test-api-key"]
+ (let [response (future (mt/client :post 200 (format "notify/db/%d" db-id)
+ {:request-options api-headers}
+ {:synchronous? true}))]
+ (is (true? (deref sync-started 10000 nil))
+ "the sync should have been triggered")
+ ;; The whole effect of `:synchronous?` is `(cond-> (future ...) synchronous? deref)`, so a
+ ;; synchronous request cannot answer while the sync is still running. Were the flag dropped
+ ;; from the request body, the response would arrive well inside this window. Waiting on a
+ ;; timeout rather than `realized?` keeps this from racing the HTTP round-trip.
+ (is (= :still-syncing (deref response 1000 :still-syncing)))
+ (deliver release-sync true)
+ (is (= {:success true} (deref response 10000 nil)))))))))
+
(deftest add-new-table-sync-test
(mt/test-driver :postgres
(testing "Ensure we have the ability to add a single new table"