From 0ec52a40b57289d33409d81c8188219f22a59b92 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Thu, 16 Jan 2025 11:21:58 -0500 Subject: [PATCH] Use `Liquid::Utils.to_s` for `join` filter (#1897) --- lib/liquid/standardfilters.rb | 13 ++++++++++++- test/integration/hash_rendering_test.rb | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 73dfe346..a18b986f 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -1063,7 +1063,18 @@ module Liquid end def join(glue) - to_a.join(glue.to_s) + first = true + output = +"" + @input.each do |item| + if first + first = false + else + output << glue + end + + output << Liquid::Utils.to_s(item) + end + output end def concat(args) diff --git a/test/integration/hash_rendering_test.rb b/test/integration/hash_rendering_test.rb index 39933c55..47e6288b 100644 --- a/test/integration/hash_rendering_test.rb +++ b/test/integration/hash_rendering_test.rb @@ -77,6 +77,12 @@ class HashRenderingTest < Minitest::Test assert_template_result("{\"numbers\"=>[{:foo=>42}]}", "{{ my_hash }}", { "my_hash" => { "numbers" => [{ foo: 42 }] } }) end + def test_join_filter_with_hash + array = [{ "key1" => "value1" }, { "key2" => "value2" }] + glue = { "lol" => "wut" } + assert_template_result("{\"key1\"=>\"value1\"}{\"lol\"=>\"wut\"}{\"key2\"=>\"value2\"}", "{{ my_array | join: glue }}", { "my_array" => array, "glue" => glue }) + end + def test_render_hash_with_hash_key assert_template_result("{{\"foo\"=>\"bar\"}=>42}", "{{ my_hash }}", { "my_hash" => { Hash["foo" => "bar"] => 42 } }) end