From 3c7448ebfdd918d91770fd932f8d5f6b3db7b22e Mon Sep 17 00:00:00 2001 From: ccocchi Date: Tue, 3 Apr 2012 18:37:11 +0200 Subject: [PATCH] Dup the template to avoid in place modification across users --- lib/rabl-fast-json/library.rb | 1 + test/cache_templates_test.rb | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/rabl-fast-json/library.rb b/lib/rabl-fast-json/library.rb index b7f6661..20fb6f2 100644 --- a/lib/rabl-fast-json/library.rb +++ b/lib/rabl-fast-json/library.rb @@ -23,6 +23,7 @@ module RablFastJson def get_compiled_template(path, source) if path && RablFastJson.cache_templates? @cached_templates[path] ||= Compiler.new.compile_source(source) + @cached_templates[path].dup else Compiler.new.compile_source(source) end diff --git a/test/cache_templates_test.rb b/test/cache_templates_test.rb index c9e01c2..5de4e56 100644 --- a/test/cache_templates_test.rb +++ b/test/cache_templates_test.rb @@ -10,8 +10,19 @@ class CacheTemplatesTest < ActiveSupport::TestCase test "cache templates if perform_caching is active and cache_templates is enabled" do RablFastJson.cache_templates = true ActionController::Base.stub(:perform_caching).and_return(true) + @library.get_compiled_template('some/path', "") + t = @library.get_compiled_template('some/path', "attribute :id") - assert_equal @library.get_compiled_template('some/path', ""), @library.get_compiled_template('some/path', "") + assert_equal({}, t.source) + end + + test "cached templates should not be modifiable in place" do + RablFastJson.cache_templates = true + ActionController::Base.stub(:perform_caching).and_return(true) + t = @library.get_compiled_template('some/path', "") + assert_nil t.context + t.context = "foobar" + assert_nil @library.get_compiled_template('some/path', "").context end test "don't cache templates cache_templates is enabled but perform_caching is not active" do