From 88b9ee0f79460e7a0a511495bc5f1ffbd6d753c2 Mon Sep 17 00:00:00 2001 From: lbrdnk <lbrdnk@users.noreply.github.com> Date: Thu, 10 Aug 2023 14:06:16 +0200 Subject: [PATCH] Update mongo native param substitution (#33003) Change is compatible with prior implementation. Match clause like the following, `{$match: {"something": "Facebook"}}`, does exact match if `something` is a scalar, and in case `something` was an array, match would check whether it contains the `Facebook` value. Same behavior, as with `{$match: {"something": {$in: ["Facebook"]}}}`. Co-authored-by: metamben <103100869+metamben@users.noreply.github.com> --- .../drivers/mongo/src/metabase/driver/mongo/parameters.clj | 4 ++++ .../mongo/test/metabase/driver/mongo/parameters_test.clj | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/drivers/mongo/src/metabase/driver/mongo/parameters.clj b/modules/drivers/mongo/src/metabase/driver/mongo/parameters.clj index 8e64991d74f..f24e3d541d9 100644 --- a/modules/drivers/mongo/src/metabase/driver/mongo/parameters.clj +++ b/modules/drivers/mongo/src/metabase/driver/mongo/parameters.clj @@ -35,6 +35,10 @@ (defn- param-value->str [{coercion :coercion_strategy, :as field} x] (cond + ;; #30136: Provide a way of using dashboard filter as a variable. + (and (sequential? x) (= (count x) 1)) + (recur field (first x)) + ;; sequences get converted to `$in` (sequential? x) (format "{$in: [%s]}" (str/join ", " (map (partial param-value->str field) x))) diff --git a/modules/drivers/mongo/test/metabase/driver/mongo/parameters_test.clj b/modules/drivers/mongo/test/metabase/driver/mongo/parameters_test.clj index 98c6cd8015b..6a886ba84a2 100644 --- a/modules/drivers/mongo/test/metabase/driver/mongo/parameters_test.clj +++ b/modules/drivers/mongo/test/metabase/driver/mongo/parameters_test.clj @@ -96,8 +96,8 @@ (is (= "{$in: [1, 2, 3]}" (substitute {:id [1 2 3]} [(param :id)])))) - (testing "multiple-values single (#22486)" - (is (= "{$in: [\"33 Taps\"]}" + (testing "multiple-values single (#30136)" + (is (= "\"33 Taps\"" (substitute {:id ["33 Taps"]} [(param :id)])))) (testing "multiple-values multi (#22486)" -- GitLab