Skip to content

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

Ensure we can resolve map lookups (#70060)

Nicola Mometto8c6e4aa60b0a
diff --git a/src/metabase/channel/template/handlebars.clj b/src/metabase/channel/template/handlebars.clj
index 29d8fc764cc..19a9baf4e04 100644
--- a/src/metabase/channel/template/handlebars.clj
+++ b/src/metabase/channel/template/handlebars.clj
@@ -7,8 +7,9 @@
[metabase.util.log :as log])
(:import
(com.github.jknack.handlebars
- Handlebars Template)
+ Handlebars Template Context)
(com.github.jknack.handlebars.cache ConcurrentMapTemplateCache)
+ (com.github.jknack.handlebars.context MapValueResolver)
(com.github.jknack.handlebars.io
ClassPathTemplateLoader)))
@@ -34,6 +35,14 @@
(u/qualified-name %)
%) context))
+(defn- build-context
+ [context]
+ (-> context
+ wrap-context
+ (Context/newBuilder)
+ (.resolver (into-array [(MapValueResolver/INSTANCE)]))
+ (.build)))
+
(def ^:private default-hbs
(delay (u/prog1 (registry (classpath-loader "/" "") :reload? true)
(handlebars-helper/register-helpers <> handlebars-helper/default-helpers))))
@@ -53,7 +62,7 @@
(render @default-hbs template-name context))
([^Handlebars req ^String template-name ctx]
(let [template ^Template (.compile req template-name)]
- (.apply template (wrap-context ctx)))))
+ (.apply template (build-context ctx)))))
(defn render-string
"Render a template string with a context."
@@ -61,7 +70,7 @@
(render-string @default-hbs template context))
([^Handlebars req ^String template ctx]
(.apply ^Template (.compileInline req template)
- (wrap-context ctx))))
+ (build-context ctx))))
(comment
(render-string "{{now}}" {})
diff --git a/test/metabase/channel/template/handlebars_test.clj b/test/metabase/channel/template/handlebars_test.clj
index 9beb7e6a0c7..771ed3d00c2 100644
--- a/test/metabase/channel/template/handlebars_test.clj
+++ b/test/metabase/channel/template/handlebars_test.clj
@@ -57,3 +57,13 @@
(testing "with custom req"
(with-temp-template! [tmpl-name "tmpl.hbs" "Hello {{uppercase name}}"]
(is (= "Hello NGOC" (handlebars/render custom-hbs tmpl-name {:name "Ngoc"}))))))
+
+(deftest dotted-path-resolution-works-on-maps-test
+ (are [template context]
+ (= "" (handlebars/render-string template context))
+ "{{x.y}}" {"x" "a string"}
+ "{{x.y}}" {"x" 42}
+ "{{x.toString}}" {"x" 42}
+ "{{x.y.z}}" {"x" {"y" (Object.)}})
+ (is (= "hello" (handlebars/render-string "{{x.y}}" {"x" {"y" "hello"}})))
+ (is (= "deep" (handlebars/render-string "{{a.b.c}}" {"a" {"b" {"c" "deep"}}}))))