The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Handle trailing spaces appropriately in snippet template tags (#63582)
Timothy S. Dean94b9737f0b43
diff --git a/e2e/test/scenarios/native/snippet-tags.cy.spec.ts b/e2e/test/scenarios/native/snippet-tags.cy.spec.tsindex d2073288288..ba8f85b105d 100644--- a/e2e/test/scenarios/native/snippet-tags.cy.spec.ts+++ b/e2e/test/scenarios/native/snippet-tags.cy.spec.ts@@ -338,6 +338,31 @@ describe("scenarios > native > snippet tags", () => {H.assertTableRowsCount(53);});});++ it("should handle snippet tags with trailing spaces correctly", () => {+ cy.log("create a snippet");+ H.createSnippet({+ name: "category filter",+ content: "category = {{category}}",+ });++ H.startNewNativeQuestion();++ cy.log("type a query with snippet tags containing trailing spaces");+ H.NativeEditor.type(+ "select id from products where {{snippet: category filter }}",+ );++ cy.log("verify snippet reference is recognized");+ getEditorTopBar().within(() => {+ cy.findByPlaceholderText("Category").should("be.visible");+ });++ cy.log("verify the query can be run with the parameter");+ getEditorTopBar().findByPlaceholderText("Category").type("Widget");+ H.runNativeQuery();+ H.assertQueryBuilderRowCount(54);+ });});function getEditorSidebar() {diff --git a/src/metabase/lib/native.cljc b/src/metabase/lib/native.cljcindex 275533eef75..55f88aa6ceb 100644--- a/src/metabase/lib/native.cljc+++ b/src/metabase/lib/native.cljc@@ -22,25 +22,60 @@[metabase.util.malli.registry :as mr][metabase.util.performance :refer [some select-keys mapv every?]]))+;; Template Tags: Variables+(def ^:private variable-tag-regex#"\{\{\s*([A-Za-z0-9_\.]+)\s*\}\}")+(defn- normalize-variable-tag+ "Matches and normalizes a variable tag like {{my_var}}.+ Returns normalized-name or nil if not a variable tag."+ [full-tag]+ (when-let [[_ content] (re-matches variable-tag-regex full-tag)]+ content))++;; Template Tags: Snippets+(def ^:private snippet-tag-regex- #"\{\{\s*(snippet:\s*[^}]+)\s*\}\}")+ ;; any spaces, snippet:, any spaces, name, any trailing spaces+ #"\{\{\s*(snippet:\s*[^}]*[^}\s])\s*\}\}")++(defn- tag-name->snippet-name [tag-name]+ (when (str/starts-with? tag-name "snippet:")+ (str/trim (subs tag-name (count "snippet:")))))++(defn- normalize-snippet-tag+ "Normalizes a snippet tag like {{snippet: foo}}. E.g., 'snippet: foo ' -> 'snippet: foo'.+ Returns normalized string or nil if not a snippet tag."+ [full-tag]+ (when-let [[_ content] (re-matches snippet-tag-regex full-tag)]+ (let [snippet-name (tag-name->snippet-name content)]+ (str "snippet: " snippet-name))))++;; Template Tags: Cards(def ^:private card-tag-regex#"\{\{\s*(#([0-9]*)(-[a-z0-9-]*)?)\s*\}\}")-(def ^:private tag-regexes- [variable-tag-regex snippet-tag-regex card-tag-regex])-(defn- tag-name->card-id [tag-name](when-let [[_ id-str] (re-matches #"^#(\d+)(-[a-z0-9-]*)?$" tag-name)](parse-long id-str)))-(defn- tag-name->snippet-name [tag-name]- (when (str/starts-with? tag-name "snippet:")- (str/trim (subs tag-name (count "snippet:")))))+(defn- normalize-card-tag+ "Matches and normalizes a card tag like {{#123}} or {{#123-slug}}.+ Normalizes '#123-slug' -> '#123'.+ Returns normalized-name or nil if not a card tag."+ [full-tag]+ (when-let [[_ content _card-id _slug] (re-matches card-tag-regex full-tag)]+ ;; TODO: see tech debt issue #39378 and `native-test/card-tag-test`+ content))++(def ^:private match-and-normalize-tag-name+ "Matches a full tag string against tag normalizer functions and returns+ normalized-name or nil if no match."+ (some-fn normalize-variable-tag+ normalize-snippet-tag+ normalize-card-tag))(defn- finish-tag [{tag-name :name :as tag}](merge tag@@ -70,10 +105,11 @@[_ :guard string?] (recur found more)[{:type ::lib.parse/param, :name tag-name}]- (let [full-tag (str "{{" tag-name "}}")- [_ matched-name] (some #(re-matches % full-tag) tag-regexes)]+ (let [full-tag (str "{{" tag-name "}}")+ normalized-name (match-and-normalize-tag-name full-tag)](recur (cond-> found- (and matched-name (not (found matched-name))) (assoc matched-name (fresh-tag matched-name)))+ (and normalized-name (not (found normalized-name)))+ (assoc normalized-name (fresh-tag normalized-name)))more))[{:type ::lib.parse/optionaldiff --git a/test/metabase/lib/native_test.cljc b/test/metabase/lib/native_test.cljcindex 4026e199b82..461779379cf 100644--- a/test/metabase/lib/native_test.cljc+++ b/test/metabase/lib/native_test.cljc@@ -23,12 +23,9 @@(deftest ^:parallel snippet-tag-test(are [exp input] (= exp (set (keys (lib.native/extract-template-tags meta/metadata-provider input))))- #{"snippet: foo "} "SELECT * FROM table WHERE {{snippet: foo }} AND some_field IS NOT NULL"- #{"snippet: foo *#&@"} "SELECT * FROM table WHERE {{snippet: foo *#&@}}"- ;; TODO: This logic should trim the whitespace and unify these two snippet names.- ;; I think this is a bug in the original code but am aiming to reproduce it exactly for now.- ;; Tech debt issue: #39378- #{"snippet: foo" "snippet:foo"} "SELECT * FROM table WHERE {{snippet: foo}} AND {{snippet:foo}}"))+ #{"snippet: foo"} "SELECT * FROM table WHERE {{snippet: foo }} AND some_field IS NOT NULL"+ #{"snippet: foo *#&@"} "SELECT * FROM table WHERE {{snippet: foo *#&@}}"+ #{"snippet: foo"} "SELECT * FROM table WHERE {{snippet: foo}} AND {{snippet:foo}}"))(deftest ^:parallel card-tag-test(are [exp input] (= exp (set (keys (lib.native/extract-template-tags meta/metadata-provider input))))@@ -42,29 +39,22 @@(deftest ^:parallel template-tags-test(testing "snippet tags"- (let [snippet {:type :snippet- :name "foo"- :id 1}+ (let [snippet {:type :snippet+ :name "foo"+ :id 1}metadata-provider (lib.tu/mock-metadata-provider{:native-query-snippets [snippet]})]- (is (=? {"snippet:foo" {:type :snippet- :name "snippet:foo"- :snippet-name "foo"- :snippet-id 1- :id string?}}- (lib.native/extract-template-tags metadata-provider "SELECT * FROM table WHERE {{snippet:foo}}")))- (is (=? {"snippet:foo" {:type :snippet- :name "snippet:foo"+ (is (=? {"snippet: foo" {:type :snippet+ :name "snippet: foo":snippet-name "foo":snippet-id 1- :id string?}- "snippet: foo" {:type :snippet+ :id string?}}+ (lib.native/extract-template-tags metadata-provider "SELECT * FROM table WHERE {{snippet:foo}}")))+ (is (=? {"snippet: foo" {:type :snippet:name "snippet: foo":snippet-name "foo":snippet-id 1:id string?}}- ;; TODO: This should probably be considered a bug - whitespace matters for the name.- ;; Tech debt issue: #39378(lib.native/extract-template-tags metadata-provider "SELECT * FROM {{snippet: foo}} WHERE {{snippet:foo}}")))))(testing "renaming a variable"@@ -103,18 +93,18 @@(testing "general case, add and remove"(let [mktag (fn [base]- (merge {:type :text+ (merge {:type :text:display-name (u.humanization/name->human-readable-name :simple (:name base)):id string?}base))v1 (mktag {:name "foo"})v2 (mktag {:name "bar"})v3 (mktag {:name "baz"})- s1 (mktag {:name "snippet:first snippet"+ s1 (mktag {:name "snippet: first snippet":snippet-name "first snippet":snippet-id 123:type :snippet})- s2 (mktag {:name "snippet:another snippet"+ s2 (mktag {:name "snippet: another snippet":snippet-name "another snippet":snippet-id 124:type :snippet})@@ -130,28 +120,28 @@:id 123}{:name "another snippet":id 124}]})]- (is (=? {"foo" v1- "#123-card-1" c1- "snippet:first snippet" s1}+ (is (=? {"foo" v1+ "#123-card-1" c1+ "snippet: first snippet" s1}(lib.native/extract-template-tagsmetadata-provider"SELECT * FROM {{#123-card-1}} WHERE {{foo}} AND {{ snippet:first snippet}}")))- (is (=? {"bar" v2- "baz" v3- "snippet:another snippet" s2- "#321" c2}+ (is (=? {"bar" v2+ "baz" v3+ "snippet: another snippet" s2+ "#321" c2}(lib.native/extract-template-tagsmetadata-provider"SELECT * FROM {{#321}} WHERE {{baz}} AND {{bar}} AND {{snippet:another snippet}}"- {"foo" (assoc v1 :id (str (random-uuid)))- "#123-card-1" (assoc c1 :id (str (random-uuid)))- "snippet:first snippet" (assoc s1 :id (str (random-uuid)))})))+ {"foo" (assoc v1 :id (str (random-uuid)))+ "#123-card-1" (assoc c1 :id (str (random-uuid)))+ "snippet: first snippet" (assoc s1 :id (str (random-uuid)))})))(let [s1-uuid (str (random-uuid))]- (is (= {"snippet:another snippet" (assoc s2 :id s1-uuid)}+ (is (= {"snippet: another snippet" (assoc s2 :id s1-uuid)}(lib.native/extract-template-tagsmetadata-provider"SELECT * FROM {{snippet:another snippet}}"- {"snippet:first snippet" (assoc s1 :id s1-uuid)})))))))+ {"snippet: first snippet" (assoc s1 :id s1-uuid)})))))))(def ^:private qp-results-metadata"Capture of the `data.results_metadata` that would come back when running `SELECT * FROM VENUES;` with the Query